Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在wordpress中以编程方式上载带有post的照片_Php_Wordpress - Fatal编程技术网

Php 在wordpress中以编程方式上载带有post的照片

Php 在wordpress中以编程方式上载带有post的照片,php,wordpress,Php,Wordpress,我正试图上传照片到wordpress与文章从前端和没有登录。首先,我添加了添加帖子的功能,无需使用插件从wordpress前端上传照片。现在我正试图通过在这个插件上添加一些代码来添加照片上传功能。但在添加代码后,它没有上传照片,但它成功发布了。我不知道哪里是我的错。请帮帮我。我的代码在下面 function guestposts_shortcode( $atts ) { extract ( shortcode_atts (array( 'cat' => '1', '

我正试图上传照片到wordpress与文章从前端和没有登录。首先,我添加了添加帖子的功能,无需使用插件从wordpress前端上传照片。现在我正试图通过在这个插件上添加一些代码来添加照片上传功能。但在添加代码后,它没有上传照片,但它成功发布了。我不知道哪里是我的错。请帮帮我。我的代码在下面

function guestposts_shortcode( $atts ) {
    extract ( shortcode_atts (array(
    'cat' => '1',
    'author' => '1',
    'thanks' => get_bloginfo('home'),
), $atts ) );

return '<form enctype="multipart/form-data" class="guests-post" action="'. plugin_dir_url("guest-posts.php") .'guest-posts/guest-posts-submit.php" method="post">    




<strong>' . __('Post Title:', 'guest-posts') . '</strong><br>
<input type="text" name="title" size="60" required="required" placeholder="' .__('Post title here', 'guest-posts') . '"><br>

 <input type="file" name="upload" id="file"><br>

    <strong>' . __('Story', 'guest-posts') . '</strong>
    '. wp_nonce_field() .'
        <textarea rows="15" cols="72" required="required" name="story" placeholder="' . __('Start writing your post here', 'guest-posts') . '"></textarea><br>
    <strong>' . __('Tags', 'guest-posts') . '</strong><br>
        <input type="text" name="tags" size="60" placeholder="' . __('Comma Separated Tags', 'guest-posts') . '"><br><br>
    <strong>' . __('Your Name', 'guest-posts') . '</strong><br>
        <input type="text" name="author" size="60" required="required" placeholder="' . __('Your name here', 'guest-posts') . '"><br>
    <strong>' . __('Your Email', 'guest-posts') . '</strong><br>
        <input type="email" name="email" size="60" required="required" placeholder="' . __('Your Email Here', 'guest-posts') . '"><br>
    <strong>' . __('Your Website', 'guest-posts') . '</strong><br>
        <input type="text" name="site" size="60" placeholder="' . __('Your Website Here', 'guest-posts') . '"><br><br><br>
    <input type="hidden" value="'. $cat .'" name="category"><input type="hidden" value="'. $author .'" name="authorid">
    <input type="hidden" value="'. $thanks .'" name="thanks">
    <input type="hidden" value="'. str_replace('/wp-content/themes', '', get_theme_root()) .'/wp-blog-header.php" name="rootpath">
    <input type="submit" value="' . __('Submit The Post', 'guest-posts') . '"> <input type="reset" value="' . __('Reset', 'guest-posts') . '"><br>
    </form>
    ';
}
add_shortcode( 'guest-posts', 'guestposts_shortcode' );

使用这个函数要好得多。使用以下功能上载文件并返回附件ID

function guestposts_handle_attachment( $file_handler, $post_id ) {

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

    $attachment_id = media_handle_upload( $file_handler, $post_id );

    if( !is_wp_error( $attachment_id ) ) {

        return $attachment_id;

    }

    return false;

}
然后,当您处理表单时,您可以使用此代码执行函数,并使上载的文件成为特色图像

if( !empty( $_FILES ) ) {

    $attachment_id = guestposts_handle_attachment( 'upload', $post_id );

    if( $attachment_id ) {

        set_post_thumbnail($post_id, $attachment_id);

    }

}
希望有帮助

问候

if( !empty( $_FILES ) ) {

    $attachment_id = guestposts_handle_attachment( 'upload', $post_id );

    if( $attachment_id ) {

        set_post_thumbnail($post_id, $attachment_id);

    }

}