-3

I have built a little form in a frontend page.

I want user to create new post in a specific sort of post (listing).

All fields are OK but only upload file is not working. Could you please tell me what is wrong with [this code]

Here is the full code :

Processing form fields code

<?php if ( ! isset( $_SESSION ) ) session_start();
/*
   Template Name: New Event Page
*/
/*
   for uploading media file as featured image post
*/
    function insert_attachment($file_handler,$post_id,$setthumb='false') {
    // check to make sure its a successful upload
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    $attach_id = media_handle_upload( $file_handler, $post_id );
    set_post_thumbnail( $post_id , $attach_id);

    if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
    return $attach_id;
    }

/* Options for template pages */

    $et_ptemplate_settings = array();
    $et_ptemplate_settings = maybe_unserialize( get_post_meta(get_the_ID(),'et_ptemplate_settings',true) );

    $fullwidth = isset( $et_ptemplate_settings['et_fullwidthpage'] ) ? (bool) $et_ptemplate_settings['et_fullwidthpage'] : false;

/* Checking errors and data forms */

$et_regenerate_numbers = isset( $et_ptemplate_settings['et_regenerate_numbers'] ) ? (bool) $et_ptemplate_settings['et_regenerate_numbers'] : false;

$et_error_message = '';
$et_contact_error = false;



    /* Checking captcha codes */
    if ( isset($_POST['et_eventorm_submit']) ) {
        if ( !isset($_POST['et_contact_captcha']) || empty($_POST['et_contact_captcha']) ) {
            $et_error_message .= '<p>' . esc_html__('Make sure you entered the captcha. ','Explorable') . '</p>';
            $et_contact_error = true;
        } else if ( $_POST['et_contact_captcha'] <> ( $_SESSION['et_first_digit'] + $_SESSION['et_second_digit'] ) ) {
            $et_numbers_string = $et_regenerate_numbers ? esc_html__('Numbers regenerated.','Explorable') : '';
            $et_error_message .= '<p>' . esc_html__('You entered the wrong number in captcha. ','Explorable') . $et_numbers_string . '</p>';

            if ($et_regenerate_numbers) {
                unset( $_SESSION['et_first_digit'] );
                unset( $_SESSION['et_second_digit'] );
            }

            $et_contact_error = true;

        /* Checking custom fields filled */
        } else if ( empty($_POST['et_contact_title_event']) || empty($_POST['et_contact_desc_event']) || empty($_POST['et_contact_infosupp_event']) ){
            $et_error_message .= '<p>' . esc_html__('Make sure you fill all fields. ','Explorable') . '</p>';
            $et_contact_error = true;
        }

} else {
    $et_contact_error = true;
    if ( isset($_SESSION['et_first_digit'] ) ) unset( $_SESSION['et_first_digit'] );
    if ( isset($_SESSION['et_second_digit'] ) ) unset( $_SESSION['et_second_digit'] );
}

if ( !isset($_SESSION['et_first_digit'] ) ) $_SESSION['et_first_digit'] = $et_first_digit = rand(1, 15);
else $et_first_digit = $_SESSION['et_first_digit'];

if ( !isset($_SESSION['et_second_digit'] ) ) $_SESSION['et_second_digit'] = $et_second_digit = rand(1, 15);
else $et_second_digit = $_SESSION['et_second_digit'];

/* Formatting data to update in WP_DB */
if( !$et_contact_error ){

$eventcategory= $_POST['cat'];



// Add the content of the form to $post as an array
$post = array(
    'post_title'    => $_POST['et_contact_title_event'],
    'post_content'  => $_POST['et_contact_infosupp_event'],
    //'post_category'   => $_POST['et_contact_cat_event'],  // Usable for custom taxonomies too
    'post_status'   => 'draft',         // Choose: publish, preview, future, etc.
    'post_type' => 'listing'  // Use a custom post type if you want to
);

$pid = wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function


if ($_FILES) {
array_reverse($_FILES);
$i = 0;//this will count the posts
        foreach ($_FILES as $file => $array) {
            if ($i == 0) $set_feature = 1; //if $i ==0 then we are dealing with the first post
            else $set_feature = 0; //if $i!=0 we are not dealing with the first post
                    $newupload = insert_attachment($file,$pid, $set_feature);
            echo $i++; //count posts
            }
} 


/* Linking list category with the post */
wp_set_object_terms($pid, $eventcategory,'listing_type');


$et_error_message = '<p>' . esc_html__('Merci pour le nouvel evenement!') . '</p>' ;


} ?>

