0

I want to get the url of a featured image.

I've created a function with wp_insert_post to post data and set a featured image for them. Everything works fine except displaying the url of featured image.

Here is my code :

$newpostname = $movie['title'];

global $wpdb;

$id_ofpost_name     = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_name = '$newpostname'" );
$id_ofpost_title    = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '$newpostname'" );

if( $id_ofpost_name || $id_ofpost_title ) { echo 'post Exist'; } 
else {
    // Register Post Data
    $post                   = array();
    $post['post_status']    = 'publish';
    $post['post_type']      = 'post';               // can be a CPT too
    $post['post_title']     = $newpostname;
    $post['post_content']   = '';
    $post['post_author']    = 1;

    // Create Post
    $post_id = wp_insert_post( $post );

    // Add Featured Image to Post
    $image_url  = $movie['urlPoster'];
    $upload_dir = wp_upload_dir();                  // Set upload folder
    $image_data = file_get_contents( $image_url );  // Get image data
    $filename   = basename( $image_url );           // Create image file name

    // Check folder permission and define file location
    if( wp_mkdir_p( $upload_dir['path'] ) ) {
        $file = $upload_dir['path'] . '/' . $filename;
    } else {
        $file = $upload_dir['basedir'] . '/' . $filename;
    }

    // Create the image  file on the server
    file_put_contents( $file, $image_data );

    // Check image file type
    $wp_filetype = wp_check_filetype( $filename, null );

    // Set attachment data
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title'     => sanitize_file_name( $filename ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    // Create the attachment
    $attach_id = wp_insert_attachment( $attachment, $file, $post_id );

    // Include image.php
    require_once(ABSPATH . 'wp-admin/includes/image.php');

    // Define attachment metadata
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );

    // Assign metadata to attachment
    wp_update_attachment_metadata( $attach_id, $attach_data );

    // And finally assign featured image to post
    set_post_thumbnail( $post_id, $attach_id );
}

The post inserting works perfectly! The problem is that I want to get the url of featured image after upload. How can I do that?

1

Well you have the ID as $attach_id so you can use a function such as wp_get_attachment_url() where you can pass $attach_id and get a url returned:

$featured_url = wp_get_attachment_url( $attach_id );

Or if you want more information such as width, height, etc. you can use functions like wp_get_attachment_image_src() or wp_get_attachment_metadata().

  • it's not working. is it even possible get featured image url after wp_insert_post function ? – Nader - J Mar 2 '16 at 20:41
  • There shouldn't be a reason why it doesn't work after wp_insert_post(). Can you it through function_exists() to see if the wp_get_attachment_url() exists at that point? I'm not sure where you code is but usually you wouldn't need to include wp-admin/includes/image.php. Maybe mock up a simpler insert post in your theme to narrow down the issue. fucntion_exists( 'wp_get_attachment_url' ); – Howdy_McGee Mar 2 '16 at 20:44
  • ok Im going to try that and let you know tge result. thanks – Nader - J Mar 2 '16 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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