WordPress.org

Ready to get started?Download WordPress

Forums

Add image to post programmatically (1 post)

  1. valchy
    Member
    Posted 1 year ago #

    Hi.
    I'm trying to download an image from an external URL and add image to my post.
    So far I've done this but I don't know to continue since the image is not shown into my post:

    // external files
    require_once('wp-load.php');
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    require_once (ABSPATH . 'wp-admin/includes/media.php');
    require_once (ABSPATH . 'wp-admin/includes/file.php');
    // code
    // create new post
    $post_title = "Post Title";
    $post_text = "Post Text";
    $post = array(
      //'id'             => [ <post id> ] //Are you updating an existing post?
      //'menu_order'     => [ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs.
      //'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments.
      'comment_status' => 'closed',
      //'ping_status'    => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off
      'ping_status' => get_option('default_ping_status'),
      //'pinged'         => [ ? ] //?
      //'post_author'    => [ <user ID> ] //The user ID number of the author.
      'post_author' => '1',
      //'post_category'  => [ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories
      //'post_content'   => [ <the text of the post> ] //The full text of the post.
      'post_content' => $post_text,
      //'post_date'      => [ Y-m-d H:i:s ] //The time post was made.
      //'post_date_gmt'  => [ Y-m-d H:i:s ] //The time post was made, in GMT.
      //'post_excerpt'   => [ <an excerpt> ] //For all your post excerpt needs.
      //'post_name'      => [ <the name> ] // The name (slug) for your post
      //'post_parent'    => [ <post ID> ] //Sets the parent of the new post.
      'post_parent' => '',
      //'post_password'  => [ ? ] //password for post?
      //'post_status'    => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] //Set the status of the new post.
      'post_status' => 'publish',
      //'post_title'     => [ <the title> ] //The title of your post.
      'post_title' => $post_title,
      //'post_type'      => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //You may want to insert a regular post, page, link, a menu item or some custom post type
      'post_type' => 'post',
      //'tags_input'     => [ '<tag>, <tag>, <...>' ] //For tags.
      //'to_ping'        => [ ? ] //?
      'to_ping' => '',
      //'tax_input'      => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies.
    );
    // get new ID of the created post
    $post_id = wp_insert_post( $post );

    Now I get the image from URL:

    // get imaged from URL
    $file = "http://website/image_name.jpg";
    $file_name = "image_name.jpg";
    $filename = "wp-content/uploads/2012/09/".$file_name;
    $wp_filetype = wp_check_filetype($filename, $mimes);
    $attachment = array(
    'post_excerpt' => 'image caption',
    'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $filename ),
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => ('post title'),
    'post_content' => 'post content',
    'post_status' => 'inherit',
    );
    $attach_id = wp_insert_attachment($attachment, $filename, $post_id);
    $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
    wp_update_attachment_metadata($attach_id, $attach_data);

    The image is saved correctly to the destination folder, but the image is not shown inside the post (as it is when you add image from the admin board) and no idea how to do that.

Topic Closed

This topic has been closed to new replies.

About this Topic