函数文档

touch_time()

💡 云策文档标注

概述

touch_time() 函数用于输出编辑文章或评论发布日期的 HTML 表单元素。它根据参数动态生成日期和时间选择字段,支持本地化翻译和可访问性文本。

关键要点

  • 函数输出用于编辑文章或评论发布日期的 HTML 表单元素,包括年、月、日、时、分等字段。
  • 接受四个参数:$edit(编辑或添加日期)、$for_post(应用于文章或评论)、$tab_index(tabindex 属性)、$multi(是否添加额外字段和按钮)。
  • 内部使用 WP_Locale 处理本地化,如月份翻译和缩写,并调用相关函数如 mysql2date() 和 current_time() 处理日期时间。
  • 函数逻辑根据 $edit 和 $for_post 参数决定显示当前时间还是现有日期,并生成相应的 HTML 输出。
  • 在 WordPress 管理界面中常用于 post_submit_meta_box() 和 WP_Posts_List_Table::inline_edit() 等场景。

代码示例

touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 )

注意事项

  • 参数 $edit 和 $for_post 接受布尔值或整数(1/true 或 0/false),默认值分别为 1 和 1。
  • $tab_index 参数用于设置 tabindex 属性,默认值为 0。
  • $multi 参数控制是否输出额外字段和按钮,默认值为 0(false)。
  • 函数内部处理了文章状态(如草稿、待审核)对日期编辑的影响。
  • 自 WordPress 4.4.0 起,改用 get_comment() 替代全局变量 $comment,以提高代码健壮性。

📄 原文内容

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

Parameters

$editint|booloptional
Accepts 1|true for editing the date, 0|false for adding the date.

Default:1

$for_postint|booloptional
Accepts 1|true for applying the date to a post, 0|false for a comment.

Default:1

$tab_indexintrequired
The tabindex attribute to add. Default 0.
$multiint|booloptional
Whether the additional fields and buttons should be added.
Default 0|false.

Source

function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
global $wp_locale;
$post = get_post();

if ( $for_post ) {
$edit = ! ( in_array( $post->post_status, array( 'draft', 'pending' ), true ) && ( ! $post->post_date_gmt || '0000-00-00 00:00:00' === $post->post_date_gmt ) );
}

$tab_index_attribute = '';
if ( (int) $tab_index > 0 ) {
$tab_index_attribute = " tabindex="$tab_index"";
}

$post_date = ( $for_post ) ? $post->post_date : get_comment()->comment_date;
$jj = ( $edit ) ? mysql2date( 'd', $post_date, false ) : current_time( 'd' );
$mm = ( $edit ) ? mysql2date( 'm', $post_date, false ) : current_time( 'm' );
$aa = ( $edit ) ? mysql2date( 'Y', $post_date, false ) : current_time( 'Y' );
$hh = ( $edit ) ? mysql2date( 'H', $post_date, false ) : current_time( 'H' );
$mn = ( $edit ) ? mysql2date( 'i', $post_date, false ) : current_time( 'i' );
$ss = ( $edit ) ? mysql2date( 's', $post_date, false ) : current_time( 's' );

$cur_jj = current_time( 'd' );
$cur_mm = current_time( 'm' );
$cur_aa = current_time( 'Y' );
$cur_hh = current_time( 'H' );
$cur_mn = current_time( 'i' );

$month = '<label><span class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
__( 'Month' ) .
'</span><select class="form-required" ' . ( $multi ? '' : 'id="mm" ' ) . 'name="mm"' . $tab_index_attribute . ">n";
for ( $i = 1; $i < 13; $i = $i + 1 ) {
$monthnum = zeroise( $i, 2 );
$monthtext = $wp_locale->
get_month_abbrev( $wp_locale->get_month( $i ) );
$month .= "ttt" . '<option value="' . $monthnum . '" data-text="' . $monthtext . '" ' . selected( $monthnum, $mm, false ) . '>';
/* translators: 1: Month number (01, 02, etc.), 2: Month abbreviation. */
$month .= sprintf( __( '%1$s-%2$s' ), $monthnum, $monthtext ) . "</option>n";
}
$month .= '</select></label>';

