Agregando una imagen local a la librería y como imagen destacada en wordpress


[php]

function _import_construct_image_post($filename,$post)
{
$wp_filetype = wp_check_filetype(basename($filename), null );
$wp_upload_dir = wp_upload_dir();
//print_r($wp_upload_dir);

$featured_upload_filename = $wp_upload_dir[ ‘path’ ] . ‘/’ . $filename;

@copy($filename, $featured_upload_filename);
echo $wp_upload_dir[ ‘url’ ] . ‘/’ . basename( $featured_upload_filename);

$attachment = array(
‘guid’ => $wp_upload_dir[ ‘url’ ] . ‘/’ . basename( $featured_upload_filename ),
‘post_mime_type’ => $wp_filetype[‘type’],
‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, basename($featured_upload_filename)),
‘post_content’ => ”,
‘post_status’ => ‘inherit’
);

$attach_id = wp_insert_attachment( $attachment,$featured_upload_filename,$post );
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once(ABSPATH . ‘wp-admin/includes/image.php’);

$attach_data = wp_generate_attachment_metadata( $attach_id, $featured_upload_filename);
wp_update_attachment_metadata( $attach_id, $attach_data );
add_post_meta( $post, ‘_thumbnail_id’, $attach_id );

}[/php]