admin管理员组

文章数量:1310081

I have this form that allows-me to change some things of the current post in my frontend:

<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'] )) {

What I would like to do is to have the habitability of changing the post title (and slug also, of course...).

Can this be implemented on this form?

I have this form that allows-me to change some things of the current post in my frontend:

<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'] )) {

What I would like to do is to have the habitability of changing the post title (and slug also, of course...).

Can this be implemented on this form?

Share Improve this question edited Nov 28, 2019 at 12:14 Chetan Vaghela 2,3984 gold badges10 silver badges16 bronze badges asked Nov 28, 2019 at 11:29 bpybpy 2995 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Yes, you can change post title and slug from frontend. by using wp_update_post you can change title and slug of post. in below code, it will update post title and slug of post_id. Replace your-post-title-field with your title field in form. slug will generated using that post title.

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
            )
        );
    }
}

本文标签: slugEdit the post title from the frontend