admin管理员组文章数量:1122846
Trying to save some plugin data to the wpdb using the update_option
method. This code works fine for my single value ad_url
, but fails for my array exclude_posts
. The POST data in Chrome dev panel shows exclude_posts[]
getting a value of 5, but nothing is going into the wpdb.
options.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');
}
}
Trying to save some plugin data to the wpdb using the update_option
method. This code works fine for my single value ad_url
, but fails for my array exclude_posts
. The POST data in Chrome dev panel shows exclude_posts[]
getting a value of 5, but nothing is going into the wpdb.
options.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');
}
}
Share
Improve this question
asked Sep 16, 2018 at 19:15
user255406user255406
1112 bronze badges
1 Answer
Reset to default 0If $_POST['exclude_posts']
is an array but you need to store it as a string, you can always use a function like serialize()
and unserialize()
to turn it into a string and vice versa.
http://php.net/manual/en/function.serialize.php
本文标签: optionssave multiselect input using updateoption for plugin
版权声明:本文标题:options - save multi-select input using update_option for plugin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302546a1931574.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论