get_sample_permalink_html()
云策文档标注
概述
get_sample_permalink_html() 函数用于生成文章或页面固定链接(permalink)的示例 HTML 编辑器界面。它根据文章状态、用户权限和固定链接设置,动态生成包含预览链接和编辑选项的 HTML 输出。
关键要点
- 函数接受三个参数:$post(必需,文章 ID 或 WP_Post 对象)、$new_title(可选,新标题)、$new_slug(可选,新 slug)。
- 返回字符串,即示例固定链接 slug 编辑器的 HTML 代码。
- 内部调用 get_sample_permalink() 获取基础链接和 post_name,并根据文章状态(如草稿、已发布)和用户权限(如 current_user_can('read_post'))生成预览链接。
- 如果固定链接结构不包含 %postname% 或 %pagename% 占位符,则输出简单链接;否则,提供可编辑的 slug 界面。
- 包含过滤器 get_sample_permalink_html,允许开发者自定义 HTML 输出。
代码示例
// 示例:获取文章 ID 为 123 的示例固定链接 HTML
$html = get_sample_permalink_html( 123 );
echo $html;注意事项
- 当启用 Gutenberg 编辑器时,get_sample_permalink_html 过滤器可能在某些文章类型中失效,需注意兼容性。
- 函数处理 RTL 语言显示问题,添加了特殊字符以修复双向文本缺陷。
- 如果用户有 manage_options 权限且固定链接结构未设置,会鼓励用户更改固定链接结构。
原文内容
Returns the HTML of the sample permalink slug editor.
Parameters
$postint|WP_Postrequired-
Post ID or post object.
$new_titlestring|nulloptional-
New title.
Default:
null $new_slugstring|nulloptional-
New slug.
Default:
null
Source
function get_sample_permalink_html( $post, $new_title = null, $new_slug = null ) {
$post = get_post( $post );
if ( ! $post ) {
return '';
}
list($permalink, $post_name) = get_sample_permalink( $post->ID, $new_title, $new_slug );
$view_link = false;
$preview_target = '';
if ( current_user_can( 'read_post', $post->ID ) ) {
if ( 'draft' === $post->post_status || empty( $post->post_name ) ) {
$view_link = get_preview_post_link( $post );
$preview_target = " target='wp-preview-{$post->ID}'";
} else {
if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
$view_link = get_permalink( $post );
} else {
// Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set.
$view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink );
}
}
}
// Permalinks without a post/page name placeholder don't have anything to edit.
if ( ! str_contains( $permalink, '%postname%' ) && ! str_contains( $permalink, '%pagename%' ) ) {
$return = '<strong>' . __( 'Permalink:' ) . "</strong>n";
if ( false !== $view_link ) {
$display_link = urldecode( $view_link );
$return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>n";
} else {
$return .= '<span id="sample-permalink">' . $permalink . "</span>n";
}
// Encourage a pretty permalink setting.
if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' )
&& ! ( 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post->ID )
) {
$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small">' . __( 'Change Permalink Structure' ) . "</a></span>n";
}
} else {
if ( mb_strlen( $post_name ) > 34 ) {
$post_name_abridged = mb_substr( $post_name, 0, 16 ) . '…' . mb_substr( $post_name, -16 );
} else {
$post_name_abridged = $post_name;
}
$post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>';
$display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) );
$return = '<strong>' . __( 'Permalink:' ) . "</strong>n";
$return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>n";
$return .= ''; // Fix bi-directional text display defect in RTL languages.
$return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>n";
$return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>n";
}
/**
* Filters the sample permalink HTML markup.
*
* @since 2.9.0
* @since 4.4.0 Added `$post` parameter.
*
* @param string $return Sample permalink HTML markup.
* @param int $post_id Post ID.
* @param string|null $new_title New sample permalink title.
* @param string|null $new_slug New sample permalink slug.
* @param WP_Post $post Post object.
*/
$return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post );
return $return;
}
Hooks
- apply_filters( ‘get_sample_permalink_html’, string $return, int $post_id, string|null $new_title, string|null $new_slug, WP_Post $post )
-
Filters the sample permalink HTML markup.
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
Skip to note 2 content
Sami Ahmed Siddiqui
This filter no longer work if
Gutenbergis enabled on your PostType.