WordPress.org

Ready to get started?Download WordPress

Forums

Get the Image
Feature request with code examples (1 post)

  1. Pepe Huerta
    Member
    Posted 2 years ago #

    Hi, I've been using this plugin since.. I cant remember.. it's just one of my favorites for its simplicity :)

    I want to request a couple of useful features that I manage to pull patching the plugin. Justin, if you dont mind, this are the scenarios and the solutions:

    First Scenario.

    Open Graph meta tags needs the image url (not the image html code), and get_the_image function does not provides a clean url for the image (or at least is not documented). So, I added a couple of lines here an there:

    /* Set the default arguments. */
    	$defaults = array(
    		[...]
    		'image_source' => false,

    Then:

    /* If $link_to_post is set to true, link the image to its post. */
    	if ( $link_to_post )
    		$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) ) . '">' . $html . '</a>';
    
    	if ( $image_source )
    		$html = $image['src'];

    So, the function will print the image source URL instead of the full html img tag:

    <?php get_the_image(array('image_source' => true)); ?>
    Result:
    http://example.com/wp-content/uploads/2012/06/example.jpg

    And now I can use the function for my Open Graph tags:

    <meta property="og:image" content="<?php get_the_image(array('image_source' => true)); ?>"/>

    Second Scenario:
    I need get_the_image to pull the post image linked to it self (not to it's parent post).

    /* Set the default arguments. */
    	$defaults = array(
    		[...]
    		'link_to_image' => false,

    Then:

    /* If $link_to_post is set to true, link the image to its post. */
    	if ( $link_to_post )
    		$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) ) . '">' . $html . '</a>';
    
    	if ( $link_to_image )
    		$html = '<a href="' . $image['src'] . '" class="attachment-link" title="' . esc_attr( apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) ) . '">' . $html . '</a>';

    That will return:
    <a href="http://example.com/wp-content/uploads/2012/06/example.jpg" class="attachment-link" title="The Title"><img src="http://example.com/wp-content/uploads/2012/06/example.jpg" /></a>

    Im sure this can be done in a better way, since I'm a noob at php coding :P But I humbly submit this to you if you think it proves useful for the plugin!

    Thank you very much and I really appreciate all the hard work you have put on this... you make my life easier! :D

    http://wordpress.org/extend/plugins/get-the-image/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic