我为wordpress制作了一个插件,添加了两个简单的文本字段选项。这些选项已成功保存在插件选项页面中,但不起作用,它不会输出已保存的选项。
代码如下:
add_action( \'admin_menu\', \'viptips_add_admin_menu\' );
add_action( \'admin_init\', \'viptips_settings_init\' );
function viptips_add_admin_menu( ) {
$icon = \'dashicons-editor-table\';
add_menu_page( \'VIP Tips\', \'VIP Tips\', \'manage_options\', \'vip_tips\', \'viptips_options_page\', $icon );
}
function viptips_settings_init( ) {
register_setting( \'pluginPage\', \'viptips_settings\' );
add_settings_section(
\'viptips_pluginPage_section\',
__( \'Main plugin options\', \'viptips\' ),
\'viptips_settings_section_callback\',
\'pluginPage\'
);
add_settings_field(
\'viptips_category_name\',
__( \'Category name:\', \'viptips\' ),
\'viptips_category_name_render\',
\'pluginPage\',
\'viptips_pluginPage_section\'
);
add_settings_field(
\'viptips_postperpage\',
__( \'Number of posts:\', \'viptips\' ),
\'viptips_postperpage_render\',
\'pluginPage\',
\'viptips_pluginPage_section\'
);
}
function viptips_category_name_render( ) {
$options = get_option( \'viptips_settings\' );
?>
<input type=\'text\' name=\'viptips_settings[viptips_category_name]\' value=\'<?php echo $options[\'viptips_category_name\']; ?>\'>
<p class="description" id="tagline-description">Name of the category that contains predictions</p>
<?php
}
function viptips_postperpage_render( ) {
$options = get_option( \'viptips_settings\' );
?>
<input type=\'number\' name=\'viptips_settings[viptips_postperpage]\' value=\'<?php echo $options[\'viptips_postperpage\']; ?>\'>
<p class="description" id="tagline-description">Number of posts to be displayed in table.</p>
<?php
}
function viptips_settings_section_callback( ) {
echo __( \'Don\\\'t forget to use the <strong>[tabscat]</strong> shortcode, in the page you want the tips table to be displayed!*\', \'viptips\' );
}
function viptips_options_page( ) {
?>
<form action=\'options.php\' method=\'post\'>
<h1>VIP Tips</h1>
<?php
settings_fields( \'pluginPage\' );
do_settings_sections( \'pluginPage\' );
submit_button();
?>
</form>
<?php
}
要输出保存的选项,我使用:
$category_name = get_option(\'viptips_category_name\');
$postperpage = get_option(\'viptips_postperpage\');
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => $category_name,
\'post_per_page\' => $postperpage
);
我哪里出错了?
最合适的回答,由SO网友:Sally CJ 整理而成
Basically, you\'re not using the correct option name.
在您的
viptips_settings_init()
函数,则使用名称
viptips_settings
当
registering the setting:
// The syntax is: register_setting( \'<settings group name>\', \'<database option name>\' )
register_setting( \'pluginPage\', \'viptips_settings\' );
WordPress会用这个
viptips_settings
保存选项时(
i.e. update_option( \'viptips_settings\', \'<value>\' )
) 因此,在从数据库获取选项时,无论该选项是一个值数组还是一个值,都应该使用相同的名称:
具有值数组的输入:
<input type=\'text\' name=\'viptips_settings[viptips_category_name]\' ...>
<input type=\'number\' name=\'viptips_settings[viptips_postperpage]\' ...>
具有单个值的输入:
<input type=\'text\' name=\'viptips_settings\' ...>
例如,不使用
$category_name
和
$postperpage
变量,可以执行以下操作(但请确保实际设置了数组键/项):
$options = (array) get_option( \'viptips_settings\' );
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => $options[\'viptips_category_name\'],
\'post_per_page\' => $options[\'viptips_postperpage\']
);
如果您的代码中包含以下内容:
PHP
// Here we\'ve got two different database option names.
register_setting( \'pluginPage\', \'viptips_category_name\' );
register_setting( \'pluginPage\', \'viptips_postperpage\' );