rwmb_meta_boxes filter

Support General rwmb_meta_boxes filter

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #4974
    woorisewoorise
    Participant

    Hello,

    I use something like the following code inside the rwmb_meta_boxes filter for a custom post type.

    Is there any recommended way to make it run only on current custom post type. For example it also runs on post's edit screen and I get a PHP notice "Undefined variable: content"

    if ( get_post_status( $post_id ) != 'publish' ) {
    	$content = 'Not Published';
    } else {
    	$content = 'Published';
    }
    #4984
    Anh TranAnh Tran
    Keymaster

    Hi,

    How do you get the $post_id and how do you use the code above inside your code to register meta boxes?

    #4988
    woorisewoorise
    Participant

    The code is something like this:

    add_filter( 'rwmb_meta_boxes', 'test' );
    function test( $meta_boxes ) {
    	
    	// Get current post ID
    	$post_id = false;
    	if ( isset( $_GET['post'] ) ) {
    		$post_id = intval( $_GET['post'] );
    	}
    	elseif ( isset( $_POST['post_ID'] ) ) {
    		$post_id = intval( $_POST['post_ID'] );
    	}
    	
        if ( get_post_status( $post_id ) != 'publish' ) {
        	$content = 'Not Published';
        } else {
        	$content = 'Published';
        }
        
        $meta_boxes[] = array(
            'title'      => esc_html__( 'Test', 'test' ),
    		'post_types' => 'testtype',
    		'context'    => 'side',
    		'priority'   => 'low',
            'fields'     => array(
    			array(
    				'id'   => "_test",
    				'type' => 'custom_html',
    				'std'  => $content,
    			),
    		)
    	);
    	
    	return $meta_boxes;
    }
    #5038
    Anh TranAnh Tran
    Keymaster

    I've tested your code and it works just fine: http://prntscr.com/e6t5gi

    Did you add some conditions before assigning value to $content? The error says "Undefined variable: content", so it must be not initialized somewhere.

    #5046
    woorisewoorise
    Participant

    I know that is working fine. My issue is that I am getting an "Undefined variable: content" notice by Query monitor on WordPress posts.

    My question is if there is any recommended way to make it run only on current custom post type? For example it also runs on post’s edit screen that's why I am getting the PHP notice. I am working on a multisite installation and the code is placed in a Plugin.

    Thank you.

    #5050
    Anh TranAnh Tran
    Keymaster

    Can you clarify the "current custom post type"? What is the "current" here?

    From your code above, the variable is always set by the if-else statement, so maybe the error is for somewhere else.

    If you want to run the code only in the edit screen, you can add condition for the meta boxes, like is_admin(). But in that case the rwmb_meta function won't work properly in the frontend. See this for more details.

    #5062
    woorisewoorise
    Participant

    The current is the one that is defined in the rwmb_meta_boxes filter: 'post_types' => 'testtype'

    I need to load the $content variable only on the testtype and not on the default post type.

    #5072
    Anh TranAnh Tran
    Keymaster

    A simple solution is creating 2 meta boxes: 1 for your testtype where you use $content, 1 for the other post type.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.