函数文档

post_submit_meta_box()

💡 云策文档标注

概述

post_submit_meta_box() 函数用于在 WordPress 后台编辑文章页面显示发布元框,包含保存、预览、状态、可见性和发布日期等表单字段。它处理文章状态逻辑和用户权限,并输出 HTML 内容。

关键要点

  • 函数接受两个参数:$post(必需,当前文章对象)和 $args(可选,元框构建参数数组)。
  • 根据文章状态(如 draft、publish、private、pending、future)动态显示按钮和选项。
  • 检查用户权限(如 current_user_can())以控制发布能力。
  • 输出包括保存草稿、预览、状态选择、可见性设置和日期时间字段。
  • 包含相关 Hook,如 post_submitbox_misc_actions,允许开发者扩展功能。
  • 函数内部使用多种 WordPress 核心函数,如 submit_button()、selected()、date_i18n() 等。

代码示例

function post_submit_meta_box( $post, $args = array() ) {
    global $action;
    $post_id = (int) $post->ID;
    $post_type = $post->post_type;
    $post_type_object = get_post_type_object( $post_type );
    $can_publish = current_user_can( $post_type_object->cap->publish_posts );
    // ... 输出 HTML 表单字段
}

注意事项

  • 确保 $post 参数是有效的 WP_Post 对象,否则可能导致错误。
  • 函数输出大量 HTML,适合在元框回调中使用,不建议直接修改核心代码。
  • 使用 Hook 如 post_submitbox_misc_actions 可以安全地添加自定义内容。

📄 原文内容

Displays post submit form fields.

Parameters

$postWP_Postrequired
Current post object.
$argsarrayoptional
Array of arguments for building the post submit meta box.

  • id string
    Meta box 'id' attribute.
  • title string
    Meta box title.
  • callback callable
    Meta box display callback.
  • args array
    Extra meta box arguments.

Default:array()

Source

function post_submit_meta_box( $post, $args = array() ) {
global $action;

$post_id = (int) $post->ID;
$post_type = $post->post_type;
$post_type_object = get_post_type_object( $post_type );
$can_publish = current_user_can( $post_type_object->cap->publish_posts );
?>
<div class="submitbox" id="submitpost">

<div id="minor-publishing">

<div style="display:none;">

</div>

<div id="minor-publishing-actions">
<div id="save-action">
post_status, array( 'publish', 'future', 'pending' ), true ) ) {
$private_style = '';
if ( 'private' === $post->post_status ) {
$private_style = 'style="display:none"';
}
?>
<input <?php echo $private_style; ?> type="submit" name="save" id="save-post" value="" class="button" />
<span class="spinner"></span>
post_status && $can_publish ) { ?>
<input type="submit" name="save" id="save-post" value="<?php esc_attr_e( 'Save as Pending' ); ?>" class="button" />
<span class="spinner"></span>

</div>

<div id="preview-action">
post_status ) {
$preview_button_text = __( 'Preview Changes' );
} else {
$preview_button_text = __( 'Preview' );
}

$preview_button = sprintf(
'%1$s<span class="screen-reader-text"> %2$s</span>',
$preview_button_text,
/* translators: Hidden accessibility text. */
__( '(opens in a new tab)' )
);
?>
<a class="preview button" href="<?php echo $preview_link; ?>" target="wp-preview-<?php echo $post_id; ?>" id="post-preview"></a>
<input type="hidden" name="wp-preview" id="wp-preview" value="" />
</div>

<div class="clear"></div>
</div>

<div id="misc-publishing-actions">
<div class="misc-pub-section misc-pub-post-status">

<span id="post-status-display">
post_status ) {
case 'private':
_e( 'Privately Published' );
break;
case 'publish':
_e( 'Published' );
break;
case 'future':
_e( 'Scheduled' );
break;
case 'pending':
_e( 'Pending Review' );
break;
case 'draft':
case 'auto-draft':
_e( 'Draft' );
break;
}
?>
</span>

post_status || 'private' === $post->post_status || $can_publish ) {
$private_style = '';
if ( 'private' === $post->post_status ) {
$private_style = 'style="display:none"';
}
?>
<a href="#post_status" <?php echo $private_style; ?> class="edit-post-status hide-if-no-js" role="button"><span aria-hidden="true"></span> <span class="screen-reader-text">

</span></a>

<div id="post-status-select" class="hide-if-js">
<input type="hidden" name="hidden_post_status" id="hidden_post_status" value="<?php echo esc_attr( ( 'auto-draft' === $post->post_status ) ? 'draft' : $post->post_status ); ?>" />
<label for="post_status" class="screen-reader-text">

</label>
<select name="post_status" id="post_status">
post_status ) : ?>
<option<?php selected( $post->post_status, 'publish' ); ?> value='publish'></option>
post_status ) : ?>
<option<?php selected( $post->post_status, 'private' ); ?> value='publish'></option>
post_status ) : ?>
<option<?php selected( $post->post_status, 'future' ); ?> value='future'></option>

<option<?php selected( $post->post_status, 'pending' ); ?> value='pending'></option>
post_status ) : ?>
<option<?php selected( $post->post_status, 'auto-draft' ); ?> value='draft'></option>

<option<?php selected( $post->post_status, 'draft' ); ?> value='draft'></option>

</select>
<a href="#post_status" class="save-post-status hide-if-no-js button"></a>
<a href="#post_status" class="cancel-post-status hide-if-no-js button-cancel"></a>
</div>

</div>

<div class="misc-pub-section misc-pub-visibility" id="visibility">

<span id="post-visibility-display">
post_status ) {
$post->post_password = '';
$visibility = 'private';
$visibility_trans = __( 'Private' );
} elseif ( ! empty( $post->post_password ) ) {
$visibility = 'password';
$visibility_trans = __( 'Password protected' );
} elseif ( 'post' === $post_type && is_sticky( $post_id ) ) {
$visibility = 'public';
$visibility_trans = __( 'Public, Sticky' );
} else {
$visibility = 'public';
$visibility_trans = __( 'Public' );
}

echo esc_html( $visibility_trans );
?>
</span>

<a href="#visibility" class="edit-visibility hide-if-no-js" role="button"><span aria-hidden="true"></span> <span class="screen-reader-text">

</span></a>

<div id="post-visibility-select" class="hide-if-js">
<input type="hidden" name="hidden_post_password" id="hidden-post-password" value="<?php echo esc_attr( $post->post_password ); ?>" />

<input type="checkbox" style="display:none" name="hidden_post_sticky" id="hidden-post-sticky" value="sticky" <?php checked( is_sticky( $post_id ) ); ?> />

<input type="hidden" name="hidden_post_visibility" id="hidden-post-visibility" value="<?php echo esc_attr( $visibility ); ?>" />
<input type="radio" name="visibility" id="visibility-radio-public" value="public" <?php checked( $visibility, 'public' ); ?> /> <label for="visibility-radio-public" class="selectit"></label><br />

<span id="sticky-span"><input id="sticky" name="sticky" type="checkbox" value="sticky" <?php checked( is_sticky( $post_id ) ); ?> /> <label for="sticky" class="selectit"></label><br /></span>

<input type="radio" name="visibility" id="visibility-radio-password" value="password" <?php checked( $visibility, 'password' ); ?> /> <label for="visibility-radio-password" class="selectit"></label><br />
<span id="password-span"><label for="post_password"></label> <input type="text" name="post_password" id="post_password" value="<?php echo esc_attr( $post->post_password ); ?>" maxlength="255" /><br /></span>

<input type="radio" name="visibility" id="visibility-radio-private" value="private" <?php checked( $visibility, 'private' ); ?> /> <label for="visibility-radio-private" class="selectit"></label><br />

<p>
<a href="#visibility" class="save-post-visibility hide-if-no-js button"></a>
<a href="#visibility" class="cancel-post-visibility hide-if-no-js button-cancel"></a>
</p>
</div>

</div>

post_status ) { // Scheduled for publishing at a future date.
/* translators: Post date information. %s: Date on which the post is currently scheduled to be published. */
$stamp = __( 'Scheduled for: %s' );
} elseif ( 'publish' === $post->post_status || 'private' === $post->post_status ) { // Already published.
/* translators: Post date information. %s: Date on which the post was published. */
$stamp = __( 'Published on: %s' );
} elseif ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { // Draft, 1 or more saves, no date specified.
$stamp = __( 'Publish <b>immediately</b>' );
} elseif ( time() < strtotime( $post->post_date_gmt . ' +0000' ) ) { // Draft, 1 or more saves, future date specified.
/* translators: Post date information. %s: Date on which the post is to be published. */
$stamp = __( 'Schedule for: %s' );
} else { // Draft, 1 or more saves, date specified.
/* translators: Post date information. %s: Date on which the post is to be published. */
$stamp = __( 'Publish on: %s' );
}
$date = sprintf(
$date_string,
date_i18n( $date_format, strtotime( $post->post_date ) ),
date_i18n( $time_format, strtotime( $post->post_date ) )
);
} else { // Draft (no saves, and thus no date specified).
$stamp = __( 'Publish <b>immediately</b>' );
$date = sprintf(
$date_string,
date_i18n( $date_format, strtotime( current_time( 'mysql' ) ) ),
date_i18n( $time_format, strtotime( current_time( 'mysql' ) ) )
);
}

if ( ! empty( $args['args']['revisions_count'] ) ) :
?>
<div class="misc-pub-section misc-pub-revisions">
' . number_format_i18n( $args['args']['revisions_count'] ) . '</b>' );
?>
<a class="hide-if-no-js" href="<?php echo esc_url( get_edit_post_link( $args['args']['revision_id'] ) ); ?>"><span aria-hidden="true"></span> <span class="screen-reader-text">

</span></a>
</div>

