• i want to add image for posts from functions.php when the theme is activate
    i use this code from wordpress doc:

    $filename=get_template_directory()."/n4p/resources/first page descriptions/support.jpg";
    $wp_filetype = wp_check_filetype(basename($filename), null );
    $wp_upload_dir = wp_upload_dir();
    $attachment = array(
        'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $postIdObj=get_page_by_title( "Support", "", "post" );
    $postId=$postIdObj->ID;
    /*
    wp_insert_attachment( $attachment, $filename, $postId );
    
    $attach_id = wp_insert_attachment( $attachment, $filename, $postId );
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    [In future please wrap your code using backticks]

    the problem is that in the upload directory the image don’t exists.
    and when i enter on featured images i see 2 broked images and the url is something like this:

    http://localhost/bwp/wp-content/uploads/E:web designbwp/wp-content/themes/bestwordpressthemesforphotographers/n4p/resources/first page descriptions/support.jpg

    so..can you help me with this?
    thanks!

The topic ‘Add image to posts’ is closed to new replies.