3

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.

3

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 );
    }
});
  • Thank you for the response. Passing a second argument into the filter function gives me an error that it's missing argument two (running WP 3.5.1 on PHP 5.3). – MegaHit May 13 '13 at 3:54
  • Also, any reason you're not simply using wp_publish_post($_REQUEST['update_id'])? That's 1 line of code instead of 4. – MegaHit May 13 '13 at 3:55
  • Have you seen priority, arguments / 10, 2 in add_filter('hook',function(){}, 10, 2)? Donnow, never used wp_publish_post, what do your tests reveal? – brasofilo May 13 '13 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.