Why is $_POST empty when saving custom Meta Box?

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' );
        ?
        divRequire 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=everyoneEveryone/option
            option value=subscriberRegistered/option
            option value=selflearnerSelf Learner/option
            option value=basiclearnerBasic Learner/option
            option value=activelearnerActive Learner/option
            option value=advancedlearnerAdvanced 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, [email protected]); //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 );
        
        
    }

Topic post-meta metabox plugin-development Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.