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. :-/

Read More
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.

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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