Skip to main content
deleted 28 characters in body
Source Link
Xhynk
  • 870
  • 5
  • 19

There's a couple functions to get different attachment parameters based on one another, but a cursory look doesn't indicate there's one to get one based on the Attachment Page URL.

You could try and get the Post ID based on the Name (or Title) using the $wpdb class, and then drop that ID into the wp_get_attachment_image_src() function. Something like this would get you started:

function get_attachment_id_by_url( $url ){
    global $wpdb;

    $url   = trim( $url, ' /' ); // Trim whitespace and trailing slash
    $parts = explode( '/', $url ); 
    $name  = end( $parts ); // We only want the last part

    if( ! $result = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name ) ) ) // potentially replace post_name with post_title
        return false;

    return $result->ID;
}

This would let you do something like this:

// Attachment URL we want to utilize
$image_url = 'https://example.com/attachment/name_of_photo/';

// Get the ID using our function above
$image_id  = get_attachment_id_by_url( $image_url );

// Get the SRC parameters from that ID (use whatever `$size` args you want)
$image_src = wp_get_attachment_image_src( $image_url, 'full' );

/**
 * var_dump($image_src) Output:
 * 
 * array(4) {
 *    [0]=> string(11748) "https://xhynkexample.com/content-mask/wp-content/uploads/2018/07attachment/name_of_photo.jpg"png"
 *    [1]=> int(640)
 *    [2]=> int(480)
 *    [3]=> bool(true)
 * }
 */

There's a couple functions to get different attachment parameters based on one another, but a cursory look doesn't indicate there's one to get one based on the Attachment Page URL.

You could try and get the Post ID based on the Name (or Title) using the $wpdb class, and then drop that ID into the wp_get_attachment_image_src() function. Something like this would get you started:

function get_attachment_id_by_url( $url ){
    global $wpdb;

    $url   = trim( $url, ' /' ); // Trim whitespace and trailing slash
    $parts = explode( '/', $url ); 
    $name  = end( $parts ); // We only want the last part

    if( ! $result = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name ) ) ) // potentially replace post_name with post_title
        return false;

    return $result->ID;
}

This would let you do something like this:

// Attachment URL we want to utilize
$image_url = 'https://example.com/attachment/name_of_photo/';

// Get the ID using our function above
$image_id  = get_attachment_id_by_url( $image_url );

// Get the SRC parameters from that ID (use whatever `$size` args you want)
$image_src = wp_get_attachment_image_src( $image_url, 'full' );

/**
 * var_dump($image_src) Output:
 * 
 * array(4) {
 *    [0]=> string(117) "https://xhynk.com/content-mask/wp-content/uploads/2018/07/name_of_photo.jpg"
 *    [1]=> int(640)
 *    [2]=> int(480)
 *    [3]=> bool(true)
 * }
 */

There's a couple functions to get different attachment parameters based on one another, but a cursory look doesn't indicate there's one to get one based on the Attachment Page URL.

You could try and get the Post ID based on the Name (or Title) using the $wpdb class, and then drop that ID into the wp_get_attachment_image_src() function. Something like this would get you started:

function get_attachment_id_by_url( $url ){
    global $wpdb;

    $url   = trim( $url, ' /' ); // Trim whitespace and trailing slash
    $parts = explode( '/', $url ); 
    $name  = end( $parts ); // We only want the last part

    if( ! $result = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name ) ) ) // potentially replace post_name with post_title
        return false;

    return $result->ID;
}

This would let you do something like this:

// Attachment URL we want to utilize
$image_url = 'https://example.com/attachment/name_of_photo/';

// Get the ID using our function above
$image_id  = get_attachment_id_by_url( $image_url );

// Get the SRC parameters from that ID (use whatever `$size` args you want)
$image_src = wp_get_attachment_image_src( $image_url, 'full' );

/**
 * var_dump($image_src) Output:
 * 
 * array(4) {
 *    [0]=> string(48) "https://example.com/attachment/name_of_photo.png"
 *    [1]=> int(640)
 *    [2]=> int(480)
 *    [3]=> bool(true)
 * }
 */
Source Link
Xhynk
  • 870
  • 5
  • 19

There's a couple functions to get different attachment parameters based on one another, but a cursory look doesn't indicate there's one to get one based on the Attachment Page URL.

You could try and get the Post ID based on the Name (or Title) using the $wpdb class, and then drop that ID into the wp_get_attachment_image_src() function. Something like this would get you started:

function get_attachment_id_by_url( $url ){
    global $wpdb;

    $url   = trim( $url, ' /' ); // Trim whitespace and trailing slash
    $parts = explode( '/', $url ); 
    $name  = end( $parts ); // We only want the last part

    if( ! $result = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name ) ) ) // potentially replace post_name with post_title
        return false;

    return $result->ID;
}

This would let you do something like this:

// Attachment URL we want to utilize
$image_url = 'https://example.com/attachment/name_of_photo/';

// Get the ID using our function above
$image_id  = get_attachment_id_by_url( $image_url );

// Get the SRC parameters from that ID (use whatever `$size` args you want)
$image_src = wp_get_attachment_image_src( $image_url, 'full' );

/**
 * var_dump($image_src) Output:
 * 
 * array(4) {
 *    [0]=> string(117) "https://xhynk.com/content-mask/wp-content/uploads/2018/07/name_of_photo.jpg"
 *    [1]=> int(640)
 *    [2]=> int(480)
 *    [3]=> bool(true)
 * }
 */