Skip to content

Instantly share code, notes, and snippets.

@spencejs
Created March 22, 2013 03:35
Show Gist options
  • Save spencejs/5218759 to your computer and use it in GitHub Desktop.
Save spencejs/5218759 to your computer and use it in GitHub Desktop.
Add Instruction Text To Featured Image Box In Wordpress Admin
@anjanphukan
Copy link

anjanphukan commented Jun 1, 2018

How to show this only for specific post type? I tried this:

add_filter( 'admin_post_thumbnail_html', 'add_featured_image_html');

function add_featured_image_html( $html ) {
$pt = get_current_screen()->post_type;
if ( $pt != 'post') return;
return $html .= "This is some sample text that can be displayed within the feature image box.";
}

but it hides the "Set featured image" link from the other post types.

@hmbashar
Copy link

How to show this only for specific post type? I tried this:

add_filter( 'admin_post_thumbnail_html', 'add_featured_image_html');
function add_featured_image_html( $html ) {
$pt = get_current_screen()->post_type;
if ( $pt != 'post') return;
return $html .= "This is some sample text that can be displayed within the feature image box.";
}

but it hides the "Set featured image" link from the other post types.

You can try this way, It's working for me.

//Add Instructions to Featured Image Box	
   
function ration_add_featured_image_html( $html ) {
   
   if(get_post_type() === 'our-experts') {
        $html .= '<p>Recommended dimension, 540x350.</p>';
   }
   return $html;
  

}
add_filter( 'admin_post_thumbnail_html', 'ration_add_featured_image_html');

@ivcreative
Copy link

This works fine but not on the Gutenberg pages, will be back if I ever find a solution for it

@jezremy
Copy link

jezremy commented Jul 21, 2020

You can do this in Gutenberg and the Classic Editor by amending the labels for the post type = post (or a custom post type):

//Add Instructions to Featured Image Box

function change_post_type_labels() {
    $post_object = get_post_type_object( 'our-experts' );

    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 );

@r-interactive
Copy link

jezremy

Hi jezremy ,

the code works, but how can I do this for multiple Custom post types. I want each custom post type featured image box to have different instructions. I am having trouble coding the if else statement, seems that get_post_type() doesn't work.

This is what I have:

`function change_post_type_labels() {

		if('news' == get_post_type()){
			$post =  'news';
			$text = 'Set featured image (540px x 350px)';

		}elseif( 'page' == get_post_type()){
			$post = 'page';
			$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 );`

Would you know what the issue is?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment