Support » Plugin: Extend Media Upload » Use title of post to update the title field of featured image

  • Resolved wp-fred

    (@wp-fred-1)


    Hello,

    Wordpress automatically adds the file-name in the title of the featured image, but this is very often very ugly. So I would like to use the Post_title instead.

    Would it be possible to enhance the ‘Extend Media Upload plugin’ with this functionality? So the plugin automatically update the title-field of the featured image based on the post_title where the featured image is attached to?

    Would be very helpfull.

    ps. I already ordered the extend-media-upload-add-on-exif which already updates the EXIF information to the caption-field. Which is very nice.

    wp-fred

    • This topic was modified 2 years ago by wp-fred.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Katsushi Kawamori

    (@katsushi-kawamori)

    How about adding a title like the following, without changing the file name?

    function post_thumbnails_title( $html, $post_id ) {
    	$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
    	return $html;
    }
    add_filter( 'post_thumbnail_html', 'post_thumbnails_title', 10, 2 );

    https://developer.wordpress.org/reference/hooks/post_thumbnail_html/

    Thread Starter wp-fred

    (@wp-fred-1)

    Hello Katsushi Kawamori,

    Thank you for your suggestion.

    I prefer to permanently update the title-field of the featured image, because I have another (gallery) plugin which uses/reads the title and caption field from the featured image.

    So I guess an add_action function is needed for that?

    wp-fred

    Plugin Author Katsushi Kawamori

    (@katsushi-kawamori)

    Change the title of the media when you update the post.

    function featured_same_title( $post_id, $post ){
    	$attach_id = intval( get_post_meta( $post_id, '_thumbnail_id', true ) );
    	if ( 0 < $attach_id ) {
    		$up_post = array(
    			'ID' => $attach_id,
    			'post_title' => $post->post_title,
    		);
    		wp_update_post( $up_post );
    	}
    }
    add_action( 'save_post', 'featured_same_title', 10, 2 );
    Thread Starter wp-fred

    (@wp-fred-1)

    Hello Katsushi Kawamori,

    Thank you for your snippet. WoW! The code works indeed for updating existing post.

    Would something similar works for inserting new post as well? Maybe by using wp_insert_post function?

    wp-fred

    Plugin Author Katsushi Kawamori

    (@katsushi-kawamori)

    For new posts, you can do it with the “save_post” action.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Use title of post to update the title field of featured image’ is closed to new replies.