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

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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 );
    }
});


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x