set-post-thumbnail wp-ajax返回0 [英] set-post-thumbnail wp-ajax return 0
问题描述
我尝试使用wordpress的媒体管理器,我在admin wordpress之外使用帖子编辑器,用户可以创建带有特色图片的帖子. 我使用_wp_post_thumbnail_html函数显示图像或显示要上传文件的链接,所有拥有角色发布者"的用户都可以上传图像并上传工作,但是不起作用显示特色图像或分配给帖子.
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.
关于wp-ajax的操作:set-post-thumbnail返回0,并且未将图像分配给新帖子. wp-ajax.php:
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操作:设置后缩略图
json:true thumbnail_id:3952 _wpnonce:b02e8553f1 action:set-post-thumbnail
响应:0
我的代码为:
<?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>
非常简单,显示来自wordpres的媒体管理器,允许上传特色图片,但不允许分配给新帖子.有解决办法吗?
Very simple, show the media manager from wordpres, allow upload featured image but not allow assign to new post. Any solution?
edit:在编辑帖子中效果很好,允许更改我想的特色图片,因为特色图片需要帖子ID,但是在wordpress的新帖子中,允许上传图片并将其分配给新帖子.
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.
推荐答案
这是正确的图像上传代码.
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();
}
有关Ajax代码,请访问以下链接.如果您有任何问题,请在下面评论.
And for Ajax code, go to this link. If you any problems, then please comment below.
这篇关于set-post-thumbnail wp-ajax返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!