• I have a CPT that each post can link to an associated User Account. This is done with a dropdown box that lists the user accounts and saves the user ID to the custom meta data field ‘attach_user_ID’

    If the CPT has an associated user ID (user account) attached, it displays the associated information with that user account, email, phone and more.

    This all works fine except that if the post doesn’t have an associated user account, I want to hide the meta box that displays that extended information.

    Been banging my head on this one for a while.

    code is as follows:

    Add custom meta boxes:

    add_action("admin_init", "admin_init");  //Add custom meta boxes and fields to CPT Lodge Members (officers)
    
    function admin_init(){
        add_meta_box("assoc_info", "Associated Information", "assoc_info", "officers", "normal", "low");
    }

    Left out the code that adds the other meta boxes that store the user ID, but this works fine, and I can call the user ID ok from most areas of the code.

    function remove_assoc_info_meta_box() {
    
        global $post;
    
        //Can't see attach_user_ID at this point, displays nothing.
        echo get_post_meta( $post->ID, "attach_user_ID", true);
    
        remove_meta_box('assoc_info', 'officers','normal');
    }
    
    //if ( empty( get_post_meta( $post->ID, 'attach_user_ID' ) )) {
    add_action('admin_menu', 'remove_assoc_info_meta_box');
    //}

    the remove meta box code works, but I can’t check the custom meta data value at this point in the code.

    If I move the add_action(remove meta box) code to a portion of the code where I can check the user ID, the meta box doesn’t hide.

    any help greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I think you need to always add the meta box, but alter what it contains based on the available data. If data is available, show it, otherwise don’t output anything from the callback at all, resulting in an empty div.

    Thread Starter masonify

    (@masonify)

    Thanks for your response bc, make that plural.

    This would leave me with an empty meta box, that still shows the title of the meta box, and the empty box below it which could be confusing for the user why there is nothing in the box.

    I’d like to remove the whole meta box if it’s not applicable.

    I’m assuming this is possible as WP codex has the remove_meta_box() function available.

    Moderator bcworkz

    (@bcworkz)

    Fair point. I’m not clear on why the meta data is not available unless it’s because it’s yet to be created at the time ‘admin_menu’ action fires. If that’s the case, it sounds like the meta box would need to be removed via jQuery instead of PHP. The jQuery would either be triggered by a value passed during ‘wp_enqueue_scripts’ action via wp_localize_script() or by an action on the edit screen itself if that’s appropriate.

    I’m unsure of the best way to get the current post ID in a ‘wp_enqueue_scripts’ callback, it should be in $_SERVER['QUERY_STRING'] at least. Come to think of it, you may be able to remove the meta box from ‘wp_enqueue_scripts’ action. A bit hacky since that is not the action’s intended purpose, but it does fire quite late!

    If all else fails, instead of an empty meta box, how about a form with the fields disabled?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Remove Meta Box if CPT cutom Meta Data exists’ is closed to new replies.