Post thumbnail relative link and HTML modify

I want to remove the website url from all the_post_thumbnail() so they become relative and remove/add attributes from the output.

So far I got the following code added to functions.php of the theme, xcept I am not sure how to modify the thumbnail $html for the second part.

Read More

Help is appreciated

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

function my_post_image_html( $html, $post_id, $post_image_id, $size ) {

    // Create relative url using pars_url in php
    $relURL = wp_get_attachment_url( $post_id ); // Get Post thumbnail Src instead of permalink
    $relURL = parse_url( $relURL );
    $relURL = $relURL['path'];

     //Pass the relURL to post_thumbnail_html modify the src attribute
    $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . $imgsrc .$post_id . '</a>';

    return $html;
}

Related posts

Leave a Reply

1 comment

  1. This is a pretty wonky function but it does the job, it will turn your post thumbnail into a relative url. Not sure why you would do this, use at your own caution, probably want to add some error checking.

    function wpse_83137_relative( $html, $post_id) {
    
          // get thumb src
          $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
          $post_thumbnail_id = get_post_thumbnail_id( $post_id );
          $urly = wp_get_attachment_image_src($post_thumbnail_id );
    
          //parse the url
          $parse_it = $urly[0];
          $parsed_url = parse_url($parse_it, PHP_URL_PATH);
    
          //add other stuff
          $some_thing = 'something else you want to add';
    
          //output relative link, add more stuff here if you want for inside html tags
          $img = '<img src="' . $parsed_url . '">';
    
          return $img . $some_thing;
        }
    
        add_filter( 'post_thumbnail_html', 'wpse_83137_relative', 10, 3 );