Form code

                            <form action="<?php echo esc_url( get_permalink( get_the_ID() ) ); ?>" method="post" id="et_contact_form" enctype="multipart/form-data">
                                <div id="et_contact_left">

                                    <!-- post event title -->
                                    <p class="clearfix">
                                        <label for="et_contact_title_event" class="et_contact_form_label">Titre de l'&eacute;v&eacute;vement sportif</label>
                                        <input type="text" name="et_contact_title_event" value="Titre de l'&eacute;v&eacute;vement sportif" id="et_contact_title_event" class="input" />
                                    </p>

                                    <!-- post event description -->
                                    <p class="clearfix">
                                        <label for="et_contact_desc_event" class="et_contact_form_label">Description br&egrave;ve de l'&eacute;v&eacute;vement sportif</label>
                                        <input type="text" name="et_contact_desc_event" value="Description br&egrave;ve de l'&eacute;v&eacute;vement sportif" id="et_contact_desc_event" class="input" />
                                    </p>

                                     <!-- post event category -->
                                    <p class="clearfix">
                                        <fieldset class="et_contact_cat_event" name="et_contact_cat_event">
                                            <label for="et_contact_cat_event" class="">Cat&eacute;gorie de l'&eacute;v&eacute;nement sportif : </label>
                                                <?php wp_dropdown_categories( 'tab_index=10&taxonomy=listing_type&hide_empty=0&value_field=name' ); ?>
                                        </fieldset>
                                    </p>

                                    <!-- post event more info -->
                                    <p class="clearfix">
                                        <label for="et_contact_infosupp_event" class="et_contact_form_label">Informations compl&eacute;mentaires</label>
                                        <textarea class="input" id="et_contact_infosupp_event" name="et_contact_infosupp_event">Informations compl&eacute;mentaires</textarea>
                                    </p>


                                <!-- post event featured image -->
                                <p class="clearfix">
                                    <!-- images -->
                                    <fieldset class="images">
                                    <label for="file">Image &agrave; la une</label>
                                    <input type="file" name="file" id="file" >
                                    </fieldset>
                                </p>    

                                </div> <!-- #et_contact_left -->

                                <div id="et_contact_right">
                                    <p class="clearfix">
                                        <?php
                                            esc_html_e('Captcha: ','Explorable');
                                            echo '<br/>';
                                            echo esc_attr($et_first_digit) . ' + ' . esc_attr($et_second_digit) . ' = ';
                                        ?>
                                        <input type="text" name="et_contact_captcha" value="<?php if ( isset($_POST['et_contact_captcha']) ) echo esc_attr($_POST['et_contact_captcha']); ?>" id="et_contact_captcha" class="input" size="2" />
                                    </p>
                                </div> <!-- #et_contact_right -->

                                <div class="clear"></div>


                                <input type="hidden" name="et_eventorm_submit" value="et_contact_proccess" />

                                <input type="reset" id="et_contact_reset" value="<?php esc_attr_e('Reset','Explorable'); ?>" />
                                <input class="et_contact_submit" type="submit" value="<?php esc_attr_e('Submit','Explorable'); ?>" id="et_contact_submit" />

                                <?php wp_nonce_field( 'et-contact-form-submit', '_wpnonce-et-contact-form-submitted' ); ?>
                            </form>
            <?php } ?>
                    </div> <!-- end #et-contact -->

As mentionned above, all the fields are passing into the database (wp_posts table & wp_postmeta).

Only the file to upload doesn't work ! I'm becoming made ! I followed this article to build my form : upload featured image from front end using wordpress add media button

Any suggestion or correction would be appreciated :'(

  • Please post the relevant code inside your question. – Pieter Goosen Feb 25 '16 at 11:04
  • I have put the code, thank you for your support :-) – dvn Feb 25 '16 at 13:03
  • It is like when i submit the form, there is nothing in the file... variable for $file is empty or something like that... Am I missing something ?? – dvn Feb 25 '16 at 17:00
1

I have found the problem.

Problem was in a .js file. This code is causing the problem :

$et_contact_form.live('submit', function() {
    et_contact_error = false;
    et_message = '<ul>';

    $et_inputs.removeClass('et_contact_error');

    $et_inputs.each(function(index, domEle){
        if ( jQuery(domEle).val() === '' || jQuery(domEle).val() === jQuery(this).siblings('label').text() ) {
            jQuery(domEle).addClass('et_contact_error');
            et_contact_error = true;

            var default_value = jQuery(this).siblings('label').text();
            if ( default_value == '' ) default_value = et_ptemplates_strings.captcha;

            et_message += '<li>' + et_ptemplates_strings.fill + ' ' + default_value + ' ' + et_ptemplates_strings.field + '</li>';
        }
        if ( (jQuery(domEle).attr('id') == 'et_contact_email') && !et_email_reg.test(jQuery(domEle).val()) ) {
            jQuery(domEle).removeClass('et_contact_error').addClass('et_contact_error');
            et_contact_error = true;

            if ( !et_email_reg.test(jQuery(domEle).val()) ) et_message += '<li>' + et_ptemplates_strings.invalid + '</li>';
        }
    });

    if ( !et_contact_error ) {
        $href = jQuery(this).attr('action');

        $et_contact_container.fadeTo('fast',0.2).load($href+' #et-contact', jQuery(this).serializeArray(), function() {
            $et_contact_container.fadeTo('fast',1);
        });
    }

    et_message += '</ul>';

    if ( et_message != '<ul></ul>' )
        $et_contact_message.html(et_message);

    return false;
})

And what is causing the trouble exactly :

if ( !et_contact_error ) {
        $href = jQuery(this).attr('action');

        $et_contact_container.fadeTo('fast',0.2).load($href+' #et-contact', jQuery(this).serializeArray(), function() {
            $et_contact_container.fadeTo('fast',1);
        });
    }

I think the fact that this javascript is refreshing the form without complete refresh page is causing that the file is not received by the server via POST methode

Am I right ?

Do you have any clue to solve the JS thing to include the file upload ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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