post_row_actions
云策文档标注
概述
post_row_actions 是一个 WordPress 过滤器,用于修改文章列表表格中的行操作链接数组。该过滤器仅适用于非层级文章类型,允许开发者自定义或添加操作链接。
关键要点
- 过滤器名称:post_row_actions,用于过滤文章列表的行操作链接。
- 适用对象:仅针对非层级文章类型(如标准文章),层级文章类型应使用 page_row_actions。
- 参数:$actions(操作链接数组,默认包含编辑、快速编辑、恢复、移至回收站、永久删除、预览和查看等链接)和 $post(文章对象)。
- 用法:通过 add_filter 钩子调用,可基于文章类型或用户权限修改链接。
代码示例
add_filter( 'post_row_actions', 'modify_list_row_actions', 10, 2 );
function modify_list_row_actions( $actions, $post ) {
// 检查自定义文章类型
if ( $post->post_type == "my_custom_post_type" ) {
// 构建自定义链接URL
$url = admin_url( 'admin.php?page=mycpt_page&post=' . $post->ID );
$edit_link = add_query_arg( array( 'action' => 'edit' ), $url );
// 保存默认的回收站链接
$trash = $actions['trash'];
// 重置操作数组,添加自定义编辑链接
$actions = array(
'edit' => sprintf( '%2$s',
esc_url( $edit_link ),
esc_html( __( 'Edit', 'contact-form-7' ) ) )
);
// 检查用户权限并添加复制链接
if ( current_user_can( 'edit_my_cpt', $post->ID ) ) {
$copy_link = wp_nonce_url( add_query_arg( array( 'action' => 'copy' ), $url ), 'edit_my_cpt_nonce' );
$actions = array_merge( $actions, array(
'copy' => sprintf( '%2$s',
esc_url( $copy_link ),
'Duplicate'
)
) );
// 重新插入回收站链接
$actions['trash'] = $trash;
}
}
return $actions;
}注意事项
- 对于层级自定义文章类型,应使用 page_row_actions 过滤器而非 post_row_actions。
- 操作链接数组可基于文章类型、状态或用户权限进行动态调整,确保安全性和功能性。
原文内容
Filters the array of row action links on the Posts list table.
Description
The filter is evaluated only for non-hierarchical post types.
Parameters
$actionsstring[]-
An array of row action links. Defaults are
'Edit', ‘Quick Edit’,'Restore','Trash', ‘Delete Permanently’,'Preview', and'View'. $postWP_Post-
The post object.
Source
$actions = apply_filters( 'post_row_actions', $actions, $post );
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 3 content
Aurovrata Venet
Here is an example of how to modify the quick links in your custom post type admin table list,
add_filter( 'post_row_actions', 'modify_list_row_actions', 10, 2 ); function modify_list_row_actions( $actions, $post ) { // Check for your post type. if ( $post->post_type == "my_custom_post_type" ) { // Build your links URL. $url = admin_url( 'admin.php?page=mycpt_page&post;=' . $post->ID ); // Maybe put in some extra arguments based on the post status. $edit_link = add_query_arg( array( 'action' => 'edit' ), $url ); // The default $actions passed has the Edit, Quick-edit and Trash links. $trash = $actions['trash']; /* * You can reset the default $actions with your own array, or simply merge them * here I want to rewrite my Edit link, remove the Quick-link, and introduce a * new link 'Copy' */ $actions = array( 'edit' => sprintf( '<a href="%1$s">%2$s</a>', esc_url( $edit_link ), esc_html( __( 'Edit', 'contact-form-7' ) ) ) ); // You can check if the current user has some custom rights. if ( current_user_can( 'edit_my_cpt', $post->ID ) ) { // Include a nonce in this link $copy_link = wp_nonce_url( add_query_arg( array( 'action' => 'copy' ), $url ), 'edit_my_cpt_nonce' ); // Add the new Copy quick link. $actions = array_merge( $actions, array( 'copy' => sprintf( '<a href="%1$s">%2$s</a>', esc_url( $copy_link ), 'Duplicate' ) ) ); // Re-insert thrash link preserved from the default $actions. $actions['trash']=$trash; } } return $actions; }Skip to note 4 content
tylercollier
If you are using a custom post type, and it’s hierarchical, you should instead use the filter
page_row_actions