WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP 3.5 - set title for image when inserting media (12 posts)

  1. kevin360
    Member
    Posted 1 year ago #

    Hopefully someone will be able to help me with this and it'll be an easy fix! In the previous version of WP I had changed the code so that when I inserted a new image into a post, the title which is shown in the media manager would be set as the title for the image inserted into the post.

    With the upgrade to WP 3.5 the media manager has changed and I'm not sure how to get that back. The php code that does the actual inserting of html into the post appears to be wp-admin/includes/media.php. I've edited the get_image_send_to_editor function so that it does output the title where I want it, but I have to statically set the title. I would like the title to be taken from the media manager Title field for the image.

    I've traced back to the file ajax-actions.php which is in wp-admin/includes, the function wp_ajax_send_attachment_to_editor, appears to be what sets up the variables that are used by the media.php file to output the code. In that function there's this line:

    $title = ''; // We no longer insert title tags into <img> tags, as they are redundant.*/

    I've tried putting in code such as:
    $title = $attachment['post_title']

    but it doesn't work. If I put in this code:
    $title = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';

    It works and the html inserted into the post contains a title which is set to the alt field. But what I want is to set it to the title that's shown in the media manager. Anyone know how I can do that?

    Thanks,
    Kevin

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Tooltips can still be added by opening the Edit Image modal and inserting something into the title attribute box. The only change is that the "Title" field — which is the name of an image's attachment page — does not populate the title attribute.

  3. kevin360
    Member
    Posted 1 year ago #

    That's what I've done in the past and why I edited the previous version of WP to automatically insert the title. Instead of always having to re-edit the image I just inserted to add the title, I edited the code so it was always inserted for me. At least it was working in the previous version of WP. I can force it to be set to the "Alt Test" field, just not the Title field which is what's odd about it.

  4. kevin360
    Member
    Posted 1 year ago #

    Actually just figured it out! Decided to output the entire attachment array to see what's in there and the title isn't even in there. Found that I need to use $post->post_title to get it. So if anyone else wants to do this, in wp-admin/includes/ajax-actions.php change this line:

    $title = ''; // We no longer insert title tags into <img> tags, as they are redundant.*/

    to this:

    $title = isset( $post->post_title ) ? $post->post_title : '';

    Then you also need to edit media.php in wp-admin/includes/ Go to function get_image_send_to_editor and change this line:

    $html = get_image_tag($id, $alt, '', $align, $size);

    to this

    $html = get_image_tag($id, $alt, $title, $align, $size);

    Now you will have the Title from the media manager field inserted into the title field of the image.

    If you also want the Title from the media manager field inserted into the Advanced Link Settings -> Title field, then you'll need to change this line in the function also:

    $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";

    to this:

    $html = '<a href="' . esc_attr($url) . "\" title=\"$title\" $rel>$html </a>";

    As an end user it's not really clear why the Title from the media manager isn't automatically inserted as the title into the post. I'm sure there is a reason it's not done, it just isn't apparent to me. When I fill in the Title field and insert an image and then I click on the Edit Image modal and the Title field is blank, it's confusing why that would be.

  5. bcworkz
    Member
    Posted 1 year ago #

    I'm pleased you were able to get the functionality you desire. But I sincerely hope no one else will attempt to do what you did, which is hack core WordPress files. Doing so is strongly discouraged. You are of course free to do what you wish to your own installation, but suggesting others follow along with your poor practice demonstrates a lack of good judgment.

  6. kevin360
    Member
    Posted 1 year ago #

    I'm not sure I agree that telling people how to do something is suggesting that they do, do that thing.

    But, you bring up a good point, yes it is hacking at the core WP files. So is there a way to do this without hacking at the core files?

  7. OllieJones
    Member
    Posted 1 year ago #

    There's a filter hook that will let you muck around with the tag getting sent to the editor. So, you can avoid hacking the core.

    add_filter('media_send_to_editor', 'mymedia_send_to_editor', 11, 3);
    
    /**
     * adjust hyperlinks and other embed codes
     *
     * @since 2.5.0
     *
     * @param string $html  the string to be inserted in the editor, filtered
     * @param int $attachment_id attachment id
     * @param array $attachment
     * @return filtered html string
     */
    function mymedia_send_to_editor($html, $attachment_id, $attachment) {
            /* grab the attachment post */
    	$post = get_post($attachment_id);
            ...
            return $html;
    }
  8. kevin360
    Member
    Posted 1 year ago #

    Thank you Ollie for taking the time to send that to me. I'll try working with that to see if I can get it to do what I'm wanting to do.

  9. OllieJones
    Member
    Posted 1 year ago #

    Please let me know what you find out kevin360. 3.5 broke my plugin, which uses that filter among others.

  10. I've put together a (very) basic plugin which restores the title attribute when inserting images. As I have the coding skills of a bowl of petunias, this could probably do with a review by someone who really knows what they're doing, but it does work on my site.

    I posted it here:

    http://wordpress.org/support/topic/restoring-titles-to-inserted-images-in-wordpress-35

  11. verify
    Member
    Posted 1 year ago #

    You can also revert it back by inserting this filter into functions.php in your theme

    // return back title tag for images removed in WP 3.5
    add_filter( 'wp_get_attachment_image_attributes', 'myReturnFeaturedImagesTitles', 10, 2 );
    function myReturnFeaturedImagesTitles($attr, $attachment) {
      if (!isset($attr['title']) && isset($attachment->post_title) && $attachment->post_title !='') {
        $attr['title'] = $attachment->post_title;
      }
      return $attr;
    }
  12. conualfy
    Member
    Posted 10 months ago #

    @verify - at least for me it does not to anything..

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.