簡體   English   中英

WordPress:前端表單以創建具有特色圖像的自定義類型帖子

[英]WordPress: Frontend form to create custom type post with featured image

我有以下這段代碼,假設是從fronend創建一個名為:(“ guy_pictures”)的自定義類型的帖子。 所有的代碼看起來都不錯,但是它並沒有在后端創建帖子。

功能:

         function insert_attachment($file_handler,$post_id,$setthumb='false') {
            if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ 
                return __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');

            echo $attach_id = media_handle_upload( $file_handler, $post_id );
            set_post_thumbnail( $post_id , $attach_id);
            //set post thumbnail if setthumb is 1
            if ($setthumb == 1) update_post_meta($post_id,'_thumbnail_id',$attach_id);
            return $attach_id;
        }

短代碼:

        function new_upload_form(){
            $content = '';

            // Check if the form was submitted
            if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

            // Do some minor form validation to make sure there is content
            if (isset ($_POST['title'])) { 
                $title =  $_POST['title']; 
            } else { 
                $content .= 'Please enter a title';
            }
            if (isset ($_POST['description'])) { 
                $description = $_POST['description']; 
            } else { 
                $content .= 'Please enter the content'; 
            }

            // Add the content of the form to $post as an array
            $post = array(
                'post_title'    => $title,
                'post_content'  => $description,
                'post_category' => $_POST['cat'],  // Usable for custom taxonomies too
                'post_status'   => 'draft',         // Choose: publish, preview, future, etc.
                'post_type'     => 'guy_pictures'  // 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);
                        $i++; //count posts
                    }
                } 

            } 


            $content .= '<form id="picture_upload_form" action="" method="post" enctype="multipart/form-data">
                        <p><label for="title">Title</label><br />
                                <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
                                </p>
                                <p><label for="description">Description</label><br />
                                <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
                                </p>

                        <label for="file">Filename:</label>
                        <input type="file" name="file" id="file"><br>
                        <p align="right"><input type="submit" value="Submit" tabindex="6" id="submit" name="submit" /></p>

                                <input type="hidden" name="post_type" id="post_type" value="domande" />
                            <input type="hidden" name="action" value="post" />
                            '.wp_nonce_field( 'new-post' ).'
                        </form>';

            return $content;
        }
        add_shortcode("new_upload", "new_upload_form");

調查: https : //www.advancedcustomfields.com/

它具有自己的acf_form()php函數,可輕松用於創建和編輯任何帖子類型的帖子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM