wp_dashboard_quick_press()
概述
wp_dashboard_quick_press() 函数用于在 WordPress 仪表盘中显示 Quick Draft 小部件,允许用户快速创建草稿文章。它处理用户权限、自动草稿的生成和更新,并输出相应的 HTML 表单。
关键要点
- 函数检查当前用户是否具有 'edit_posts' 权限,若无则直接返回。
- 通过 get_user_option() 获取上次的 post_ID,并基于 post 状态决定是否创建新的自动草稿或重用现有草稿。
- 使用 get_default_post_to_edit() 生成默认文章数据,并可能通过 update_user_option() 更新用户选项以保存 post_ID。
- 输出包含标题、内容、分类和发布按钮的 HTML 表单,集成 WordPress 安全机制如 wp_nonce_field()。
- 涉及多个相关函数和 Hook,如 apply_filters('enter_title_here') 用于过滤标题占位符文本。
代码示例
function wp_dashboard_quick_press( $error_msg = false ) {
global $post_ID;
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
// 检查是否需要新的自动草稿或可重用旧的
$last_post_id = (int) get_user_option( 'dashboard_quick_press_last_post_id' );
if ( $last_post_id ) {
$post = get_post( $last_post_id );
if ( empty( $post ) || 'auto-draft' !== $post->post_status ) {
$post = get_default_post_to_edit( 'post', true );
update_user_option( get_current_user_id(), 'dashboard_quick_press_last_post_id', (int) $post->ID );
} else {
$post->post_title = '';
}
} else {
$post = get_default_post_to_edit( 'post', true );
$user_id = get_current_user_id();
if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ), true ) ) {
update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID );
}
}
$post_ID = (int) $post->ID;
// 输出 HTML 表单代码...
}注意事项
- 函数自 WordPress 3.8.0 版本引入,主要用于仪表盘小部件,不建议直接在其他上下文中调用。
- 依赖于用户选项和文章状态管理,确保在启用多站点时正确处理用户所属站点。
- 输出包含 JavaScript 依赖(如 hide-if-no-js 类),需确保前端脚本正常加载。
Displays the Quick Draft widget.
Parameters
$error_msgstring|falseoptional-
Error message.
Default:
false
Source
function wp_dashboard_quick_press( $error_msg = false ) {
global $post_ID;
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
// Check if a new auto-draft (= no new post_ID) is needed or if the old can be used.
$last_post_id = (int) get_user_option( 'dashboard_quick_press_last_post_id' ); // Get the last post_ID.
if ( $last_post_id ) {
$post = get_post( $last_post_id );
if ( empty( $post ) || 'auto-draft' !== $post->post_status ) { // auto-draft doesn't exist anymore.
$post = get_default_post_to_edit( 'post', true );
update_user_option( get_current_user_id(), 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID.
} else {
$post->post_title = ''; // Remove the auto draft title.
}
} else {
$post = get_default_post_to_edit( 'post', true );
$user_id = get_current_user_id();
// Don't create an option if this is a super admin who does not belong to this site.
if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ), true ) ) {
update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID.
}
}
$post_ID = (int) $post->ID;
?>
<form name="post" action="<?php echo esc_url( admin_url( 'post.php' ) ); ?>" method="post" id="quick-press" class="initial-form hide-if-no-js">
array( 'error' ),
)
);
}
?>
<div class="input-text-wrap" id="title-wrap">
<label for="title">
</label>
<input type="text" name="post_title" id="title" autocomplete="off" />
</div>
<div class="textarea-wrap" id="description-wrap">
<label for="content"></label>
<textarea name="content" id="content" placeholder="<?php esc_attr_e( 'What’s on your mind?' ); ?>" class="mceEditor" rows="3" cols="15" autocomplete="off"></textarea>
</div>
<p class="submit">
<input type="hidden" name="action" id="quickpost-action" value="post-quickdraft-save" />
<input type="hidden" name="post_ID" value="<?php echo $post_ID; ?>" />
<input type="hidden" name="post_type" value="post" />
'save-post' ) ); ?>
<br class="clear" />
</p>
</form>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/dashboard.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/dashboard.php#L542">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/dashboard.php#L542-L614">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/enter_title_here/"><span class="hook-func">apply_filters</span>( ‘enter_title_here’, <nobr><span class="arg-type">string</span> <span class="arg-name">$text</span></nobr>, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Filters the title field placeholder text.</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/wp_admin_notice/">wp_admin_notice()</a><code>wp-includes/functions.php
Outputs an admin notice.
wp_dashboard_recent_drafts()wp-admin/includes/dashboard.php
Show recent drafts of the user on the dashboard.
submit_button()wp-admin/includes/template.php
Echoes a submit button, with provided text and appropriate class(es).
get_default_post_to_edit()wp-admin/includes/post.php
Returns default post information to use when populating the “Write Post” form.
esc_attr_e()wp-includes/l10n.php
Displays translated text that has been escaped for safe use in an attribute.
wp_nonce_field()wp-includes/functions.php
Retrieves or display nonce hidden field for forms.
update_user_option()wp-includes/user.php
Updates user option with global blog capability.
get_blogs_of_user()wp-includes/user.php
Gets the sites a user belongs to.
get_user_option()wp-includes/user.php
Retrieves user option that can be either per Site or per Network.
current_user_can()wp-includes/capabilities.php
Returns whether the current user has the specified capability.
__()wp-includes/l10n.php
Retrieves the translation of $text.
_e()wp-includes/l10n.php
Displays translated text.
esc_url()wp-includes/formatting.php
Checks and cleans a URL.
get_current_blog_id()wp-includes/load.php
Retrieves the current site ID.
admin_url()wp-includes/link-template.php
Retrieves the URL to the admin area for the current site.
apply_filters()wp-includes/plugin.php
Calls the callback functions that have been added to a filter hook.
get_current_user_id()wp-includes/user.php
Gets the current user’s ID.
get_post()wp-includes/post.php
Retrieves post data given a post ID or post object.
| Used by | Description |
|---|---|
wp_dashboard_quick_press_output()wp-admin/includes/deprecated.php |
Output the QuickPress dashboard widget. |
Changelog
| Version | Description |
|---|---|
| 3.8.0 | Introduced. |