Custom Post Row Actions

时间:2011-02-03 作者:Zack

我偶然发现this question 在写这个问题的时候。我有一个问题是关于这个问题的。

我发现你用的是get_delete_post_link 筛选为我的操作创建一个新的url(或一个类似的函数——在任何情况下,我都会将该函数与布尔值一起使用)。唯一的问题是,I don\'t know how to capture the event now. 考虑到我在谷歌上找不到很多关于行后操作的例子,我将不胜感激-/

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}" );
}
如果您需要有关我如何使用它的更多信息,请参阅以下代码:

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: 嗯,上述内容并不完整。我只是注意到它实际上并没有生成一个奇怪的链接。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 个回复
最合适的回答,由SO网友:Bainternet 整理而成

主要问题是$actions接受链接的整个锚定标记,而get\\u delete\\u post\\u link函数只输出url。

因此,如果您只是想更换标签;“垃圾”;然后,可以通过一个简单的函数使用post\\u row\\u actions过滤器挂钩

像这样的

    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;
    }
如果meta为“0”,则会生成注销;在“中”;和登录,如果其他明智的,但这两个链接将删除帖子。我会加上一个检查,看看帖子是否已经在垃圾桶里了。但这只是一个开始,我希望这会有所帮助。

SO网友:Rutwick Gangurde

您可以像下面这样链接到自己的函数。我将传递一个url参数和post id,然后查看是否设置了该参数,并基于该参数调用一个函数。如果您想要注销url而不是自定义url,请尝试用以下内容替换锚点。。。<a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Logout</a>. 显然,你必须登录才能看到“注销”链接,因此我认为在那里放置登录链接没有任何意义!

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>";
}
让我知道它是否有效!

谢谢Rutwick

结束

相关推荐