从前台表单上传图片作为特色图片

时间:2016-02-25 作者:dvn

我在前端页面上构建了一个小表单。

我希望用户在特定种类的帖子(列表)中创建新帖子。

所有字段都正常,但只有上载文件不起作用。你能告诉我[这个代码]有什么问题吗

以下是完整代码:

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 -->
如上所述,所有字段都传递到数据库中(wp\\u posts表和wp\\u posttea)。

只有要上载的文件无效!我正在变得成熟!我按照这篇文章构建了我的表单:upload featured image from front end using wordpress add media button

如有任何建议或更正,将不胜感激:\'(

1 个回复
SO网友:dvn

我发现了问题。

问题出在a中。js文件。此代码导致以下问题:

$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;
})
到底是什么导致了问题:

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);
        });
    }
我认为,这个javascript在没有完全刷新页面的情况下刷新表单的事实导致服务器无法通过POST方法接收文件

我说得对吗?

你有什么线索可以解决JS问题,包括文件上传?

相关推荐

Wordpress cutting images size

我目前正在制作一个页面的横幅,过了一段时间,我注意到我的图像质量比我预期的稍差一些。我上传了1920x1024大小的图片,但在我查看管理仪表板后,我注意到它们已经缩小到1024x575!这有什么原因吗?有什么办法可以防止这种情况发生吗?提前感谢!