尝试使用update_option
方法此代码适用于我的单个值ad_url
, 但我的阵列失败exclude_posts
. Chrome开发面板中的POST数据显示exclude_posts[]
获取值5,但wpdb中没有任何内容。
选项。php
<form method="post" action="options.php">
<?php
settings_fields(\'insert-video-ad-settings\');
do_settings_sections(\'insert-video-ad-settings\');
?>
<input id=adurl type="textarea" name="ad_url" autocomplete="off" value="<?php
echo get_option(\'ad_url\');
?>"/>
<?php
if(isset($_POST[\'exclude_posts\'])) {
$exclude_posts = $_POST[\'exclude_posts\'];
}
if( isset( $exclude_posts ) )
{
update_option( \'exclude_posts\', $exclude_posts );
}
else
{
delete_option( \'exclude_posts\' );
}
?>
<select id="exclude_posts" name="exclude_posts[]" multiple="multiple">
<?php
$postslist = get_posts(array(
\'order\' => \'ASC\',
\'orderby\' => \'date\'
));
$exclude = get_option(\'exclude_posts\');
If(!$exclude){
$exclude=array();
}
if ($postslist) {
global $post;
foreach ($postslist as $post)
{
?>
<option value="<?php echo $post->ID; ?>" <?php echo ( in_array( $post->ID, $exclude ) ? \'selected\' : \'\' ); ?> >
<?php echo $post->post_title; ?>
</option>
<?php
}
}
?>
</select>
myplugin。php
add_action(\'admin_init\', \'update_ad_url\');
if (!function_exists("update_ad_url")) {
function update_ad_url()
{
register_setting(\'insert-video-ad-settings\', \'ad_url\');
}
}
add_action(\'admin_init\', \'update_exclude_posts\');
if (!function_exists("update_exclude_posts")) {
function update_exclude_posts()
{
register_setting(\'insert-video-ad-settings\', \'exclude_posts\');
}
}