Publish pending article from front end with a button?

First create a function that will print the publish button :

//function to print publish button
function show_publish_button(){
    Global $post;
    //only print fi admin
    if (current_user_can('manage_options')){
        echo '<form name="front_end_publish" method="POST" action="">
                <input type="hidden" name="pid" id="pid" value="'.$post->ID.'">
                <input type="hidden" name="FE_PUBLISH" id="FE_PUBLISH" value="FE_PUBLISH">
                <input type="submit" name="submit" id="submit" value="Publish">
            </form>';
    }
}

Next create a function to change the post status:

//function to update post status
function change_post_status($post_id,$status){
    $current_post = get_post( $post_id, 'ARRAY_A' );
    $current_post['post_status'] = $status;
    wp_update_post($current_post);
}

Then make sure to catch the submit of the button:

if (isset($_POST['FE_PUBLISH']) && $_POST['FE_PUBLISH'] == 'FE_PUBLISH'){
    if (isset($_POST['pid']) && !empty($_POST['pid'])){
        change_post_status((int)$_POST['pid'],'publish');
    }
}

Now all of the above code can go in your theme’s functions.php file and all you have to do is add show_publish_button(); in your loop of pending posts after each post.

Leave a Comment