Conditional Logic Custom Callback not working for me

Support MB Tabs Conditional Logic Custom Callback not working for me

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #6107
    bdthemesbdthemes
    Participant

    Hello,

    I used this logic: 'visible' => ['orphan_toolbar_check()', true]

    Callback Function is:

    function orphan_toolbar_check() {
    if ( get_theme_mod('orphan_toolbar') ) {
    return true;
    } else {
    return false;
    }
    }

    but it's not working for me, can you help about it?

    it's showing this error in console: http://prntscr.com/flv12i (it's showing orphan_toolbar_check is not defined but i already define it)

    NOTE: i am using Metabox: 4.10.1 and conditional logic: 1.4.1

    #6109
    Tan NguyenTan Nguyen
    Participant

    Dear bdthemes,

    Conditional Logic works with Javascript callback only, it doesn't works with PHP, however, with PHP, you can check if meet a condition and then register meta box or field, like so:

    
    if (get_theme_mod('orphan_toolbar')) {
        $meta_boxes = [
            ...
        ];
    }
    
    #6113
    bdthemesbdthemes
    Participant

    Hello Nguyen, Thanks for your reply. You told whole metabox in condition but I want to show/hide one field for depend on one customizer setting. So how can do it?

    For example:

    I want to show/hide toolbar option depend on this customizer field:

    get_theme_mod( 'orphan_toolbar'); // This return true/false
    

    I want visible this metabox field when i get true from get_theme_mod( 'orphan_toolbar'). My field code look like:

    array(
    	'name'	   => 'Toolbar',
    	'id'	   => $prefix . "toolbar", 
    	'type'     => 'checkbox',
    	'desc'     => 'Enable or disable the toolbar for this page.',
    	'tab'      => 'layout',
    	'visible' => [what condition i need to set for it?],
    ),
    
    #6117
    Tan NguyenTan Nguyen
    Participant

    Dear bdthemes,

    Here is an example:

    
    add_filter( 'rwmb_meta_boxes', 'YOURPREFIX_register_meta_boxes' );
    
    function YOURPREFIX_register_meta_boxes( $meta_boxes ) {
        // Define a meta box
        $meta_box = array(
            'title'      => __( 'Media', 'textdomain' ),
            'post_types' => 'post',
            'fields'     => array(
                array(
                    'name' => __( 'URL', 'textdomain' ),
                    'id'   => 'url',
                    'type' => 'text',
                ),
            )
        );
    
        // Add new field when condition meets
        if (get_theme_mod( 'orphan_toolbar')) {
            $meta_box['fields'][] = array(
                'name'     => 'Toolbar',
                'id'       => $prefix . "toolbar", 
                'type'     => 'checkbox',
                'desc'     => 'Enable or disable the toolbar for this page.',
                'tab'      => 'layout',
            ),
        }
    
        // Add the meta box to $meta_boxes array to register
        $meta_boxes[] = $meta_box;
    }
    
    
    #6143
    bdthemesbdthemes
    Participant

    I understand it, Thanks for your help 🙂

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Conditional Logic Custom Callback not working for me’ is closed to new replies.