Php 在wordpress中从前端添加功能图像

Php 在wordpress中从前端添加功能图像,php,jquery,html,wordpress,Php,Jquery,Html,Wordpress,如何从前端WordPress在自定义文章类型中添加特征图像。有人能帮忙吗?我试过了,但没有得到正确的结果。请帮忙 <?php if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) { global $wpdb; if ( tr

如何从前端WordPress在自定义文章类型中添加特征图像。有人能帮忙吗?我试过了,但没有得到正确的结果。请帮忙

 <?php
 if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {
 global $wpdb;
    if ( trim( $_POST['postTitle'] ) === '' ) {
        $postTitleError = 'Please enter a title.';
        $hasError = true;
    }

    $post_information = array(
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => $_POST['postContent'],
        'post_type' => 'stories',
        'post_status' => 'publish'
    );

 echo  $post_id =  wp_insert_post( $post_information );
    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
    }
    }
 }
 ?>

<div class="mid-wrapper">

<form action="" id="primaryPostForm" method="POST">

    <fieldset>
        <label for="postTitle"><?php _e('Post Title:', 'framework') ?></label>

        <input type="text" name="postTitle" id="postTitle" class="required" />
    </fieldset>

    <fieldset>
        <label for="postContent"><?php _e('Post Content:', 'framework') ?></label>

        <textarea name="postContent" id="postContent" rows="8" cols="30" class="required"></textarea>
    </fieldset>
 <label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>

    <fieldset>
        <input type="hidden" name="submitted" id="submitted" value="true" />
 <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
        <button type="submit"  name="submitted"><?php _e('Add Post', 'framework') ?></button>
    </fieldset>

</form>

</div>  
    <?php 

    // 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 { 
        echo 'Please enter a title';
    }
    if (isset ($_POST['description'])) { 
        $description = $_POST['description']; 
    } else { 
        echo 'Please enter the content'; 
    }

    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $title,
        'post_content'  => $description,

        'post_status'   => 'draft',         // Choose: publish, preview, future, etc.
        'post_type'     => 'stories'  // Use a custom post type if you want to
    );
    echo $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
        }
        } 


    //attachment helper function   

                // http://codex.wordpress.org/Function_Reference/wp_insert_post
        //wp_redirect( '/submit-2/' ); // redirect to home page after submit

    } // end IF
    // Do the wp_insert_post action to insert it
    ?>


    <div class="mid-wrapper">

    <form action="" method="post" enctype="multipart/form-data">
    <p><label for="title">Titolo</label><br />
            <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
            </p>
            <p><label for="description">Testo</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="Invia" 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" />
        <?php wp_nonce_field( 'new-post' ); ?>
    </form>

    </div>

add this funcion in functions.php
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 if setthumb is 1
if ($setthumb == 1) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
    }

文件名:

如何从前端WordPress在自定义文章类型中添加特征图像。有人能帮忙吗?我试过了,但没有得到正确的结果。请帮忙。


 <?php
 if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {
 global $wpdb;
    if ( trim( $_POST['postTitle'] ) === '' ) {
        $postTitleError = 'Please enter a title.';
        $hasError = true;
    }

    $post_information = array(
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => $_POST['postContent'],
        'post_type' => 'stories',
        'post_status' => 'publish'
    );

 echo  $post_id =  wp_insert_post( $post_information );
    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
    }
    }
 }
 ?>

<div class="mid-wrapper">

<form action="" id="primaryPostForm" method="POST">

    <fieldset>
        <label for="postTitle"><?php _e('Post Title:', 'framework') ?></label>

        <input type="text" name="postTitle" id="postTitle" class="required" />
    </fieldset>

    <fieldset>
        <label for="postContent"><?php _e('Post Content:', 'framework') ?></label>

        <textarea name="postContent" id="postContent" rows="8" cols="30" class="required"></textarea>
    </fieldset>
 <label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>

    <fieldset>
        <input type="hidden" name="submitted" id="submitted" value="true" />
 <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
        <button type="submit"  name="submitted"><?php _e('Add Post', 'framework') ?></button>
    </fieldset>

</form>

</div>  
    <?php 

    // 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 { 
        echo 'Please enter a title';
    }
    if (isset ($_POST['description'])) { 
        $description = $_POST['description']; 
    } else { 
        echo 'Please enter the content'; 
    }

    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $title,
        'post_content'  => $description,

        'post_status'   => 'draft',         // Choose: publish, preview, future, etc.
        'post_type'     => 'stories'  // Use a custom post type if you want to
    );
    echo $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
        }
        } 


    //attachment helper function   

                // http://codex.wordpress.org/Function_Reference/wp_insert_post
        //wp_redirect( '/submit-2/' ); // redirect to home page after submit

    } // end IF
    // Do the wp_insert_post action to insert it
    ?>


    <div class="mid-wrapper">

    <form action="" method="post" enctype="multipart/form-data">
    <p><label for="title">Titolo</label><br />
            <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
            </p>
            <p><label for="description">Testo</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="Invia" 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" />
        <?php wp_nonce_field( 'new-post' ); ?>
    </form>

    </div>

add this funcion in functions.php
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 if setthumb is 1
if ($setthumb == 1) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
    }
蒂托洛

Testo

文件名:

在functions.php中添加此函数 函数insert\u attachment($file\u handler,$post\u id,$setthumb='false')){ 如果($\u FILES[$file\u handler]['error']!==UPLOAD\u ERR\u OK){return\u return\u 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\u id=media\u handle\u upload($file\u handler,$post\u id); //如果setthumb为1,则设置post缩略图 如果($setthumb==1)更新发布元数据($post\u id、“'u thumbnail\u id'、$attach\u id); 返回$attach_id; }
Lol重复上面已经说过的话不会有什么帮助,最好正确地解释一下,这样人们就会明白你想做什么。