• Resolved gorirrajoe

    (@gorirrajoe)


    I’m trying to modify this filter:
    https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Adding-your-own-show_on-filters#example-page-template-show_on-filter

    it doesn’t seem to prevent the box from showing:

    functions.php

    function be_metabox_exclude_on_template( $display, $meta_box ) {

    if( 'exclude_template' !== $meta_box['show_on']['key'] )
    return $display;

    // Get the current ID
    if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
    elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
    if( !isset( $post_id ) ) return false;

    $template_name = get_page_template_slug( $post_id );
    $template_name = substr($template_name, 0, -4);

    // If value isn't an array, turn it into one
    $meta_box['show_on']['value'] = !is_array( $meta_box['show_on']['value'] ) ? array( $meta_box['show_on']['value'] ) : $meta_box['show_on']['value'];

    // See if there's a match
    if( in_array( $template_name, $meta_box['show_on']['value'] ) ) {
    return false;
    } else {
    return true;
    }
    }
    add_filter( 'cmb_show_on', 'be_metabox_exclude_on_template', 10, 2 );

    initiating the cmb2 metabox:


    $cmb = new_cmb2_box( array(
    'id' => 'perpage',
    'title' => __( 'Per Page Specifics', 'rnr3' ),
    'object_types' => array( 'page' ), // Post type
    'context' => 'side',
    'show_on' => array( 'key' => 'exclude_template', 'value' => 'bling.php' ),
    'priority' => 'high',
    'show_names' => true // Show field names on the left
    ) );

    i don’t want it to show up a page that uses the “bling.php” template. what am i doing wrong?

    https://ww.wp.xz.cn/plugins/cmb2/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter gorirrajoe

    (@gorirrajoe)

    to clean up:

    functions.php

    function be_metabox_exclude_on_template( $display, $meta_box ) {
    
    if( 'exclude_template' !== $meta_box['show_on']['key'] )
    return $display;
    
    // Get the current ID
    if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
    elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
    if( !isset( $post_id ) ) return false;
    
    $template_name = get_page_template_slug( $post_id );
    $template_name = substr($template_name, 0, -4);
    
    // If value isn't an array, turn it into one
    $meta_box['show_on']['value'] = !is_array( $meta_box['show_on']['value'] ) ? array( $meta_box['show_on']['value'] ) : $meta_box['show_on']['value'];
    
    // See if there's a match
    if( in_array( $template_name, $meta_box['show_on']['value'] ) ) {
    return false;
    } else {
    return true;
    }
    }
    add_filter( 'cmb_show_on', 'be_metabox_exclude_on_template', 10, 2 );

    init:

    $cmb = new_cmb2_box( array(
    'id' => 'perpage',
    'title' => __( 'Per Page Specifics', 'rnr3' ),
    'object_types' => array( 'page' ), // Post type
    'context' => 'side',
    'show_on' => array( 'key' => 'exclude_template', 'value' => 'bling.php' ),
    'priority' => 'high',
    'show_names' => true // Show field names on the left
    ) );
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Try what you see at https://github.com/WebDevStudios/CMB2/wiki/Display-Options#limit-to-specific-page-templates if you haven’t seen it already.

    While there are similar things between the two, if you’re using CMB2, it’d be best to stick to the CMB2 wiki instead of the old CMB1 wiki.

    Thread Starter gorirrajoe

    (@gorirrajoe)

    i’ve used that code and it works. i was just looking for an “exclude from template” feature

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If I understand the code correctly, the following would show it on any page that has this template chosen:

    'show_on'      => array( 'key' => 'page-template', 'value' => 'template-contact.php' ),

    From the sounds of it, you want to show it on all pages that do NOT have bling.php chosen for the template?
    Kind of looks that way from the upper snippet in your second post, but since you’re using CMB2, and not the original, the hook in this needs to change to cmb2_show_on:

    add_filter( 'cmb_show_on', 'be_metabox_exclude_on_template', 10, 2 );

    Pretty much all of the hooks got renamed to “cmb2_*” which adds to the notion of not relying on the CMB1 wiki with regards to copy/pasting code.

    Thread Starter gorirrajoe

    (@gorirrajoe)

    HUZZAH! that worked…

    init – updated (removed .php from template filename due to substr in function:

    $cmb = new_cmb2_box( array(
          'id'            => 'perpage',
          'title'         => __( 'Per Page Specifics', 'rnr3' ),
          'object_types'  => array( 'page' ), // Post type
          'context'       => 'side',
          'priority'      => 'high',
          'show_on'       => array( 'key' => 'exclude_template', 'value' => array( 'bling', 'series-home' )),
          'show_names'    => true // Show field names on the left
        ) );

    functions.php – updated:

    function be_metabox_exclude_on_template( $display, $meta_box ) {
    
      if( 'exclude_template' !== $meta_box['show_on']['key'] )
        return $display;
    
      // Get the current ID
      if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
      elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
      if( !isset( $post_id ) ) return false;
    
      $template_name = get_page_template_slug( $post_id );
      $template_name = substr($template_name, 0, -4);
    
      // If value isn't an array, turn it into one
      $meta_box['show_on']['value'] = !is_array( $meta_box['show_on']['value'] ) ? array( $meta_box['show_on']['value'] ) : $meta_box['show_on']['value'];
    
      // See if there's a match
      if( in_array( $template_name, $meta_box['show_on']['value'] ) ) {
        return false;
      } else {
        return true;
      }
    }
    add_filter( 'cmb2_show_on', 'be_metabox_exclude_on_template', 10, 2 );

    THANKS!!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘show_on – exclude on template’ is closed to new replies.