How to add a “publish” link to the quick actions

Without doing Ajax (like in Quick Edit), the admin_url should be the very edit.php page.

Note that:

  • the filter post_row_actions takes two arguments, the second one being $post, so the global is not necessary.
  • instead of using id as query argument, it’s best practice to use custom names, in this case update_id.
  • I didn’t know the function get_admin_url and normally use admin_url for this.
add_filter( 'post_row_actions', function ( $actions, $post )
{
    if ( get_post_status( $post ) != 'publish' )
    {
        $nonce = wp_create_nonce( 'quick-publish-action' ); 
        $link = admin_url( "edit.php?update_id={$post->ID}&_wpnonce=$nonce" );
        $actions['publish'] = "<a href="https://wordpress.stackexchange.com/questions/99211/$link">Publish</a>";
    }   
    return $actions;
},
10, 2 );

Then, we need to hook in a very early action, load-edit.php, and perform a wp_update_post if the nonce and update_id are ok:

add_action( 'load-edit.php', function() 
{
    $nonce = isset( $_REQUEST['_wpnonce'] ) ? $_REQUEST['_wpnonce'] : null;
    if ( wp_verify_nonce( $nonce, 'quick-publish-action' ) && isset( $_REQUEST['update_id'] ) )
    {
        $my_post = array();
        $my_post['ID'] = $_REQUEST['update_id'];
        $my_post['post_status'] = 'publish';
        wp_update_post( $my_post );
    }
});

Leave a Comment