replace featured image from frontend

Question

I need to replace the featured image of a post in the frontend. I’am using this code without success:

<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
    <input type="file" name="my_image_upload" id="my_image_upload"  multiple="false" />
    <input type="hidden" name="post_id" id="post_id" value="55" />
    <?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
    <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>  

    <?php

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

    include('wp-admin/includes/image.php' );
    include('wp-admin/includes/file.php' );
    include('wp-admin/includes/media.php' );

    if (has_post_thumbnail( $_POST['post_id'] )) {
    $featured_image = wp_get_attachment_image_src(get_post_thumbnail_id(  $_POST['post_id'] ), 'full' );
    wp_delete_attachment($featured_image, true); 
    }
    $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

What am’I missing?

0
, Alex 1 year 0 Answers 61 views 0

Leave an answer