WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a front end post form that I'm having some problems getting the right path for the media to save as custom fields. The path to the file gets saved as mysite.com/home/betauser/public_html/betatheme/wp_content/....file path. How do I get the path to save correctly?

<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
if ( ! function_exists( 'wp_handle_upload' )) { 
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
} else {
    echo 'Please enter a game  title';
if (isset ($_POST['description'])) {
    $description = $_POST['description'];
} else {
    echo 'Please enter the content';
$tags = $_POST['post_tags'];

// Add the content of the form to $post as an array
$new_post = array(
    'post_title'    => $title,
    'post_content'  => $description,
    'post_category' => 11,
    'tags_input'    => array($tags),
    'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
    'post_type' => fod_music  // Use a custom post type if you want to
$overrides = array( 'test_form' => false);
$uploaded_music = wp_handle_upload( $file['music_file'], $overrides );
$uploaded_music_art = wp_handle_upload( $file['music_image'], $overrides ); 
//save the new post and return its ID
$pid = wp_insert_post($new_post);
wp_redirect( get_permalink($pid)); 

do_action('wp_insert_post', 'wp_insert_post');

<div id="postbox">
 <form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
  <p><label for="title">Song Title</label><br />
   <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
  <p><label for="music_file">Upload a song</label><br />
   <input type="file" name="music_file" id="music_file"/>
  <p><label for="description">Description</label><br />
   <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
  <p><label for="music_image">Upload a music art</label><br />
   <input type="file" name="music_image" id="music_image"/>
  <p><label for="post_tags">Tags</label>
   <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
  <p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
  <input type="hidden" name="action" value="new_post" />
  <?php wp_nonce_field( 'new-post' ); ?>
share|improve this question
up vote 1 down vote accepted

For starters, the 'post_category' argument only accepts category ID's, so you will have to find that through your database, or through get_category_by_slug:

$cat = get_category_by_slug( 'music' ); 
$cat_id = $cat->term_id;

Secondly, look at your wp_handle_upload() section. You are passing $file['file'] in both cases, where 'file' is not the name of either of your upload fields. I believe it should be:

$uploaded_music = wp_handle_upload( $file['music_file'], $overrides );
$uploaded_music_art = wp_handle_upload( $file['music_image'], $overrides ); 
share|improve this answer
yeap I had just caught both those things. Do you know why it's storing the wrong paths to the media? – Pollux Khafra Aug 29 '12 at 14:02
I edited the question to reflect the issue with the path saved and upvoted this answer. – Pollux Khafra Aug 29 '12 at 14:20
What are the paths being given? – Eric Holmes Aug 29 '12 at 15:09
You can see the path example in the edited question. It's adding path from my server to the path. – Pollux Khafra Aug 29 '12 at 15:11
instead of 'file' from the returned values, use 'url'. See the codex for why. update_post_meta($pid,'music_code',$uploaded_music['url']); update_post_meta($pid,'music_art',$uploaded_music_art['url']); – Eric Holmes Aug 29 '12 at 15:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.