$day = '<label><span class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
__( 'Day' ) .
'</span><input type="text" ' . ( $multi ? '' : 'id="jj" ' ) . 'name="jj" value="' . $jj . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" class="form-required" inputmode="numeric" /></label>';
$year = '<label><span class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
__( 'Year' ) .
'</span><input type="text" ' . ( $multi ? '' : 'id="aa" ' ) . 'name="aa" value="' . $aa . '" size="4" maxlength="4"' . $tab_index_attribute . ' autocomplete="off" class="form-required" inputmode="numeric" /></label>';
$hour = '<label><span class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
__( 'Hour' ) .
'</span><input type="text" ' . ( $multi ? '' : 'id="hh" ' ) . 'name="hh" value="' . $hh . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" class="form-required" inputmode="numeric" /></label>';
$minute = '<label><span class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
__( 'Minute' ) .
'</span><input type="text" ' . ( $multi ? '' : 'id="mn" ' ) . 'name="mn" value="' . $mn . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" class="form-required" inputmode="numeric" /></label>';

echo '<div class="timestamp-wrap">';
/* translators: 1: Month, 2: Day, 3: Year, 4: Hour, 5: Minute. */
printf( __( '%1$s %2$s, %3$s at %4$s:%5$s' ), $month, $day, $year, $hour, $minute );

echo '</div><input type="hidden" id="ss" name="ss" value="' . $ss . '" />';

if ( $multi ) {
return;
}

echo "nn";

$map = array(
'mm' => array( $mm, $cur_mm ),
'jj' => array( $jj, $cur_jj ),
'aa' => array( $aa, $cur_aa ),
'hh' => array( $hh, $cur_hh ),
'mn' => array( $mn, $cur_mn ),
);

foreach ( $map as $timeunit => $value ) {
list( $unit, $curr ) = $value;

echo '<input type="hidden" id="hidden_' . $timeunit . '" name="hidden_' . $timeunit . '" value="' . $unit . '" />' . "n";
$cur_timeunit = 'cur_' . $timeunit;
echo '<input type="hidden" id="' . $cur_timeunit . '" name="' . $cur_timeunit . '" value="' . $curr . '" />' . "n";
}
?>

<p>
<a href="#edit_timestamp" class="save-timestamp hide-if-no-js button"></a>
<a href="#edit_timestamp" class="cancel-timestamp hide-if-no-js button-cancel"></a>
</p>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/template.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/template.php#L807">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/template.php#L807-L898">View on GitHub</a></p></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/zeroise/">zeroise()</a><code>wp-includes/formatting.php

Add leading zeros when necessary.

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

Outputs the HTML selected attribute.

mysql2date()wp-includes/functions.php

Converts given MySQL date string into a different format.

current_time()wp-includes/functions.php

Retrieves the current time based on specified type.

WP_Locale::get_month_abbrev()wp-includes/class-wp-locale.php

Retrieves translated version of month abbreviation string.

WP_Locale::get_month()wp-includes/class-wp-locale.php

Retrieves the full translated month by month number.

__()wp-includes/l10n.php

Retrieves the translation of $text.

_e()wp-includes/l10n.php

Displays translated text.

get_post()wp-includes/post.php

Retrieves post data given a post ID or post object.

get_comment()wp-includes/comment.php

Retrieves comment data given a comment ID or comment object.

Show 5 moreShow less

Used by Description
post_submit_meta_box()wp-admin/includes/meta-boxes.php

Displays post submit form fields.

WP_Posts_List_Table::inline_edit()wp-admin/includes/class-wp-posts-list-table.php

Outputs the hidden row displayed when inline editing

Changelog

Version Description
4.4.0 Converted to use get_comment() instead of the global $comment.
0.71 Introduced.