• Resolved exactprecisions

    (@exactprecisions)


    I am using the documentation and wanted to use the ‘slug’ show_on filter as a example. I call it after I require cmb2 and before the config. It will show the fields on all pages.

    	$cmb_front_page = new_cmb2_box( array(
    		'id'           => $prefix . 'metabox',
    		'title'        => esc_html__( 'Front page metabox', 'cmb2' ),
    		'object_types' => array( 'page' ), // Post type
    		'context'      => 'normal',
    		'priority'     => 'high',
    		'show_names'   => true, // Show field names on the left
    		'show_on'      => array(
    			'slug' => $frontPageID,
    		), // Specific page template to display this metabox
    	) );
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    what value is in $frontPageID ? number? post slug?

    Have you considered trying a quick custom callback for show_on_cb to get a more advanced possible solution?

    Thread Starter exactprecisions

    (@exactprecisions)

    I actually forgot to change $frontPageID when I pasted it here. Can you reference any information on the custom callback for show_on_cb?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    It’s pretty much like most other custom functions you can declare like for hooks, etc.

    More info at https://github.com/CMB2/CMB2/wiki/Adding-your-own-show_on-filters

    For example, you should be able to retrieve the post ID being used and once you have that, just call whatever functions you need to get what info you need. Then return either true or false based on your conditions, to say “yes, show this metabox”

    Thread Starter exactprecisions

    (@exactprecisions)

    I tried using the example here

    https://github.com/CMB2/CMB2/wiki/Adding-your-own-show_on-filters#example-front-page-show_on-filter

    and couldn’t get it to work

    function ed_metabox_include_front_page( $display, $meta_box ) {
    	if ( ! isset( $meta_box['show_on']['key'] ) ) {
    		return $display;
    	}
    
    	if ( 'front-page' !== $meta_box['show_on']['key'] ) {
    		return $display;
    	}
    
    	$post_id = 0;
    
    	// If we're showing it based on ID, get the current ID
    	if ( isset( $_GET['post'] ) ) {
    		$post_id = $_GET['post'];
    	} elseif ( isset( $_POST['post_ID'] ) ) {
    		$post_id = $_POST['post_ID'];
    	}
    
    	if ( ! $post_id ) {
    		return false;
    	}
    
    	// Get ID of page set as front page, 0 if there isn't one
    	$front_page = get_option( 'page_on_front' );
    
    	// there is a front page set and we're on it!
    	return $post_id == $front_page;
    }
    add_filter( 'cmb2_show_on', 'ed_metabox_include_front_page', 10, 2 );
    Thread Starter exactprecisions

    (@exactprecisions)

    Wait! I think I see the difference now.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    i likely confused you with mentioning filters. You don’t implement them via add_filter, but the internals of the function are like a callback if you were, where you can kind of do whatever needed.

    You define the function like you have, remove the add_filter part, and then where you had 'show_on' => array(...) you replace with 'show_on_cb' => 'ed_metabox_include_front_page' so that CMB2 knows what function to try invoking

    Thread Starter exactprecisions

    (@exactprecisions)

    Yes thank you it worked. Thanks a lot!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘custom show_on filter not working’ is closed to new replies.