WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 16 months ago

#24457 closed defect (bug) (worksforme)

add_filter('gettext' ignores get_post_type after deleting a featured image

Reported by: vmodha Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

Steps to reproduce:

  1. register a custom post type using 'register_post_type', in the example my post type is 'gorilla_residents'.
  1. use the following code to filter the 'Set featured image' text for a specific post_type:
function change_featuredimage_txt($translated) {
    global $post;

    if (get_post_type($post) == 'gorilla_residents') {
        $translated = str_ireplace('Set featured image', 'Set Gorilla Avatar', $translated);
        $translated = str_ireplace('Featured Image', 'Gorilla Avatar', $translated);
        $translated = str_ireplace('Remove featured image', 'Remove Gorilla Avatar', $translated);

    }

    return $translated;
}
add_filter('gettext', 'change_featuredimage_txt');
  1. In the admin, create a new instance of the post type, e.g. a Resident.
  1. Asign a featured image. NOTE: the correct filtered text is displayed.
  1. Remove/Delete the featured image.

NOTE: the incorrect featured image text is displayed.

It looks as though the add_filter('gettext' ... function in my functions.php is being ignored after the ajax delete featured image function is called.

Change History (4)

comment:1 @johnbillion2 years ago

I've rolled my own function for determining the current post type when filtering this text. See here.

I'm not sure if this is a bug on its own. The global $post object isn't always populated (which is why your code doesn't always work) but it doesn't need to be.

Related: #19257

comment:2 @vmodha2 years ago

Thanks Johnbillion, your method resolves the issue. However I do still believe this is a bug as the functionality behaves differently when loaded via ajax, it should always resolve the same.

comment:3 @helen2 years ago

In this instance, it would be better and more appropriate to use the admin_post_thumbnail_html filter, which receives the $post_id for context, and to remove the metabox and re-add it with the title you want. Translation filters are a little bit hacky for this purpose.

function myprefix_add_meta_boxes() {
	// Relabel featured image meta box
	remove_meta_box( 'postimagediv', 'gorilla_residents', 'side' );
	add_meta_box( 'postimagediv', 'Gorilla Avatar', 'post_thumbnail_meta_box', 'gorilla_residents', 'side', 'default' );
}
add_action( 'add_meta_boxes', 'myprefix_add_meta_boxes' );

function myprefix_admin_post_thumbnail_html( $output, $post_id ) {
	if ( 'gorilla_residents' === get_post_type( $post_id ) ) {
		$output = str_replace( 'Set featured image', 'Set Gorilla Avatar', $output );
		$output = str_replace( 'Remove featured image', 'Remove Gorilla Avatar', $output );
	}

	return $output;
}
add_filter( 'admin_post_thumbnail_html', 'myprefix_admin_post_thumbnail_html', 10, 2 );

comment:4 @nacin16 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.