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

I’m trying to add a “Publish” link by the quick actions:

enter image description here

Read More

but I’m not exactly sure of how implement it.

Here’s what I’ve got so far:

add_filter('post_row_actions', function ( $actions )
{
    global $post;

    // If the post hasn't been published yet
    if ( get_post_status($post) != 'publish' )
    {
        $nonce = wp_create_nonce('quick-publish-action');

        $link = get_admin_url('path to where') . "?id={$post->id}&_wpnonce=$nonce";

        $actions['publish'] = "<a href="$link">Publish</a>";
    }

    return $actions;
});

As you can see, I don’t know where to link to in get_admin_url. I think I should be using wp_publish_post(), but I don’t know where to put that code, and how to link to it.

Related posts

1 comment

  1. 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='$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 );
        }
    });
    

Comments are closed.