【问题标题】:Wordpress Frontend Post Form - Image upload should not be mandatoryWordpress 前端发布表单 - 图片上传不应该是强制性的
【发布时间】:2016-10-28 19:04:32
【问题描述】:

我终于设法让 wordpress 前端表单正常工作,创建自定义帖子类型帖子并填写各种自定义字段。但是由于我集成了媒体上传,因此表单提交仅在附加文件时才有效。但是媒体上传不应该是强制性的,它应该是可选的。欢迎提出任何建议!

// Formular Verloren
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

$post_information = array(
'post_type' => 'verloren',
);

$postID = wp_insert_post( $post_information ); //here's the catch

    //Image Upload
    if (!function_exists('wp_generate_attachment_metadata')){
                         require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                         require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                         require_once(ABSPATH . "wp-admin" . '/includes/media.php');
                 }
                    if ($_FILES) {
                         foreach ($_FILES as $file => $array) {
                                 if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                                         return "upload error : " . $_FILES[$file]['error'];
                                 }
                                 $attach_id = media_handle_upload( $file, $postID );
                         }
                 }
                 if ($attach_id > 0){
                         //and if you want to set that image as Post  then use:
                         update_post_meta($postID,'_thumbnail_id',$attach_id);
                 }


    //Custom Field Updates 
    update_field('field_580a640d6e458', $_POST['brand'], $postID);
    update_field('field_580a644ddc37f', $_POST['modell'], $postID);
    update_field('field_580a60c14f201', $_POST['date'], $postID);
    update_field('field_580a6191738cd', $_POST['spot'], $postID);
    ...


    //Redirect to Page
    wp_redirect(get_permalink(100)); exit;
}

这是我的提交按钮,如果相关的话:

<button id="submit"  class="btn" name="submit" type="submit" value="Publish">Submit</button>
<input name="action" type="hidden" value="new_post" />

【问题讨论】:

    标签: php wordpress forms wordpress-theming


    【解决方案1】:

    解决了。也许不是最好的代码,但它对我有用:

    // Formular Verloren
    if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
    
    $post_information = array(
    'post_type' => 'verloren',
    );
    
    $postID = wp_insert_post( $post_information ); //here's the catch
    
        //Field Updates Verloren Post
        update_field('field_580a640d6e458', $_POST['brand'], $postID);
        update_field('field_580b7f49ed5dc', $_POST['mail'], $postID);
    
        //require the needed files
                require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
        //then loop over the files that were sent and store them using  media_handle_upload();
                if ($_FILES) {
                        foreach ($_FILES as $file => $array) {
                                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                                        wp_redirect(get_permalink(100)); exit;
                                        die();
                                }
                                $attach_id = media_handle_upload( $file, $postID );
                        }
                }
    
        //and if you want to set that image as Post  then use:
            update_post_meta($postID,'_thumbnail_id',$attach_id);
            wp_redirect(get_permalink(100)); exit;
            die();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-07
      • 2017-11-29
      • 2019-08-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 2015-10-12
      相关资源
      最近更新 更多