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 build a script, to post from frontend. There are many custom-fields. Most works fine, but with one thing i have problems.

First a description, then the script.

It seems like, that images won't upload, cause i can't find them in the upload-directory or anywhere else. The cutom-field will be generated, that's no problem. I say that, cause the field appears at the backend - but empty. I think it's empty, cause it doesn't upload. Yes, for testing i have put 777 to my serversettings for the upload-directory, so i think the server is not disabling the upload.

P.S.: I shortened the code to make it clear. If you want the hole code, just say a word.

Thank you for helping!

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
    // Funktion zum Image-Upload, falls sie noch nicht existiert
    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');
    }

    // Variablen für die Bilder
    $image_1_gesucht = wp_handle_upload( $file['image_1_gesucht'] );
    $image_2_gesucht = wp_handle_upload( $file['image_2_gesucht'] );

    // Benutzerdefinierfte Felder für die Bilder
    add_post_meta($pid,'image_1_gesucht',$image_1_gesucht['url']);
    add_post_meta($pid,'image_2_gesucht',$image_2_gesucht['url']);
}

<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
    <div class="pro50left">
        <fieldset class="images">
            <label for="images">Bild 1:</label>
            <input type="file" name="image_1_gesucht" id="image_1_gesucht">
        </fieldset>
    </div>
    <div class="pro50left">
        <fieldset class="images">
            <label for="images">Bild 2:</label>
            <input type="file" name="image_2_gesucht" id="image_2_gesucht">
        </fieldset>
    </div>
</form>
share|improve this question
add comment

1 Answer

Found the solution. Forgot 2 lines of code. New is line 2 and line 11. I post the complete code:

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
    $file=$_FILES;
    // Funktion zum Image-Upload, falls sie noch nicht existiert
    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');
    }

    // Variablen für die Bilder
    $overrides = array( 'test_form' => false);
    $image_1_gesucht = wp_handle_upload( $file['image_1_gesucht'], $overrides );
    $image_2_gesucht = wp_handle_upload( $file['image_2_gesucht'], $overrides );

    // Benutzerdefinierfte Felder für die Bilder
    add_post_meta($pid,'image_1_gesucht',$image_1_gesucht['url']);
    add_post_meta($pid,'image_2_gesucht',$image_2_gesucht['url']);
}

<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
    <div class="pro50left">
        <fieldset class="images">
            <label for="images">Bild 1:</label>
            <input type="file" name="image_1_gesucht" id="image_1_gesucht">
        </fieldset>
    </div>
    <div class="pro50left">
        <fieldset class="images">
            <label for="images">Bild 2:</label>
            <input type="file" name="image_2_gesucht" id="image_2_gesucht">
        </fieldset>
    </div>
</form>
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.