如何在WordPress中添加自定义文章定时发布功能?
请提供在WordPress中创建和管理自定义文章定时发布功能的方法。
-
玖比肆 评论方法如下:
1. 创建一个WordPress主题或子主题。如果您已经有自己的主题,可以跳过此步骤。
2. 在theme的functions.php中添加以下代码来创建一个自定义日期字段
```
function custom_meta_box_date() {
add_meta_box(
'custom_meta_box_date',
__( 'Custom Publish Date', 'textdomain' ),
'custom_meta_box_date_callback',
'post',
'side',
'high'
);
}
add_action( 'add_meta_boxes', 'custom_meta_box_date' );function custom_meta_box_date_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'custom_meta_box_date_nonce' );
$date_value = get_post_meta( $post->ID, '_custom_publish_date', true );
?>
60,
'display' => __( 'Custom Schedule' ),
);
}
return $schedules;
}
add_filter( 'cron_schedules', 'custom_cron_schedule' );
```4. 编写一个函数来处理定时发布
```
function custom_publish_post( $post_id ) {
$date = get_post_meta( $post_id, '_custom_publish_date', true );
$now = current_time( 'mysql' );if ( ! $date || $date < $now ) { return; } wp_publish_post( $post_id );}```5. 添加一个函数来计划一个发布```function custom_schedule_post( $post_id ) { $date = get_post_meta( $post_id, '_custom_publish_date', true ); if ( ! $date ) { return; } $timestamp = strtotime( $date ); wp_schedule_single_event( $timestamp, 'custom_publish_post', array( $post_id ) );}add_action( 'save_post', 'custom_schedule_post' );```6. 添加一个定时发布计划```function custom_cron_schedule_post() { $args = array( 'post_type' => 'post',
'post_status' => 'draft',
'meta_query' => array(
array(
'key' => '_custom_publish_date',
'value' => date( 'Y-m-d H:i:s' ),
'compare' => '<=', 'type' => 'DATETIME'
),
),
);$posts = get_posts( $args );
if ( empty( $posts ) ) {
return;
}foreach ( $posts as $post ) {
custom_publish_post( $post->ID );
}
}add_action( 'custom_publish_post', 'custom_publish_post' );
add_action( 'wp', 'custom_cron_schedule_post' );
```这些步骤将在您的WordPress中创建自定义文章定时发布功能。现在,您可以在编辑页面中设置自定义发布日期,当到达该日期时,文章将自动发布。
2023年 5月 14日 下午4:55
