Custom Fields
Re-Envisioned
Support Meta Box Conditional Logic Show A Specific Tab When A Feature Image is Added

  • Creator
    Topic
  • #8096
    Resolved tsquez
    Participant

    HI there,

    I am using meta-box-tab as well as meta-box-conditional-logic extensions. In my theme the user can select where the Featured Image of a post or page is displayed. Here is the code I am added to the theme:

    https://ghostbin.com/paste/daepa

    As you can see the user has two options: they can show the featured image below the title or display it above the title.

    My question is how can I hide the Featured Image Options tab if there is no featured image and show the Featured Image Options tab if a featured image is used?

    Thanks for any help you can provide.

Viewing 5 replies - 11 through 15 (of 15 total)
  • Author
    Replies
  • #8183
    tsquez
    Participant

    Hey there,

    Yes I thought about that before asking the question and I even tried it to see what happens.

    This is what happens:

    1. I move the featured image tab to the last position. I remove the featured image and now the featured image tab is hidden because there is no featured image:

    View post on imgur.com

    2. So I add a featured image and the Featured Image Options tab appears:

    View post on imgur.com

    As you can see this is not the desired effect. Which is why I asked the question in the first place amigo. Trust me I never ask a question on how to do something if I haven’t already tried doing everything possible to make it work 😉

    So, is there a way to maybe set a tab to be the active tab, maybe like this:

    'active' => true,

    Thanks again.

    #8196
    Anh Tran
    Keymaster

    Hi Thomas,

    I’ve been thinking about this, too. It seems that I have to add some more code to target this situation. Let me try to do that.

    #8207
    tsquez
    Participant

    Awesome amigo I look forward to seeing what you come up with.

    I know you’ll knock it out of the ballpark

    #8274
    tsquez
    Participant

    OK so my theme <b>TotalPress</b> was reviewed yesterday. I was excited. However they are using a new plugin for review. You can find it here: https://github.com/WPTRT/theme-sniffer

    You can run checks against different version of PHP, from 5.2 all the way to 7.

    So they ran the theme through this new plugin and it ran into a few errors running it against php 5.2. One of the errors was generated from the code for Meta Box Conditional Logic plugin.

    It’s this code from the library, at the very bottom: https://github.com/wpmetabox/library/blob/master/extensions/conditional-logic/featured-image-with-tabs.php

    Specifically:

    // hide our featured image options tab if there is no featured image being used.
    add_filter( 'rwmb_outside_conditions', function ( $conditions ) {
      $conditions['.rwmb-tab-totalpress_featured_image_options'] = array(
        'visible' => array('_thumbnail_id', '!=', '-1'),
      );
      $conditions['.rwmb-tab-panel-totalpress_featured_image_options'] = array(
        'visible' => array('_thumbnail_id', '!=', '-1'),
      );
      return $conditions;
    } );

    This is what the error states:

    Closures / anonymous functions are not available in PHP 5.2 or earlier

    So I had to remove this from the theme as well as the plugin meta box conditional logic. I really would like to include this in the theme but for now to get the theme approved, I have removed it.

    Any idea what it means…lol – know how I can correct it?

    Any help you can provide is greatly appreciated.

    #8284
    Anh Tran
    Keymaster

    Hi Thomas,

    You can change from closure / anonymous function to this:

    add_filter( 'rwmb_outside_conditions', 'prefix_tab_conditions' );
    function prefix_tab_conditions( $conditions ) {
      $conditions['.rwmb-tab-totalpress_featured_image_options'] = array(
        'visible' => array('_thumbnail_id', '!=', '-1'),
      );
      $conditions['.rwmb-tab-panel-totalpress_featured_image_options'] = array(
        'visible' => array('_thumbnail_id', '!=', '-1'),
      );
      return $conditions;
    } );

    In short, just move the part function... down and add a name for it. Then alter the add_filter function and use the new function name.

    Read more about anonymous function here.

Viewing 5 replies - 11 through 15 (of 15 total)
  • You must be logged in to reply to this topic.