Replace image name on upload to the new post name on front-end form
Question
I’m using this form to replace featured images and post title on my frontend form:
<form id="featured_upload" name="featured_upload" method="post" action="#" enctype="multipart/form-data">
<p><label for="title">Title</label><input type="text" id="title" value="<?php echo $post_to_edit->post_title; ?>" size="45" name="title" /></p>
<input type="file" name="my_image_upload" id="my_image_upload" multiple="false" required />
<p><input type="hidden" name="post_id" id="post_id" value="" /></p>
<?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
<input id="" name="" type="submit" value="Upload"/>
</form>
Here’s 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'] )) {
//changing the post title
if(!empty($_POST['title'])) {
//$new_title = sanitize_title($_POST['title']);
$new_slug = sanitize_title($_POST['title']);
$my_post = array('post_title' => $_POST['title'],'post_name' => $new_slug);
// Update new post title into the database
wp_update_post( $my_post );
The problem:
I need the uploaded picture to be saved with the new post title ($_POST['title']
)
Example:
The chosen image is image1.jpg
The new post name is renamed post
(just like on $_POST['title']
)
On the upload process, image1.jpg
should be renamed to renamed-post.jpg
0
front-end, posts, slug, title
4 years
2019-11-29T03:59:08-05:00
2019-11-29T03:59:08-05:00 0 Answers
75 views
0
Leave an answer