我有这个表格,可以在我的前端更改当前帖子的一些内容:
<form id="featured_upload" name="featured_upload" method="post" action="#" enctype="multipart/form-data">
<p><span class="badge badge-primary"><input type="file" name="my_image_upload" id="my_image_upload" multiple="false" required /></span></p>
<p><input type="text" name="newtags" id="newtags" value="" placeholder="new tags aqui..." /> <input type="text" name="h2" id="h2" value="" placeholder="h2 heading aqui..." /></p>
<p><input type="hidden" name="post_id" id="post_id" value="" /></p>
<?php wp_nonce_field( \'my_image_upload\', \'my_image_upload_nonce\' ); ?>
<span class="badge badge-light" ><input id="" name="" type="submit" value="Upload"/></span>
</form>
Starting the saving process...
// Check the nonce is valid, and the user can edit this post.
if ( isset( $_POST[\'my_image_upload_nonce\'], $_POST[\'post_id\'] )
&& wp_verify_nonce( $_POST[\'my_image_upload_nonce\'], \'my_image_upload\' )
&& current_user_can( \'manage_options\', $_POST[\'post_id\'] )) {
我想做的是改变帖子标题(当然还有slug…)的宜居性。
是否可以在此表单上执行此操作?
最合适的回答,由SO网友:Chetan Vaghela 整理而成
是的,您可以从前端更改帖子标题和slug。通过使用wp_update_post
你可以更改文章的标题和段落。在下面的代码中,它将更新帖子标题和slugpost_id
. 代替your-post-title-field
在表单中显示标题字段。slug将使用该帖子标题生成。
if ( isset( $_POST[\'my_image_upload_nonce\'], $_POST[\'post_id\'] )
&& wp_verify_nonce( $_POST[\'my_image_upload_nonce\'], \'my_image_upload\' )
&& current_user_can( \'manage_options\', $_POST[\'post_id\'] )) {
if(!empty($_POST[\'your-post-title-field\']))
{
$new_slug = $new_title = sanitize_title($_POST[\'your-post-title-field\']);
wp_update_post(
array (
\'ID\' => $_POST[\'post_id\'],
\'post_title\' => $new_title,
\'post_name\' => $new_slug
)
);
}
}