Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在wordpress插件的自定义元框中保存多个复选框值?_Php_Html_Wordpress_Plugins - Fatal编程技术网

Php 如何在wordpress插件的自定义元框中保存多个复选框值?

Php 如何在wordpress插件的自定义元框中保存多个复选框值?,php,html,wordpress,plugins,Php,Html,Wordpress,Plugins,是的,我知道还有一个相关的问题已经得到了回答,但我不明白如何在我的代码中实现这个解决方案。我只是个初学者,帮帮我吧。我想在WordPress插件的自定义metabox中保存多个复选框值。当用户保存或更新帖子时,应保存选中复选框的值 function cd_meta_box_cb($post){ global $post; echo'<b> Select the contributors that have contributed to this post: </b>';

是的,我知道还有一个相关的问题已经得到了回答,但我不明白如何在我的代码中实现这个解决方案。我只是个初学者,帮帮我吧。我想在WordPress插件的自定义metabox中保存多个复选框值。当用户保存或更新帖子时,应保存选中复选框的值

function cd_meta_box_cb($post){
global $post;
echo'<b> Select the contributors that have contributed to this post: </b>';
echo '<br><br>';   
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
global $wpdb;
$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users 
ORDER BY user_nicename");
$i=0;
$n=count($authors);
foreach($authors as $author) {
    echo"<input type='checkbox' id='my_meta_box_check' 
    name='my_meta_box_check'";
    echo"value=";
    the_author_meta('user_nicename', $author->ID);
    echo">";
    echo"<label for='author'.$i>";
    the_author_meta('user_nicename', $author->ID);
    echo"</label>";
    echo "<br />";
  }

 echo"<input type='submit' id='submit_btn' name='submit' value='Submit'>";
  }
//save custom data when our post is saved
 function save_custom_data($post_id) 
 {  
  global $post;
  $contributor=get_post_meta($post->ID,'my_meta_box_check',true);
  if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;     

 if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( 
 $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
if ( isset($_POST['my_meta_box_check']) ) 
{        

    $data=serialize($_POST['my_meta_box_check']);   
    update_post_meta($post_id, 'my_meta_box_check',$data);      
 } 
else {
    delete_post_meta($post_id, 'my_meta_box_check');
 } 
}
add_action( 'save_post', 'save_custom_data' );
function displaymeta()
{

    global $post;
    $m_meta_description = get_post_meta($post->ID, 'my_meta_box_check', 
  true);
    echo 'Meta box value: ' . unserialize($m_meta_description);
}

add_filter( 'the_content', 'displaymeta' );

?>
功能cd\u元盒\u cb($post){
全球$员额;
echo“选择对此帖子做出贡献的贡献者:”;
回音“

”; wp_nonce_字段('my_meta_box_nonce','meta_box_nonce'); 全球$wpdb; $authors=$wpdb->get_results(“从$wpdb->users中选择ID、用户名 按用户(名称)订购; $i=0; $n=计数($s); foreach($authors作为$author的作者){ 回声“; 回声“; 作者元('user\u nicename',$author->ID); 回声“; 回声“
”; } 回声“; } //保存帖子时保存自定义数据 函数save_custom_data($post_id) { 全球$员额; $contributor=get\u post\u meta($post->ID,'my\u meta\u box\u check',true); if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE)返回; 如果(!isset($_POST['meta_box_nonce'))| |!wp_verify_nonce( $\u POST['meta\u box\u nonce'],'my\u meta\u box\u nonce')返回; //如果当前用户无法编辑此帖子,请退出 如果(!current_user_can('edit_post'))返回; 如果(isset($\u POST['my\u meta\u box\u check'])) { $data=serialize($\u POST['my\u meta\u box\u check']); 更新发布元数据($post\u id,'my\u meta\u box\u check',$data); } 否则{ 删除帖子元($post\u id,'my\u meta\u box\u check'); } } 添加操作('save_post','save_custom_data'); 函数displaymeta() { 全球$员额; $m\u meta\u description=get\u post\u meta($post->ID,'my\u meta\u box\u check', 正确的); 回显“元框值:”.unserialize($m_Meta_description); } 添加_过滤器('the_content','displaymeta'); ?>
将复选框字段名称提到数组中,如
my\u meta\u box\u check[]

请使用下面的代码

<input type='checkbox' id='my_meta_box_check' 
name='my_meta_box_check[]'" >

Hi komal当你试图保存元值时会发生什么..Hi Yogesh,当我保存帖子时,它在帖子上只显示一个复选框值(因为过滤器)。即使我检查了多个值,它也只显示一个。它也只在wppost_元表中存储一个值。如何在一个meta_键中存储多个meta值?好的,该解决方案将数据很好地存储在数据库中,但当我尝试回显时,它会显示“Array”。如何显示选中复选框的各个值?好的,我使用了foreach循环,现在它工作得非常好!非常感谢你,约格什!:)干杯你好,我在另一个代码中有另一个查询,你能帮我吗@Yogesh?嗨,KomalR,你确定。我会帮你的,谢谢!请查看此链接