在主题和插件开发中,我喜欢全面运行所有内容,以确保没有问题,也不会出现任何错误。只有在开发中,我才能在footer.php
要查看正在发生的事情:
if (is_user_logged_in()) :
// GET POST META
// https://developer.wordpress.org/reference/functions/get_post_meta/
global $wp_query;
$post_meta = get_post_meta($post->ID);
echo \'<h1>Post Meta</h1>\';
echo \'<pre>\';
var_dump($post_meta);
echo \'</pre>\';
wp_reset_query();
// SEE WHAT IS ASSIGNED
$queried_object = get_queried_object();
echo \'<h1>Quered Object</h1>\';
echo \'<pre>\';
var_dump($queried_object);
echo \'</pre>\';
wp_reset_query();
endif;
然而,由于我一直在尝试实现更多的设置页面,而不是小部件,所以我想在构建设置页面时测试我得到了什么,这样就不用调用每个设置页面了
get_option(\'whatever\');
我想转储所有内容,以便查看正在收集的内容以及是否出现任何错误。然而,当我这样做时:
echo \'<h1>Get Options</h1>\';
echo \'<pre>\';
var_dump(get_option());
echo \'</pre>\';
wp_reset_query();
我被抛出了一个错误。当我研究时
get_option()
我没有看到任何关于垃圾场的讨论,而且我在查看时也没有找到解决方案
get-option. 有什么方法可以把所有东西都扔掉吗
get_option()
?
编辑:
澄清一下,这在设置页面开发时很有用,那么有没有办法从设置中转储选项?例如:
function wpse_267354() {
add_submenu_page(
\'options-general.php\',
\'Foobar Page\', // $page_title
\'PAGE NAME\', // $menu_title
\'manage_options\', // $capability
\'wpse_267354_slug\', // $menu_slug
\'wpse_267354_function\' // $function
);
}
add_action( \'admin_menu\', \'wpse_267354\' );
SO网友:Michael Ecklund
您需要一种管理设置的方法。你可以使用任何你认为合适的管理方法。然而,我想出了一个相当“简单”的设置PHP Class
, 您可以使用它作为具有属性的对象来检索插件的设置。
对象是插件的设置。对象的属性包含实际设置本身。
很容易进行自定义和扩展。希望这能帮助您更轻松地维护插件和/或主题。
This class will:
<帮助您更好地理解插件在默认情况下是什么,以及用户希望插件做什么帮助您更好地了解插件的工作原理,减少与设置管理相关的意外错误的发生帮助您以结构化的方式一致地检索所有设置或特定设置
Example of Plugin Settings Class:
<?php
if ( ! defined( \'\\ABSPATH\' ) ) {
exit;
}
if ( class_exists( \'MBE_Settings\' ) ) {
return false;
}
/**
* This is an example Settings Class for managing Plugin Settings.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
* Class MBE_Settings
*/
class MBE_Settings {
/**
* @var array
*/
public $general = array();
/**
* @var array
*/
public $display = array();
/**
* @var array
*/
private $default_settings = array();
/**
* @var array
*/
private $user_settings = array();
/**
* @var array
*/
private $settings = array();
/**
* Settings constructor.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
*/
public function __construct() {
$this->set_default_settings();
$this->set_user_settings();
if ( ! $this->set_settings() ) {
$this->__destruct();
return false;
}
return true;
}
/**
* Sets the default plugin settings in the event that the user hasn\'t configured the plugin.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
* @return bool
*/
private function set_default_settings() {
$default_settings = array();
# General Settings
$default_settings[\'general\'] = array(
\'posts_per_page\' => 10,
\'description\' => \'Example Description\'
);
# Display Settings
$default_settings[\'display\'] = array(
\'icons\' => 0,
\'descriptions\' => 1
);
$this->default_settings = $default_settings;
return true;
}
/**
* Sets plugin settings configured by the user.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
* @return bool
*/
private function set_user_settings() {
$user_settings = array();
$user_settings[\'general\'] = get_option( \'mbe-general-settings\' ); // General Settings
$user_settings[\'display\'] = get_option( \'mbe-display-settings\' ); // Display Settings
$this->user_settings = array_filter( $user_settings );
return true;
}
/**
* Merges default settings with user configured settings, producing final settings to be used by the plugin.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
* @return bool
*/
private function set_settings() {
$default_settings = $this->get_default_settings();
if ( $user_settings = $this->get_user_settings() ) {
$settings[\'general\'] = wp_parse_args( $user_settings[\'general\'], $default_settings[\'general\'] );
$settings[\'display\'] = wp_parse_args( $user_settings[\'display\'], $default_settings[\'display\'] );
} else {
$settings = $default_settings;
}
$this->settings = $settings;
# General Settings
if ( isset( $settings[\'general\'] ) ) {
$this->general = $settings[\'general\'];
}
# Display Settings
if ( isset( $settings[\'display\'] ) ) {
$this->display = $settings[\'display\'];
}
return true;
}
/**
* Retrieve default plugin settings.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
* @return array
*/
public function get_default_settings() {
return $this->default_settings;
}
/**
* Retrieve plugin settings configured by the user.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
* @return array
*/
public function get_user_settings() {
return $this->user_settings;
}
/**
* Retrieve all plugin settings or specific plugin settings.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
* @param String|null $group
*
* @return array|bool
*/
public function get_settings( String $group = null ) {
if ( ! is_null( $group ) ) {
if ( $group == \'default\' ) {
return $this->get_default_settings();
} else {
$group = str_replace( \'-\', \'_\', $group );
if ( ! property_exists( $this, $group ) ) {
return false;
}
return $this->{$group};
}
}
return $this->settings;
}
/**
* Destroys the object in the event of failure.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
*/
public function __destruct() {
$this->general = null;
$this->display = null;
unset( $this->default_settings );
unset( $this->user_settings );
}
}
/**
* Wrapper function to retrieve Plugin Settings from Settings Class.
*
* @author Michael Ecklund
* @author_url https://www.michaelbrentecklund.com
*
* @param String|null $group
*
* @return array|bool
*/
function mbe_get_settings( String $group = null ) {
$settings = new MBE_Settings();
return $settings->get_settings( $group );
}
?>
要对此进行自定义,可以根据需要设置每个属性。将属性视为“组名”或“节名”。然后相应调整:
set_default_settings();
,
set_user_settings();
, 和
set_settings();
.
Note: 此类仅处理检索插件设置。此类不处理插件设置的更新。