WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#40621 closed enhancement (invalid)

Content of multiple metaboxes into one custom metabox - nonce error

Reported by: mireillesan Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.4
Component: Options, Meta APIs Keywords:
Focuses: Cc:

Description

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)

Change History (2)

#1 @johnbillion
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

This question is better suited to the support forums where you'll be more likely to get an answer.

#2 @mireillesan
4 years ago

@johnbillion: Thanks! Posted in the support forum.
Based on your answer, it seems that my idea is possible afterall.

Note: See TracTickets for help on using tickets.