'Change post status to draft (on button click - front-end) with wp_update_post()

I'm adding a button via shortcode into my post on front-end. On click, this button should change the current post status to 'draft'.

Changing to 'draft' seems to work, but it happens as soon as the post is loaded, not on the button click. Where am I going wrong?

Here is my code:

function post_to_draft_button_shortcode() {
    global $post, $current_user;
   
    $post_id = get_queried_object_id();
   
    if ( $post->post_author == $current_user->ID ) {
    ob_start();
    ?>
    <div class="button-draft">
        <a onclick="return confirm('Change this post to draft, sure?')" href="<?php echo wp_update_post(array('ID' => $post_id, 'post_status'  =>  'draft')) ?>">Save as draft</a>
    </div>
    <?php
    $output = ob_get_clean();
    }
    return $output;
   
}
add_shortcode( 'post-to-draft', 'post_to_draft_button_shortcode' ); 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source