源码介绍
将其添加到 WordPress 主题的 functions.php 文件中:
function custom_publish_actions() {
// 添加复选框到发布元框
echo '<div id="custom-publish-time-option" style="padding:10px;">
<label>
<input type="checkbox" name="custom_publish_time_checkbox" id="customPublishTimeCheckbox" value="yes" onchange="autoUpdateTime();" />
自动更新发布时间
</label>
</div>';
// JavaScript 自动更新发布时间
echo '<script type="text/javascript">
function autoUpdateTime() {
if (document.getElementById("customPublishTimeCheckbox").checked) {
var currentDate = new Date();
document.getElementById("aa").value = currentDate.getFullYear();
document.getElementById("mm").value = ("0" + (currentDate.getMonth() + 1)).slice(-2);
document.getElementById("jj").value = ("0" + currentDate.getDate()).slice(-2);
document.getElementById("hh").value = ("0" + currentDate.getHours()).slice(-2);
document.getElementById("mn").value = ("0" + currentDate.getMinutes()).slice(-2);
} else {
// Optionally clear fields or set to original values
}
}
</script>';
}
function update_post_date_on_publish($data, $postarr) {
// 检查复选框是否被勾选并更新发布时间
if (isset($_POST['custom_publish_time_checkbox']) && $_POST['custom_publish_time_checkbox'] == 'yes' && $data['post_status'] == 'publish') {
$current_time = current_time('mysql');
$data['post_date'] = $current_time;
$data['post_date_gmt'] = get_gmt_from_date($current_time);
}
return $data;
}
// 添加动作钩子到发布框
add_action('post_submitbox_misc_actions', 'custom_publish_actions');
// 添加过滤器以检查复选框状态并更新发布时间
add_filter('wp_insert_post_data', 'update_post_date_on_publish', 10, 2);
有问题及时联系站长,QQ:1240555208
更多优质资源在QQ群里,可以进群领取:467392290~
© 版权声明
THE END
暂无评论内容