get_edit_post_link()
云策文档标注
概述
get_edit_post_link() 函数用于获取指定文章的编辑链接,支持多种文章类型,并可根据用户权限和上下文参数调整输出。
关键要点
- 函数返回文章编辑链接的字符串,若文章类型不存在或无编辑界面则返回 null
- 参数 $post 可选,默认为全局 $post,可接受文章 ID 或 WP_Post 对象
- 参数 $context 可选,默认为 'display',控制 '&' 字符的编码方式
- 函数内部检查用户权限(current_user_can('edit_post')),无权限时返回 null
- 支持文章、页面、附件、修订、全局样式、模板和模板部件等多种文章类型
- 可通过 get_edit_post_link 过滤器钩子自定义编辑链接
代码示例
// 获取当前文章的编辑链接
$edit_link = get_edit_post_link();
// 获取指定文章 ID 的编辑链接,并设置上下文为 'raw'
$edit_link = get_edit_post_link( 123, 'raw' );
// 使用过滤器隐藏非管理员的编辑链接
add_filter( 'get_edit_post_link', function( $link ) {
if ( ! current_user_can( 'administrator' ) ) {
return null;
}
return $link;
});注意事项
- 函数在用户未登录或无编辑权限时返回 null,不适合在后台任务等无用户上下文中使用
- $context 参数为 'display' 时编码 '&' 为 &,其他值则输出原始 '&'
- 对于 wp_template 和 wp_template_part 类型,链接生成涉及样式表和文章名称的编码
原文内容
Retrieves the edit post link for post.
Description
Can be used within the WordPress loop or outside of it. Can be used with pages, posts, attachments, revisions, global styles, templates, and template parts.
Parameters
$postint|WP_Postoptional-
Post ID or post object. Default is the global
$post. $contextstringoptional-
How to output the
'&'character. Default'&'.
Source
function get_edit_post_link( $post = 0, $context = 'display' ) {
$post = get_post( $post );
if ( ! $post ) {
return;
}
if ( 'revision' === $post->post_type ) {
$action = '';
} elseif ( 'display' === $context ) {
$action = '&action=edit';
} else {
$action = '&action;=edit';
}
$post_type_object = get_post_type_object( $post->post_type );
if ( ! $post_type_object ) {
return;
}
if ( ! current_user_can( 'edit_post', $post->ID ) ) {
return;
}
$link = '';
if ( 'wp_template' === $post->post_type || 'wp_template_part' === $post->post_type ) {
$slug = urlencode( get_stylesheet() . '//' . $post->post_name );
$link = admin_url( sprintf( $post_type_object->_edit_link, $post->post_type, $slug ) );
} elseif ( 'wp_navigation' === $post->post_type ) {
$link = admin_url( sprintf( $post_type_object->_edit_link, (string) $post->ID ) );
} elseif ( $post_type_object->_edit_link ) {
$link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
}
/**
* Filters the post edit link.
*
* @since 2.3.0
*
* @param string $link The edit link.
* @param int $post_id Post ID.
* @param string $context The link context. If set to 'display' then ampersands
* are encoded.
*/
return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );
}
Hooks
- apply_filters( ‘get_edit_post_link’, string $link, int $post_id, string $context )
-
Filters the post edit link.
Skip to note 5 content
Ahmed El-Atab
// Hide the Edit Post Link from Non Administrators Start. function wpdocs_remove_get_edit_post_link( $link ) { if ( current_user_can( 'administrator' ) ) { return $link; } return null; } add_filter( 'get_edit_post_link', 'wpdocs_remove_get_edit_post_link' ); // Hide the Edit Post Link from Non Administrators End.Skip to note 6 content
jakeparis
These docs are confusing about the second parameter.
$contextdefaults to “display”, which means the ampersand is encoded with&action=edit. If you pass any other value (doesn’t matter what), the ampersond is not encoded, so the return value is&action;=editSkip to note 7 content
Guilherme Carvalho
The default output is not ‘&’ for the second parameter. It’s actually ‘&’, this can cause issues depending on your environment when used directly in the browser.
Anything else than ‘display’ in the $context param will display the correct symbol instead of the HTML entity for it.
Skip to note 8 content
Anthony Eden
It’s not obvious unless you read the code for this function, but this function does not return a result if you’re calling it outside the context of a logged-in user. For example, you cannot run this in a background task.
This is due to this line:
current_user_can( 'edit_post', $post->ID )