WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Having Trouble with get_image_tag filter (8 posts)

  1. mrwweb
    Member
    Posted 2 years ago #

    I'm working on a plugin that would create four new fields for each item in the media library: Creator Name, Creator URL, License Name, and License URL. It strings these all together to make a nice credit for the media item.
    I have the meta fields setup and working thanks to this tutorial. I've also successfully used the the_content filter to display the new fields on attachment pages. Therefore, I know the data is being stored and can be referenced in filters.

    I'm just stuck on the implementation to get media credits showing below images embedded in posts. I think I should be using the get_image_tag filter, but am not 100% sure.

    I get the feeling I'm not handling the arguments on get_image_tag correctly, but I don't know what is going wrong. I'm still pretty new with filters/actions, so this may be something really obvious that I'm missing. I have neither any output nor errors with wp_debug enabled.

    Here's my code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Any help is greatly appreciated.

  2. mrwweb
    Member
    Posted 2 years ago #

    Sorry code moderator.

    Here's my code: http://pastebin.com/VRcEyYBh

    (I unprefixed all my functions and variables for readability.)

  3. mrwweb
    Member
    Posted 2 years ago #

    Can anyone at least confirm to me when the get_image_tag function runs? (It's in /wp-includes/media.php.)

    It's my understanding that it is what displays the HTML for images inserted into pages and posts, but my inability to append even a static string to the $html variable or to get an error has me grasping for answers.

  4. Ian Dunn
    Member
    Posted 2 years ago #

    I ran a quick test using this code:

    add_filter('get_image_tag', 'append_media_credit', 10, 2);
    function append_media_credit($html, $id)
    {
    	$media_string = 'test';
    	$html = $html . $media_string;
    	return $html;
    }

    And got:

    <a href="http://linux-playground.mpangodev.com/wpms/test-theme/files/2011/10/Avenue-A-Strummer-Memorial.jpg"><img src="http://linux-playground.mpangodev.com/wpms/test-theme/files/2011/10/Avenue-A-Strummer-Memorial-300x225.jpg" alt="" title="OLYMPUS DIGITAL CAMERA" width="300" height="225" class="alignnone size-medium wp-image-678" />test</a>

    When I uploaded an image and clicked on the "Insert into Post" button.

    So, it looks like you're on the right track. Can you copy the whole plugin to pastebin? I'll take a look and see if I can find something.

    Are you saving the image before inserting it into the post? get_post_meta() in media_credit_string() is going to pull from the database, so it won't get anything if you haven't saved the meta fields first.

  5. mrwweb
    Member
    Posted 2 years ago #

    Thanks, Ian. I owe you one.

    I don't know if I can work on this today, but I'm going to try your code, and just see if I can get that working on its own.

    I just pasted the entire plugin here: http://pastebin.com/6Xp4cXiG

    The relevant "broken" part (I think) is lines 88-98 which uses the function starting on line 128.

    I was also surprised to see that the image link wraps the result of your filter above. Not what I expected nor wanted. Would it make sense to figure out the hook that wraps the image tag in the link and then append to that? Is there even a hook for that or is it just the editor doing that?

  6. Ian Dunn
    Member
    Posted 2 years ago #

    I installed it on one of my dev servers and it seems to work fine. I uploaded an image and then inserted it into the post (without saving first) and got this:

    <div id="_mcePaste"><img class="aligncenter size-medium wp-image-399" title="Capitalism Without Discretion" src="http://redacted.mpangodev.com/wp/wp-content/uploads/Capitalism Without Discretion-300x225.jpg" alt="" width="300" height="225" /><span class="mrw-media-attr">Credit: <a href="http://test.url">test creator name</a>. <a href="http://test-license.url">test license name</a>.</span></div>

    Maybe there's something about your installation that is causing it to break? Another plugin or parts of the theme, maybe? I'm using WP 3.3-beta3.

    If you want to use an earlier filter so you can get outside the image link, try image_send_to_editor.

  7. mrwweb
    Member
    Posted 2 years ago #

    Ian, you are a genius!

    I was under the impression that the hook fired on the front end, not in the editor. I had created my page with images for testing before I had finished the plugin, and was expecting it to insert the text outside of the editor, and not when inserting the image. You're right that it seems to be working, just not as I had expected. I'm not thrilled with the fact that this wouldn't retroactively add credits to images that have already been inserted into posts/pages.

    Now to dig for a new hook. (I get the sinking feeling that the only other option would be doing some regex on the_content, but I'll keep looking. I'm still rather green when it comes to this stuff.)

    Thanks, again. I really appreciate it.

  8. Ian Dunn
    Member
    Posted 2 years ago #

    Ah, ok. Yeah, I don't think there's any filter for individual parts of the content once it's been inserted into the database (except maybe if it's in a shortcode). You'd have to either use the the_content filter to apply the changes on the fly, or use your current method and add a part that goes back and retroactively applies the changes to the content that's already in the database.

Topic Closed

This topic has been closed to new replies.

About this Topic