dongtuo5262 2016-01-22 01:51
浏览 62

set-post-thumbnail wp-ajax返回0

i try use the media manager from wordpress i use the post editor outside admin wordpress and users can create a posts whit a featured image. I use _wp_post_thumbnail_html function to show image or show link to upload file, all users whit a rol "publisher" can upload images and upload work but doesn't work show featured image or assign to post.

on wp-ajax whit action: set-post-thumbnail returns 0 and image aren't assign to new post. wp-ajax.php:

json:true thumbnail_id:3952 _wpnonce:b02e8553f1 action:set-post-thumbnail

response: 0

My code as:

<?php $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
                        //$postid =  get_post( $post_id );
                        echo _wp_post_thumbnail_html( $thumbnail_id, $post_id );
                    ?>
                    <br>

Very simple, show the media manager from wordpres, allow upload featured image but not allow assign to new post. Any solution?

edit: in edit post works fine allow change featured image i suppose because featured image required a post id, but on new post from wordpress allow upload image and asign this to new post.

  • 写回答

1条回答 默认 最新

  • duanmaifu3428 2016-01-22 06:05
    关注

    This is proper image-uploading code.

    <?php
    global $wpdb;
    include ('../../../../wp-load.php');
    if ( $_FILES ) {
    
        if(!function_exists('wp_handle_upload')){
            require_once(ABSPATH . 'wp-admin/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        }
    
    
    
        $upload_overrides = array('test_form' => false);
    
        $response = array();
        ini_set('max_execution_time', 0);
        foreach($_FILES as $file){
    
            $movefile_profile = wp_handle_upload($file, $upload_overrides);
            $filename = $movefile_profile['url'];
            $parent_post_id = $post_id; // please provide post id must
            $filetype = wp_check_filetype( basename( $filename ), null );
            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
                'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                'post_mime_type' => $filetype['type'],
                'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                'post_content'   => '',
                'post_status'    => 'inherit'
            );
    
            $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
            $filepath = $wp_upload_dir['path'] . '/' . basename( $filename );
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filepath );
            wp_update_attachment_metadata( $attach_id, $attach_data );
            set_post_thumbnail($post_id, $attach_id); // this set_post_thumbnail will set you post thumbnail
            $response['message'] = 'Done';
        }
    
        echo json_encode($response);
        die();
    
    }
    

    And for Ajax code, go to this link. If you any problems, then please comment below.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么