Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to solve this issue for days.. I consumed all options and therefore need some fresh ideas.. I have a page template for posting/editing 2 different custom post types from front end of wordpress.

For the sake of simplicity, I will name these custom post types as post_type_x and post_type_y here. Names of the pages, at which these post types can be editted would be page_x and page_y respectively and both pages are assigned to use the same template. So, the code structure of the template page is below:

<?php
if (is_page('page_x')) {
    $current_post_type = "post_type_x";

    some if statements to determine the $action variable according to the $_POST variables

        switch ($action) {
            case "...":                 
            .... break;         
            case "update_save":
            .... break;
        }

}  elseif (is_page('page_y')) {
    $current_post_type = "post_type_y";

    some if statements to determine the $action variable according to the $_POST variables

        switch ($action) {
            case "...":                 
            .... break;         
            case "update_save":
            .... break;
        }

} else { ...
}
....
get_header(); ?>
....

<form action="<?php the_permalink(); ?>" method="post" name="post_adv_resp_form" enctype="multipart/form-data">
....
<input type="file" name="file-attachment" id="file-attachment" />
....
</form>
....

Under the "update_save" case there is such a code:

case "update_save":
$post_id = update_post_of_custom_type($_POST['post_id'],$post_title,$post_content,'draft');
if ($post_id != false) {
$file_upload = upload_file_of_custom_type($_FILES['file-attachment'],$post_id,'file-attachment');   
... 
} break;

The problem is: When I am testing on page_x, "update_save" case works perfectly. upload_file_of_custom_type function uploads the file, update the post meta. But, when I am on page_y, the script uploads the file and then breaks, does not update the post meta. And I have a white screen. Although the debug mode is on, no error shows on the screen. When I checked the apache logs, I found nothing..

function upload_file_of_custom_type($files,$post_id,$file_form_handler) {
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    ... some controls ...

    $attach_id = media_handle_upload( $file_form_handler, $post_id, array( 'test_form' => false ) ); $notice = "attach_id: ".$attach_id."upload is OK";                            
    if ($attach_id != false){                                      
        $file_load = update_post_meta($post_id,'_wp_attached_file',$attach_id);       
        if ($file_load == false) {
        $notice = __('An error occured while setting the post meta for the uploaded file.', TDOMAIN);
        } elseif ($file_load == true) {
            $notice = 'OK';
        }
    } else {
        $notice = __('An error occured while uploading the file.', TDOMAIN);
    }
return $notice;
}

One more information.. the post_type_y is posted as a sub page of post_type_y.. I don't know whether this makes any difference.. Are there any ideas about what the problem might be?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I had given a rest to this problem and returned it back today with a fresh mind.. I noticed that the problem is about file upload function.. I revised the code as below:

$attach_id = media_handle_upload( $file_form_handler, $post_id );                         
if ($attach_id != false){ // if the attachment is loaded, register them as post meta
    $file_load = update_post_meta($attach_id,'_wp_attached_file',$files['name']);                                     
    $attach_data = wp_generate_attachment_metadata( $attach_id, ABSPATH."media/".$files['name'] );
    $file_load = wp_update_attachment_metadata( $attach_id, $attach_data );
    if ($file_load == false) {
        $notice = __('An error occured while setting the post meta for the uploaded file.', TDOMAIN);
    } elseif ($file_load == true) {
        $notice = 'OK';
    }
} else {
    $notice = __('An error occured while uploading the file.', TDOMAIN);
}
share|improve this answer
add comment

Your Answer

 
discard

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.