2

I came across this question while writing this question. I have one question that extends upon that question.

I figured out that you use the get_delete_post_link filter to create a new url for my actions (or a similar function -- In any case, I'm using that one with the boolean at the end). Only thing is, I don't know how to capture the event now. Some help would be appreciated considering I can't find many examples of post row actions on Google. :-/

public function _wp_filter_get_delete_post_link( $post_id, $deprecated, $force_delete = false )
{
    if ( ! empty( $deprecated ) )
    {
        _deprecated_argument( __FUNCTION__, '3.0.0' );
    }

    $post = &get_post( $post_id );      
    if ( ! $post ) return;

    if ( strcmp($post->post_type, "visitor") ) return;

    $post_type_object = get_post_type_object( $post->post_type );
    if ( !$post_type_object ) return;
    if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) return;
    // http://localhost/wordpress/wp-admin/post.php?post=163&action=trash&_wpnonce=a56abdcbb3

    $action = ( $force_delete ? 'logout' : 'login' );

    $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );

    return wp_nonce_url( $delete_link, "$action-{$post->post_type}_{$post->ID}" );
}

And, if you're needing some more information on how I'm using it, here's some more code:

public function _wp_post_row_actions( $actions, $post )
{
    if ( $post->post_type == 'visitor' )
    {
        //unset( $actions['edit'] );
        unset( $actions['inline hide-if-no-js'] );
        unset( $actions['trash'] );
        unset( $actions['view'] );

        $state = get_post_meta( $post->ID, 'v_state', true ) === 'in';

        if ( $state )
        {
            $actions['trash'] = get_delete_post_link( $post->ID, '', true );    // Logout
            // get_delete_post_link
        }
        else
        {
            $actions['trash'] = get_delete_post_link( $post->ID );  // Login
        }
    }

    return $actions;
}

EDIT: Well, the above isn't complete. I just noticed that it doesn't actually generate a link which is weird. So, I guess what I'm asking is for a way to customize the Post Row Actions for my custom post type by adding a "Logout/Login" link in place of the $actions['trash'] link.

1
  • Did you ever get a working solution to this? Jan 23, 2013 at 12:01

2 Answers 2

6

The main problem is that $actions accepts the whole anchor tag for the link and get_delete_post_link function outputs just the url.

So if You are just looking to replace the label "Trash" then you can use the post_row_actions filter hook with a simple function

Something like this

    add_filter( 'post_row_actions','my_action_row', 10, 2 );
    
    function my_action_row( $actions, $post ){
       if ($post->post_type =="visitor"){
          //remove what you don't need
           unset( $actions['inline hide-if-no-js'] );
           unset( $actions['trash'] );
           unset( $actions['view'] );
           //check capabilites
           $post_type_object = get_post_type_object( $post->post_type );
           if ( !$post_type_object ) return;
           if ( !current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) return;
    
          //the get the meta and check
           $state = get_post_meta( $post->ID, 'v_state', true );
           if ($state == 'in'){
             $actions['trash'] = "<a class='submitdelete' title='" . esc_attr(__('Delete this item permanently')) . "' href='" . get_delete_post_link($post->ID, '', true) . "'>" . __('Logout') . "</a>";
           }else{
             $actions['trash'] = "<a class='submitdelete' title='" . esc_attr(__('Delete this item permanently')) . "' href='" . get_delete_post_link($post->ID, '', true) . "'>" . __('Login') . "</a>";
    
    
           }
       }
       return $actions;
    }

This will generate a logout if the meta is "IN" and login if other wise but both links will delete the post. and i would add a check to see if the post is already in the trash. But its a start and i hope it helps.

3
  • 1
    Now what about capturing the events from the links? How do you do that?
    – Zack
    Feb 4, 2011 at 15:45
  • you can link to your own functions if you want instead of the delete_post_link
    – Bainternet
    Feb 4, 2011 at 15:50
  • 1
    Erm. Brainfart I'm having. :-/ How exactly do I link to my own function?
    – Zack
    Feb 4, 2011 at 16:04
0

You can link to your own function like below. I'm passing a url parameter along with the post id, then I'm seeing if that parameter is set, and calling a function based on that. If you want the logout url instead of the custom url, try replacing the anchor with this... <a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Logout</a>. Obviously you'd have to login to see that 'logout' link, hence I don't see a point in putting a login link over there!

add_action('post_row_actions', 'testing');
function testing( $actions, $post )
{
if ( $post->post_type == 'visitor' )
{
    //unset( $actions['edit'] );
    unset( $actions['inline hide-if-no-js'] );
    unset( $actions['trash'] );
    unset( $actions['view'] );

    //Adding a custom link and passing the post id with it
    $actions['customedit'] = '<a href=\''.admin_url('?action=test&post='.$post->ID).'\' target=\'blank\'>Test</a>';
}
return $actions;
}

//Now to get the 'test' argument, and triggering a function based on it...

if($_GET['action'] == 'test')
{
$post_to_print = $_GET['post'];
print_post($post_to_print);
}

function print_post($id)
{
$my_post = get_post($id);

echo "<pre>";
print_r($my_post);
echo "</pre>";
}

Let me know if it worked!

Thanks, Rutwick

4
  • The block of code checking the $_GET['action'] is incorrect? Nov 20, 2012 at 13:43
  • $post argument does not exists, your code doesn't work.
    – jepser
    Jun 1, 2013 at 4:07
  • It's an old piece of code! Check BAInternet's answer above. Jun 1, 2013 at 5:40
  • 1
    jepser I think you just need to pass the accepted args, so the $post can be catched, try use add_action('post_row_actions', 'testing', 10, 2); May 7, 2015 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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