1

I want to add an external HTML link to a post thumbnail using the add_filter function below, but the code doesnt work.

add_filter( 'post_thumbnail_html', 'my_post_link_html', 10, 3 );

function my_post_link_html( $html, $post_id, $post_image_id ) {

if ( $GLOBALS['ghostpool_affiliate_button_link'] ) {
$affiliate_link = $GLOBALS['ghostpool_affiliate_button_link'];
$affiliate_target = apply_filters( 'gp_affiliate_link_target', '' );
}
$html = '<a href="' . get_permalink( $affiliate_link ) . '" rel="nofollow" . target="' . $affiliate_target . '">' . $html . '</a>';

return $html;

}

UPDATE:

<?php if ( $GLOBALS['ghostpool_affiliate_button_link'] ) { ?>
    <a href="<?php echo $GLOBALS['ghostpool_affiliate_button_link']; ?>" id="gp-affiliate-button" rel="nofollow" target="<?php echo apply_filters( 'gp_affiliate_link_target', '' ); ?>">
        <?php echo $GLOBALS['ghostpool_affiliate_button_text']; ?>
    </a>    
<?php } ?>
3
  • What are the values of $GLOBALS['ghostpool_affiliate_button_link'], $affiliate_link, and $affiliate_target? The code you've posted does successfully wrap the post thumbnail within a link, as long as $affiliate_link is a valid post ID, so more information is needed to help you. Commented Sep 8, 2016 at 0:45
  • @dave-romsey Forgot to mention, that this is actually a page, not a post, will that make the difference for the filter I am using? $GLOBALS['ghostpool_affiliate_button_link'] stores the link I want to assign to the post thumbnail, it is entered in the page edit section in custom meta box.
    – Radi
    Commented Sep 8, 2016 at 12:32
  • The post_thumbnail_html filter will work the same on posts and pages. However, you should be passing a post id to get_permalink(), not a URL. Added an answer taking this new information into account. Commented Sep 8, 2016 at 14:14

1 Answer 1

0

It sounds like you are trying to use get_permalink() on a URL. get_permalink() expects a post ID, not a URL. Try this:

add_filter( 'post_thumbnail_html', 'my_post_link_html', 10, 3 );
function my_post_link_html( $html, $post_id, $post_image_id ) {
    // Uncomment for debugging. This will replace the featured image with the debugging output. 
    // exit ( var_dump( $GLOBALS['ghostpool_affiliate_button_link'] ) ); 

    if ( $GLOBALS['ghostpool_affiliate_button_link'] ) {
        $affiliate_link = $GLOBALS['ghostpool_affiliate_button_link'];
        $affiliate_target = apply_filters( 'gp_affiliate_link_target', '' );

        $html = '<a href="' . esc_url( $affiliate_link ) . '" rel="nofollow" . target="' . esc_attr( $affiliate_target ) . '">' . $html . '</a>';
    }

    return $html;
}
5
  • thanks, but it still doesnt work. I guess $GLOBALS['ghostpool_affiliate_button_link'] doesnt store the ID or I am doing something wrong. Please take a look at the code I pasted in the update of my post. That's where I came from with $GLOBALS['ghostpool_affiliate_button_link']. This is the code for the Buy Now button on the right of my site link and I want to assign the same link to the Bloomberg image on the left.
    – Radi
    Commented Sep 9, 2016 at 12:20
  • Guessing is not good enough. You need to guess, then check. :-) Do an exit ( var_dump( $GLOBALS['ghostpool_affiliate_button_link'] ) ); at the top of my_post_link_html(); What's the value? Commented Sep 9, 2016 at 14:21
  • at the top you mean after $post_image_id and before the closing bracket or the line above the function my_post_link_html? And where will I see the results of the dump?
    – Radi
    Commented Sep 9, 2016 at 14:59
  • Updated the code, added note about debugging. Commented Sep 9, 2016 at 15:04
  • This doesnt dump anything, the thumbnail doesnt change at all. I checked all the thumbnails on the site and none of them changed to output anything. There would be probably another issue with this because I would need that link only for that particular image and not every thumbnail on the page. I ended up hard coding the link for this image, that will sort the problem for the time being, but would like to know how to do that the proper way using filters in functions.php.
    – Radi
    Commented Sep 9, 2016 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.