How to get the SRC previous and next images for use in image.php?

Trying to remove the image from the function previous_image_link () directly through the arguments of this function I do this is not possible. Therefore, try to simply extract the SRC:

Read More
<td style="background: url(

<?php

    $html = previous_image_link('full');
    preg_match_all('/<img [^>]*src=["|']([^"|']+)/i', $html, $matches);
    foreach ($matches[1] as $key=>$value) {
        echo $value;
    }
?>

) no-repeat center center; background-size: cover;">

However, instead of the expected

<td style="background: url(/wp-content/uploads/2015/03/tiaurus.info_20150323125918.jpg") no-repeat center center; background-size: cover;">

getting

<td height="3166" class="attachment-full" alt="img_alt_text" src="/wp-content/blablabla/20150323125918.jpg" 8176"="">) no-repeat center center;background-size: cover;"&gt;
            </td>
) no-repeat center center;background-size: cover;">

As in my case it is necessary to use preg_match_all?

Or can someone tell me how to get the SRC previous and next images for use in image.php?

Related posts

Leave a Reply

2 comments

  1. Instead of trying to use a regex to extract the URL, I think you should try to reverse engineer the previous_image_link function to find a more direct approach.

    previous_image_link is essentially a wrapper around adjacent_image_link. Once you see how the adjacent image is being fetched, it is pretty easy to write a function that fetches the image src instead of the formatted link:

    function adjacent_image_src($prev = true) {
        $post = get_post();
        $attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) );
    
        foreach ( $attachments as $k => $attachment ) {
            if ( $attachment->ID == $post->ID ) {
                break;
            }
        }
    
        $attachment_id = false;
        $src = false;
    
        if ( $attachments ) {
            $k = $prev ? $k - 1 : $k + 1;
    
            if ( isset( $attachments[ $k ] ) ) {
                $attachment_id = $attachments[ $k ]->ID;
                $src = wp_get_attachment_image_src( $attachment_id );
            }
        }
    
        return $src;
    }
    

    You could also just return $attachment_id and then you could get whatever information you need about the attachment.

  2. You can use PHP function pre_match and eliminate the foreach loop.

    preg_match('/ src=["']([^s]+)["']/', $html, $matches);
    echo $matches[1];
    

    Since your $html string only has one src value to retrieve, preg_match is sufficient. preg_match_all is designed for multiple matches.

    Also, since src= is unique in the string, we can narrow down our pattern to something that starts with src=, and is wrapped by either double quotes or single quotes. ["'] is a character class that limits the match to one character that is either a double quote or a single quote. (The single quote is escaped because I’m using single quotes to delimit the regular expression string.)

    The parenthesis () capture a value and add it to the $matches array.

    [^] is a negative character class, in other words, don’t match any of the characters listed here. + matches one or more of the previous character. So [^s]+ is simply saying, match one or more characters that are not a space character.

    Given:

    $html = '<a href="/33279/retro-kartinyi-iz-skotcha-max-zorn"><img width="1600" height="900" class="attachment-full" style="width: 516px; height: 290px;" alt="Max Zorn" src="/wp-content/uploads/2015/03/tiaurus.info_20150323125854.jpg"></a>';
    

    echo $matches[1]; returns:

    /wp-content/uploads/2015/03/tiaurus.info_20150323125854.jpg