<div class="misc-pub-section curtime misc-pub-curtime">
<span id="timestamp">
' . $date . '</b>' ); ?>
</span>
<a href="#edit_timestamp" class="edit-timestamp hide-if-no-js" role="button">
<span aria-hidden="true"></span>
<span class="screen-reader-text">

</span>
</a>
<fieldset id="timestampdiv" class="hide-if-js">
<legend class="screen-reader-text">

</legend>

</fieldset>
</div>
post_status && get_post_meta( $post_id, '_customize_changeset_uuid', true ) ) :
$message = sprintf(
/* translators: %s: URL to the Customizer. */
__( 'This draft comes from your <a href="%s">unpublished customization changes</a>. You can edit, but there is no need to publish now. It will be published automatically with those changes.' ),
esc_url(
add_query_arg(
'changeset_uuid',
rawurlencode( get_post_meta( $post_id, '_customize_changeset_uuid', true ) ),
admin_url( 'customize.php' )
)
)
);
wp_admin_notice(
$message,
array(
'type' => 'info',
'additional_classes' => array( 'notice-alt', 'inline' ),
)
);
endif;

/**
* Fires after the post time/date setting in the Publish meta box.
*
* @since 2.9.0
* @since 4.4.0 Added the `$post` parameter.
*
* @param WP_Post $post WP_Post object for the current post.
*/
do_action( 'post_submitbox_misc_actions', $post );
?>
</div>
<div class="clear"></div>
</div>

<div id="major-publishing-actions">

<div id="delete-action">

<a class="submitdelete deletion" href="<?php echo get_delete_post_link( $post_id ); ?>"></a>

</div>

<div id="publishing-action">
<span class="spinner"></span>
post_status, array( 'publish', 'future', 'private' ), true ) || 0 === $post_id ) {
if ( $can_publish ) :
if ( ! empty( $post->post_date_gmt ) && time() < strtotime( $post->post_date_gmt . ' +0000' ) ) :
?>
<input name="original_publish" type="hidden" id="original_publish" value="<?php echo esc_attr_x( 'Schedule', 'post action/button label' ); ?>" />

<input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e( 'Publish' ); ?>" />

<input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e( 'Submit for Review' ); ?>" />

<input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e( 'Update' ); ?>" />
'publish' ) ); ?>

</div>
<div class="clear"></div>
</div>

</div>
</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#L30">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L30-L411">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/post_submitbox_minor_actions/"><span class="hook-func">do_action</span>( ‘post_submitbox_minor_actions’, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Fires after the Save Draft (or Save as Pending) and Preview (or Preview Changes) buttons in the Publish meta box.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/post_submitbox_misc_actions/"><span class="hook-func">do_action</span>( ‘post_submitbox_misc_actions’, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Fires after the post time/date setting in the Publish meta box.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/post_submitbox_start/"><span class="hook-func">do_action</span>( ‘post_submitbox_start’, <nobr><span class="arg-type">WP_Post|null</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Fires at the beginning of the publishing actions section of the Publish meta box.</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.

get_preview_post_link()wp-includes/link-template.php

Retrieves the URL used for the post preview.

is_post_type_viewable()wp-includes/post.php

Determines whether a post type is considered “viewable”.

submit_button()wp-admin/includes/template.php

Echoes a submit button, with provided text and appropriate class(es).

touch_time()wp-admin/includes/template.php

Prints out HTML form date elements for editing post or comment publish date.

esc_attr_e()wp-includes/l10n.php

Displays translated text that has been escaped for safe use in an attribute.

_ex()wp-includes/l10n.php

Displays translated string with gettext context.

esc_attr_x()wp-includes/l10n.php

Translates string with gettext context, and escapes it for safe use in an attribute.

selected()wp-includes/general-template.php

Outputs the HTML selected attribute.

checked()wp-includes/general-template.php

Outputs the HTML checked attribute.

date_i18n()wp-includes/functions.php

Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.

current_time()wp-includes/functions.php

Retrieves the current time based on specified type.

get_edit_post_link()wp-includes/link-template.php

Retrieves the edit post link for post.

get_delete_post_link()wp-includes/link-template.php

Retrieves the delete posts link for post.

is_sticky()wp-includes/post.php

Determines whether a post is sticky.

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.

_x()wp-includes/l10n.php

Retrieves translated string with gettext context.

esc_url()wp-includes/formatting.php

Checks and cleans a URL.

esc_attr()wp-includes/formatting.php

Escaping for HTML attributes.

esc_html()wp-includes/formatting.php

Escaping for HTML blocks.

add_query_arg()wp-includes/functions.php

Retrieves a modified URL query string.

number_format_i18n()wp-includes/functions.php

Converts float number to format based on the locale.

admin_url()wp-includes/link-template.php

Retrieves the URL to the admin area for the current site.

do_action()wp-includes/plugin.php

Calls the callback functions that have been added to an action hook.

get_post_meta()wp-includes/post.php

Retrieves a post meta field for the given post ID.

get_post_type_object()wp-includes/post.php

Retrieves a post type object by name.

Show 23 moreShow less

Changelog

Version Description
2.7.0 Introduced.