[wp-trac] [WordPress Trac] #40621: Content of multiple metaboxes into one custom metabox - nonce error

WordPress Trac noreply at wordpress.org
Mon May 1 11:53:20 UTC 2017


#40621: Content of multiple metaboxes into one custom metabox - nonce error
--------------------------------+-----------------------------
 Reporter:  mireillesan         |      Owner:
     Type:  enhancement         |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Options, Meta APIs  |    Version:  4.7.4
 Severity:  normal              |   Keywords:
  Focuses:                      |
--------------------------------+-----------------------------
 WP is full of multiple standalone metaboxes and I want to put all these
 metaboxes into one metabox. So far this works good with WP default
 metaboxes, but not with external metaboxes from plugins.

 My code is like this:

 Full Source:
 https://developer.wordpress.org/reference/functions/add_meta_box


 {{{
 class someClass {

     public function __construct() {
         ...
         add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
         add_action( 'save_post',      array( $this, 'save'         ) );
     }

     public function add_meta_box( $post_type ) {

         $post_types = array( 'recipes' );

         if ( in_array( $post_type, $post_types ) ) {
             add_meta_box( 'some_meta_box_name', __( 'Meta Box Headline',
 'textdomain' ), array( $this, 'render_meta_box_content' ), $post_type,
 'advanced',                'high'            );
         }
     }


     public function save( $post_id ) {

         if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) ) {
             return $post_id;
         }

         $nonce = $_POST['myplugin_inner_custom_box_nonce'];

         if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) {
             return $post_id;
         }
         ...
     }



     public function render_meta_box_content( $post ) {

        wp_nonce_field( 'myplugin_inner_custom_box',
 'myplugin_inner_custom_box_nonce' );  ?>

         <ul class="tabbed-menu">
             <li><a href="#">Recipes Categories</a></li>
             <li><a href="#">Plugin link</a></li>
             <li><a href="#">Another plugin link</a></li>
         </ul>

         <ul class="tabbed-content">
             <li>
                 <?php $box['args']['taxonomy'] = 'recipes_category';
                 $post_categories_meta_box( $post, $box ); ?>
             </li>
             <li>Plugin metabox</li>
             <li>Another plugin metabox</li>
         </ul>

         <?php
     }
 }}}


 When I look into a plugin's code for metabox


 {{{
 add_meta_box( 'woo_sl_box', 'Licensing', 'woo_license_meta_box',
 'recipes', 'normal', 'core' );
 }}}


 I was hoping that I could use the plugin's function in my own metabox
 after using unset( $wp_meta_boxes[]...) first, like this


 {{{
 <li><?php woo_license_meta_box(); ?></li>
 }}}


 However, saving doesn't work due to the different
 {{{
 nonce
 }}}
  that's being used between the plugin and my own metabox. While when using
 WP default metaboxes, e.g.:

 {{{
 $box['args']['taxonomy'] = 'recipes_category';
 $post_categories_meta_box( $post, $box );
 }}}

 there's no nonce error when clicking on the ''update'' button.

 Can anyone explain why? And is there a better way to handle what I'm
 trying to do? (e.g. hooks/filters that are already available)

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40621>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list