Prakash GPz
Prakash GPz

Reputation: 1523

wordpress wp_insert_attachment() url has wrong file directory

i have following code:

$wp_filetype = wp_check_filetype( basename( $image_url ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
    'guid' => $wp_upload_dir['url']. '/' . basename( $image_url ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($image_url)),
    'post_content' => '',
    'post_status' => 'publish'
);

$attach_id = wp_insert_attachment( $attachment, $image_url );

everything works fine, but the attached img tag has a different url than expected in its src attribute. ( wp-contents/uploads instead of wp-contents/uploads/2013/03/ )

Upvotes: 2

Views: 3556

Answers (1)

m1r0
m1r0

Reputation: 460

Try changing $wp_upload_dir['url'] to $wp_upload_dir['path'], or $uploads['baseurl'] . $uploads['subdir'].

Upvotes: 1

Related Questions