0

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?

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
            )
        );
    }
}
  • 1
    in $_POST['post_id'] which id you get ? you have to pass post id in value of name="post_id" which you want to update. currently it is blank that's why it is creating new post. – Chetan Vaghela Nov 28 at 13:22
  • 1
    Thanks! Since my form submission is handling the current post, all I have to to was to remove 'ID' => $_POST['post_id'],. – bpy Nov 28 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.