0

I'm creating a Meta Box in my custom Plugin where the Administrator can select which users can view a certain post based on their roles. When I select the role and save the meta box content by clicking on the Update button, nothing gets saved because both $_POST['metabox_nonce'] and $_POST['membership_level'] are empty. Why is the form not sending any data? Thank you so much for your help.

This is my code:

add_action( 'add_meta_boxes', array($this, 'add_italianglot_membership_box' )); 

add_action( 'save_post', array( $this, 'italianglot_membership_save_post_meta_box' ), 10, 2 );

public function add_italianglot_membership_box($post_type) { 
            
            $post_types = get_post_types( array( 'public' => true ), 'names' );
            add_meta_box(
                'italianglot_box_id',                
                'Italianglot Level',                 
                array( $this, 'italianglot_membership_box_html' ),
                $post_type,
                'side',
                'default'
            );
        
    }
    
    public function italianglot_membership_box_html( $post ) { 
        global $post;
        wp_nonce_field( basename( __FILE__ ), 'italianglot_metabox_nonce' );
        ?>
        <div>Require the below membership tier or higher to view this post.<p></div>
        <select name="ig_membership_level" id="ig_membership_level" class="form-input">
            <option value="everyone">Everyone</option>
            <option value="subscriber">Registered</option>
            <option value="selflearner">Self Learner</option>
            <option value="basiclearner">Basic Learner</option>
            <option value="activelearner">Active Learner</option>
            <option value="advancedlearner">Advanced Learner</option>
        </select>
        <?php
    }


    public function italianglot_membership_save_post_meta_box( $post_id, $post ) { 
        
        if ( !isset( $_POST['italianglot_metabox_nonce'] ) || !wp_verify_nonce( $_POST['italianglot_metabox_nonce'], basename( __FILE__ ))  ) {
            $test = $_POST['ig_membership_level'];
            error_log($test, 1, "myemail@gmail.com"); //This is where I test the variable content and it's always empty
            return $post_id;
        }

        $post_type = get_post_type_object( $post->post_type );

        if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) {
            return $post_id;
        }
        
        if(isset( $_POST['ig_membership_level'] ) ) {
            $italianglot_level = $_POST['ig_membership_level'];
        } else {
            $italianglot_level = "everyone";
        }
        
        update_post_meta( $post_id, 'ig_membership_level', $italianglot_level );
        
        
    }
2
  • It isn't empty in my test run. You're testing inside a block which is triggered when nonce verification fails, which isn't the case here. Try moving your error_log outside of that block.
    – Ismail
    May 24, 2022 at 2:40
  • Thank you so much! It worked! Since I kept receiving the error_log email, I assumed the nonce verification kept failing and the variable in the email was always empty. May 24, 2022 at 14:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.