当您使用Settins API时,这就是为什么您不需要使用update_option
或add_option
功能。设置API将自行处理该问题。
这就是为什么您的代码不起作用的原因。
1.
add_settings_section(
\'wpa_ad_insert_main\',
\'Ad Insertion Settings\',
\'wpa_ad_insert_section_text\',
\'wpa_ad_insert\'
);
第三个参数是回调函数。因为,你不需要这个函数,所以只需添加
__return false
作为回调。然后,此函数的代码如下所示:
add_settings_section(
\'wpa_ad_insert_main\',
\'Ad Insertion Settings\',
\'__return_false\',
\'wpa_ad_insert\'
);
2.
register_setting(
\'wpa_ad_insert_options\',
\'wpa_ad_insert_options\',
\'wpa_ad_insert_validate_options\'
);
请看上面的代码。由于第二个参数是选项名,所以当您要从数据库检索选项时,必须使用此选项名。您没有调用该选项名称,而是调用了下面列出的两个选项名称:
$options = get_option(\'wpa_ad_insert_paragraph\');
$options = get_option(\'wpa_ad_insert_adcode\');
因为您在
register_setting
函数,这就是您未能将选项检索到数据库中的原因。你应该打电话
get_option
使用您注册的选项名称的函数
register_setting
作用这就像:
$options = get_option(\'wpa_ad_insert_options\');
3.但数据仍然没有保存到数据库中,对吗?因为你曾经
plugin_options
作为表单输入和文本区域的名称。因为您使用的是wordpress设置API,所以您应该使用与注册时相同的名称
option_name
在
register_setting
作用
因此,您需要修复一些事情才能使此代码可用。我修改了几行代码,这很有效。替换您的register_setting
此功能:
register_setting(
\'wpa_ad_insert_options\',
\'plugin_options\',
\'wpa_ad_insert_validate_options\'
);
替换您的
add_settings_section
此功能:
add_settings_section(
\'wpa_ad_insert_main\',
\'Ad Insertion Settings\',
\'__return_false\',
\'wpa_ad_insert\'
);
全部替换
get_option
函数:$options=wpa\\u options(\'plugin\\u options\');
最后添加以下新代码:
function wpa_options() {
$default = array(
\'wpa_ad_insert_paragraph\' => 1,
\'wpa_ad_insert_adcode\' => \'\'
);
return get_option(\'plugin_options\', $default);
}
Put it all together: // Add a menu for our option page
add_action(\'admin_menu\', \'wpa_ad_insert_add_page\');
function wpa_ad_insert_add_page() {
add_options_page( \'Ad Insertion\', \'Ad Insertion\', \'manage_options\', \'wpa_ad_insert\', \'wpa_ad_insert_option_page\' );
}
// Draw the option page
function wpa_ad_insert_option_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Ad Insertion</h2>
<form action="options.php" method="post">
<?php settings_fields(\'wpa_ad_insert_options\'); ?>
<?php do_settings_sections(\'wpa_ad_insert\'); ?>
<input name="Submit" type="submit" value="Save Changes" />
</form>
</div>
<?php
}
// Register and define the settings
add_action(\'admin_init\', \'wpa_ad_insert_admin_init\');
function wpa_ad_insert_admin_init(){
register_setting(
\'wpa_ad_insert_options\',
\'plugin_options\',
\'wpa_ad_insert_validate_options\'
);
add_settings_section(
\'wpa_ad_insert_main\',
\'Ad Insertion Settings\',
// \'wpa_ad_insert_section_text\',
\'__return_false\',
\'wpa_ad_insert\'
);
add_settings_field(
\'wpa_ad_insert_paragraph\',
\'Ad will appear after paragraph number:\',
\'wpa_ad_insert_paragraph_setting\',
\'wpa_ad_insert\',
\'wpa_ad_insert_main\'
);
add_settings_field(
\'wpa_ad_insert_adcode\',
\'Ad code:\',
\'wpa_ad_insert_adcode_setting\',
\'wpa_ad_insert\',
\'wpa_ad_insert_main\'
);
}
register_setting(
\'wpa_ad_insert_options\',
\'plugin_options\',
\'wpa_ad_insert_validate_options\'
);
function wpa_ad_insert_paragraph_setting() {
$options = wpa_options(\'plugin_options\');
$items = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20");
echo "<select id=\'wpa_ad_insert_paragraph\' name=\'plugin_options[wpa_ad_insert_paragraph]\'>";
foreach($items as $item) {
$selected = ($options[\'wpa_ad_insert_paragraph\']==$item) ? \'selected="selected"\' : \'\';
echo "<option value=\'$item\' $selected>$item</option>";
}
echo "</select>";
}
function wpa_ad_insert_adcode_setting() {
$options = wpa_options(\'plugin_options\');
echo "<textarea id=\'wpa_ad_insert_adcode\' name=\'plugin_options[wpa_ad_insert_adcode]\' rows=\'7\' cols=\'50\' type=\'textarea\'>{$options[\'wpa_ad_insert_adcode\']}</textarea>";
}
function wpa_insert_ad($content) {
$paragraphAfter = get_option(\'wpa_ad_insert_paragraph\');
$ad = get_option(\'wpa_ad_insert_adcode\');
if( is_single() && is_main_query() ) {
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++ ) {
if ($i == $paragraphAfter)
echo $content[$i] . "</p>";
}
}
return $content;
}
add_filter(\'the_content\', \'wpa_insert_ad\');
function wpa_options() {
$default = array(
\'wpa_ad_insert_paragraph\' => 1,
\'wpa_ad_insert_adcode\' => \'\'
);
return get_option(\'plugin_options\', $default);
}