Custom Post Row Actions

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.

2 Answers
2

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="https://wordpress.stackexchange.com/questions/8481/" . get_delete_post_link($post->ID,"', true) . "'>" . __('Logout') . "</a>";
           }else{
             $actions['trash'] = "<a class="submitdelete" title="" . esc_attr(__("Delete this item permanently')) . "' href="https://wordpress.stackexchange.com/questions/8481/" . 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.

Leave a Comment