[resolved] How to get image URL into custom field using wp_insert_post (4 posts)

  1. kirkward
    Posted 3 years ago #

    Please let me start by saying, I is not a coder! I am not that smart. Plus, I'm an old, tired and ugly pensioner.

    What I am showing below is the results of copy, paste and play around until I get something close to what I want.

    I have created a dashboard widget. I include a php file that has the following code. So far, I've gotten everything to work the way I want, except the image uploaded for the "thumb" is not being placed in the existing thumb custom field, and the thumb custom field is not being updated.

    I am creating a new custom field named 'thumb,' but it is empty.

    What I done wrong?

    <!-- New Post Form --><div id="postbox">
    <form id="new_post" name="new_post" method="post" action="">
    <p><label for="title">Your Name</label><br /> <input type="text" id="title" value="" tabindex="1" size="20" name="title" /> </p>
    <p><label for="description">Your Bio</label><br /> <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea> </p>
    <p><label for="async-upload">Your Picture</label><br />
        <input type="file" id="async-upload" value="" tabindex="1" size="20" name="async-upload" />
    <p align="right"><input type="submit" value="Update" tabindex="6" id="submit" name="submit" /></p>
    <input type="hidden" name="post_type" id="post_type" value="post" />
    <input type="hidden" name="action" value="post" /> <?php wp_nonce_field( 'new-post' ); ?>
    </form> </div> <!--// New Post Form -->
    $post_slug = "featured-post" ;
    $url = "/featured/featured-post" ;
    $postid = url_to_postid( $url );
        // Entering the text between the quotes
        echo "$postid" ;
    <br />
    $cat_name = "featured" ;
    $kw_cat_id = get_cat_ID( $cat_name );
        echo "$kw_cat_id" ;
    $postdate = date('2010-02-23 18:57:33');
    if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
    // Do some minor form validation to make sure there is content
    if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
    } else {
    echo 'Please enter a title';
    if (isset ($_POST['description'])) {
    $description = $_POST['description'];
    } else {
    echo 'Please enter the content';
    // Add the content of the form to $post as an array
    $post = array(
    'ID'      => $postid,
    'post_title'    => $title,
    'post_name'     => $post_slug,
    'post_content'    => $description,
    'post_category'    =>array( $kw_cat_id ),  // Usable for custom taxonomies too
    'post_date'     =>   $postdate,
    'post_status'    => 'publish'            // Choose: publish, preview, future, etc.
    // Pass  the value of $post to WordPress the insert function
    // http://codex.wordpress.org/Function_Reference/wp_insert_post
    $newPost = wp_insert_post($post);
    //insert image
    require_once(ABSPATH . 'wp-admin/includes/admin.php');
    $attachmentId = media_handle_upload('async-upload'); //post id of Client Files page
    add_post_meta($newPost, 'thumb', wp_get_attachment_url($attachmentId));
    wp_redirect( home_url() ); // redirect to home page after submit
    // Do the wp_insert_post action to insert it
    do_action('wp_insert_post', 'wp_insert_post');
    } // end IF
  2. kirkward
    Posted 3 years ago #


    I have been able to get the custom field updated, or at least I think I have. But, is being updated with an empty value.

    Here is the change I have made ...

    /insert image
    require_once(ABSPATH . 'wp-admin/includes/admin.php');
    $attachmentId = media_handle_upload('async-upload'); //post id of Client Files page
    update_post_meta($postid, 'thumb', wp_get_attachment_url($attachmentId));

    It appears to me that the original code is not getting the path of the uploaded image. I believe that is one section I have not changed, because as I said, I is not a coder.

    Can anyone give me any suggestions on how to get the image url, so it can be used as the value to go with thumb as a key?

  3. kirkward
    Posted 3 years ago #

    Seems the code I was copying did not have the post id parameter in the async line, and it did not have the enctype declaration between the form tags.

    Got it working.

  4. RonStrilaeff
    Posted 3 years ago #

    Hi Kirkward,

    If it's not too much trouble could you paste in the entire working code?
    I'm starting to work on something similar. There are lots of bits and pieces everywhere, but and it's hard to find a working example of the whole thing, from form fields to finished post.

    Thanks, Ron

Topic Closed

This topic has been closed to new replies.

About this Topic