post_slug_meta_box()
概述
post_slug_meta_box() 函数用于在 WordPress 后台显示文章或页面的 slug 编辑表单字段。它通过应用 editable_slug 过滤器来允许开发者自定义 slug 的可编辑性。
关键要点
- 函数接受一个必需的参数 $post,类型为 WP_Post,表示当前文章对象。
- 使用 apply_filters() 调用 editable_slug 过滤器,传递 $post->post_name 和 $post 参数,以过滤 slug 的可编辑状态。
- 该函数自 WordPress 2.6.0 版本引入,是核心功能的一部分。
代码示例
function post_slug_meta_box( $post ) {
/** This filter is documented in wp-admin/edit-tag-form.php */
$editable_slug = apply_filters( 'editable_slug', $post->post_name, $post );
?>
<input type="text" name="post_name" value="<?php echo esc_attr( $editable_slug ); ?>" id="post_name" />
<?php
}注意事项
- editable_slug 过滤器可用于自定义 slug 的显示或编辑行为,适用于 WP_Post 或 WP_Term 对象。
- 相关函数包括 esc_attr() 用于转义 HTML 属性,确保安全性。
Displays slug form fields.
Parameters
$postWP_Postrequired-
Current post object.
Source
function post_slug_meta_box( $post ) {
/** This filter is documented in wp-admin/edit-tag-form.php */
$editable_slug = apply_filters( 'editable_slug', $post->post_name, $post );
?>
<label class="screen-reader-text" for="post_name">
</label><input name="post_name" type="text" class="large-text" id="post_name" value="<?php echo esc_attr( $editable_slug ); ?>" />
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/meta-boxes.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/meta-boxes.php#L936">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L936-L947">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/editable_slug/"><span class="hook-func">apply_filters</span>( ‘editable_slug’, <nobr><span class="arg-type">string</span> <span class="arg-name">$slug</span></nobr>, <nobr><span class="arg-type">WP_Term|WP_Post</span> <span class="arg-name">$tag</span></nobr> )</a></dt><dd><p>Filters the editable slug for a post or term.</p>
</dd></dl></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/_e/">_e()</a><code>wp-includes/l10n.php
Displays translated text.
esc_attr()wp-includes/formatting.php
Escaping for HTML attributes.
apply_filters()wp-includes/plugin.php
Calls the callback functions that have been added to a filter hook.
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |