我有关于这个功能的WordPress文档链接,但没有解释如何使用这个功能。我有一个Ajax函数、PHP和一个HTML输入。在这个HTML中,我设置了标题和内容,还想设置缩略图。也许我可以用这个函数来实现?有什么例子吗?
https://developer.wordpress.org/reference/functions/wp_ajax_set_post_thumbnail/
[EDIT]
我有一个html输入
<input type="file">
我接到一个AJAX电话
$.ajax({
url: Ajax.ajax_url,
type: \'POST\',
data: {
action: \'make_the_post_activity\',
security: Ajax.nonce,
dataType: \'json\'
},
success: function( response ){
}
});
和一个PHP函数
add_action(\'wp_ajax_make_the_post_activity\', \'make_the_post_activity\');
add_action(\'wp_ajax_nopriv_make_the_post_activity\', \'make_the_post_activity\');
function make_the_post_activity(){
if( check_ajax_referer(\'namorada-security-ajax\', \'security\', false) ){
// Here inside I add the the new post calling by ajax the fields as title and content from the HTML file.
}
}
然后在这里我得到了新帖子的ID。但我不知道如何用输入(文件)中的图像更新这个新的帖子id。我知道Ajax不能用XMLHttpRequest上传图像,但我确信还有另一种可能性,我仍然不知道。
我理解这位朋友在下面添加的函数,但在这种情况下,我需要向Ajax函数发送一个JSON,我上传到wordpress库中的图像的ID,但我不知道如何上传它,如何将ID发送到Ajax,然后通过REST API发布它。
[EDIT 2]我将在这里介绍使用Ajax和PHP上传照片所需的完整功能,以及制作新帖子所需的领域,可能需要大量代码。
HTML
<div class="modal-dialog vertical-align-center" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"><?php echo __(\'Escreva seu post\', \'namorada-general\'); ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<textarea class="form-control input_activity_content" id="post_activity_form_emoji" data-emoji-placeholder=":smiley:"></textarea>
</div>
<div style="width:100%;float:left;">
<i class="fa fa-image p-1 upload_photo_activity" style="float:right;font-size:20px;cursor:pointer;"></i>
<i class="fa fa-video-camera p-1 youtube_video_activity" style="float:right;font-size:20px;cursor:pointer;"></i>
</div>
<div style="width:100%;float:left;display:none;" id="youtube_video_activity">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default">YouTube</span>
</div>
<input type="text" class="form-control input_youtube_video_activity" aria-label="Default" aria-describedby="inputGroup-sizing-default">
</div>
</div>
<div style="width:100%;float:left;display:none;" id="upload_photo_activity">
<div class="custom-file">
<input type="file" class="custom-file-input input_upload_photo_activity" id="packagephoto" name="packagephoto">
<label class="custom-file-label choose_photo_activity" for="validatedCustomFile" accept="image/x-png,image/gif,image/jpeg">Escolha sua foto</label>
<div class="invalid-feedback">Example invalid custom file feedback</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo __(\'Cancelar\', \'namorada-general\'); ?></button>
<input id="addimage" name="addimage" class="btn btn-primary make_the_post_activity" value="<?php echo __(\'Publicar\', \'namorada-general\'); ?>">
</div>
</div>
</div>
The AJAX Functions (我有一个用于插入帖子的函数(第一个)和另一个用于将图像插入我自己的数据库(第二个),我不知道如何将此图像与此新帖子的特色图像连接。
[第一个AJAX函数]
$(\'.make_the_post_activity\').live(\'click\', function(){
var post_activity_video_url = $(\'.input_youtube_video_activity\').val();
var post_activity_image_url = $(\'.input_upload_photo_activity\').val();
var post_activity_content = $(\'.input_activity_content\').val();
$.ajax({
url: Ajax.ajax_url,
type: \'POST\',
data: {
action: \'make_the_post_activity\',
security: Ajax.nonce,
dataType: \'json\',
user_id: user_id,
girl_page_author: girl_page_author,
post_activity_video_url: post_activity_video_url,
post_activity_image_url: post_activity_image_url,
post_activity_content: post_activity_content
},
success: function( response ){
if( response.hasOwnProperty(\'status\') ){
alert( response.message );
}
},
error: function( error ){
},
complete: function(){
}
});
});
这个AJAX函数,我有了第一个PHP函数的动作
[PHP Function for the Previous AJAX Function]
add_action(\'wp_ajax_make_the_post_activity\', \'make_the_post_activity\');
add_action(\'wp_ajax_nopriv_make_the_post_activity\', \'make_the_post_activity\');
function make_the_post_activity(){
if( check_ajax_referer(\'namorada-security-ajax\', \'security\', false) ){
$user_id = $_REQUEST[\'user_id\'];
$garota_id = $_REQUEST[\'garota_id\'];
$girl_page_author = $_REQUEST[\'girl_page_author\'];
$post_activity_video_url = $_REQUEST[\'post_activity_video_url\'];
$post_activity_image_url = $_REQUEST[\'post_activity_image_url\'];
$post_activity_content = $_REQUEST[\'post_activity_content\'];
if( is_user_logged_in() && current_user_can(\'publish_posts\') && $user_id == $girl_page_author ){
$args = array(
\'post_author\' => $user_id,
\'post_content\' => $post_activity_content,
\'post_title\' => __(\'Nova postagem de\', \'namorada-general\'),
\'post_type\' => \'post-activity\',
\'post_status\' => \'publish\'
);
$new_post = wp_insert_post( $args );
if( isset($new_post) ){
// Save the YouTube URL when it exists
if( isset($post_activity_video_url) && $post_activity_video_url != \'\' ){
$field_key_video_url_youtube_post_activity = \'field_5f08ca584384d\';
update_field( $field_key_video_url_youtube_post_activity, $post_activity_video_url, $new_post );
}
// Save the Featured Image of the Post when it exists
$field_key_image_url_post_activity = \'field_5f08ca6d4384e\';
update_field( $field_key_image_url_post_activity, $post_activity_image_url, $new_post );
$response = array(
\'status\' => \'success\',
\'message\' => \'Your post was published\'
);
}
} else {
$response = array(
\'status\' => \'fail\',
\'message\' => \'you are not the author\'
);
}
} else {
$response = array(
\'status\' => \'fail\',
\'message\' => \'it is not passing by the ajax referer\'
);
}
wp_send_json( $response );
}
然后,我有了第二个AJAX函数—添加图像(来自HTML输入和其他字段)。
[SECOND AJAX FUNCTION]
$(\'#addimage\').click(function(e) {
var packagename = $(\'#packagename\').val();
var packagephoto_data = $(\'#packagephoto\').prop(\'files\')[0];
var form_data = new FormData();
form_data.append(\'packagephoto_name\', packagephoto_data);
form_data.append(\'packagename\', packagename);
form_data.append(\'action\', \'ng_add_image_ajax\');
$.ajax({
url: Ajax.ajax_url,
type: \'post\',
contentType: false,
processData: false,
data: form_data,
success: function(data){
}
});
});
这个函数将下一个PHP函数作为动作
[PHP FUNCTION FOR THE PREVIOUS AJAX CALL]
function ng_add_image_ajax() {
add_filter( \'upload_dir\', \'ng_upload_dir\' );
global $wpdb;
$tablename = $wpdb->prefix . \'imagepackages\';
$uploadedfile = $_FILES[\'packagephoto_name\'];
$packagephoto_name = $_FILES["packagephoto_name"]["name"];
$upload_overrides = array(
\'test_form\' => false, /* this was in your existing override array */
\'unique_filename_callback\' => \'ng_packagephoto_filename\' // Function for image rename
);
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
if ($movefile && !isset($movefile[\'error\'])) {
$data = array(
\'packagename\' => $_POST[\'packagename\'],
\'packagephoto\' => ng_packagephoto_filename(\'\', $packagephoto_name, \'\'),
\'status\' => 0 );
// FOR database SQL injection security, set up the formats
$formats = array(
\'%s\', // packagename should be an string
\'%s\', // packagephoto should be a integer
\'%d\' // status should be an integer
);
// Actually attempt to insert the data
$insert = $wpdb->insert($tablename, $data, $formats);
if($insert){
echo "<span class=\'text-success\'>The image has been added succefully</span>";
}else{
echo "<span class=\'text-danger\'>The image not added succefully</span>";
}
}else{
echo "<span class=\'text-danger\'>Image Not Upload</span>";
}
remove_filter( \'upload_dir\', \'ng_upload_dir\' );
}
add_action( \'wp_ajax_ng_add_image_ajax\', \'ng_add_image_ajax\' ); // If called from admin panel
add_action( \'wp_ajax_nopriv_ng_add_image_ajax\', \'ng_add_image_ajax\' );
function ng_packagephoto_filename($dir, $filename, $ext){
$newfilename = time() . \'1_\'. $filename;
return $newfilename;
}
function ng_upload_dir( $dirs ) {
$user = wp_get_current_user();
$dirs[\'subdir\'] = \'\';
$dirs[\'path\'] = $dirs[\'basedir\'].\'\';
$dirs[\'url\'] = $dirs[\'baseurl\'].\'\';
return $dirs;
}
再说一遍,这是第二个AJAX函数,使用FormData()添加上传图像。然后有一种方法可以将此formData与媒体库连接,然后将此照片作为帖子的特色图像。
对不起,我知道这样读太多太不愉快了。