• Resolved rose18

    (@rose18)


    Hi,

    I would like to add instruction text to the Featured Image Box in the block editor, and I could do this with this code below:

    function change_post_type_labels() {
        $post_object = get_post_type_object( 'post' );
    
        if ( ! $post_object ) {
            return false;
    	}
    	
        $post_object->labels->set_featured_image = 'Set featured image (540px x 350px)';
    	
        return true;
    }
    
    add_action( 'wp_loaded', 'change_post_type_labels', 20 );

    I want each custom post type featured image box to have different instructions. I am having trouble with the if else statement, and it seems get_post_type() doesn’t work.
    This is what I have below and it’s not working:

    function change_post_type_labels() {
    
    		if('post' == get_post_type()){
    			$post =  'post';
    			$text = 'Set featured image (540px x 350px)';
    
    		}elseif( 'news' == get_post_type()){
    			$post = 'news';
    			$text = 'Set featured image (600px x 300px)';
    		}
    
    		$post_object = get_post_type_object( $post);
    
    		if ( ! $post_object ) {
    		   return false;
    		}
    
    		$post_object->labels->set_featured_image =  $text;
    		return true;
    }
    
    add_action( 'wp_loaded', 'change_post_type_labels', 20 );

    What is the issue?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Mario Santos

    (@santosguillamot)

    Hi @rose18!

    I believe that the second snippet is not working because the global $post, used by get_post_type() function, hasn’t been initialized yet in the hook you are using, wp_loaded.

    The good news is that, if I am not mistaken, you don’t need to do that check. You could directly modify the global $wp_post_types variable using the admin_init hook. Something like this should work:

    
    function change_post_type_labels()
    {
        global $wp_post_types;
    
        $wp_post_types['post']->labels->set_featured_image = "Set featured image (540px x 350px)";
        $wp_post_types['news']->labels->set_featured_image = "Set featured image (600px x 350px)";
    }
    
    add_action('admin_init', 'change_post_type_labels');
    

    Of course, test it yourself before implementing it on your site. Hope that helps!

    Thread Starter rose18

    (@rose18)

    Thank you @santosguillamot ! The code that you provided works ??

    Thanks Again!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Block Editor: Add different Instruction Text To Featured Image Box based on CPT’ is closed to new replies.