代码放在 func.php 里,然后在后台编辑或新建文章
参照上图右下角,有一个文章有效期,往死里戳
保存或更新后,前台对应文章就会有一个倒计时。时间一到,自动进入回收站。
//文章有效期
CSF::createMetabox('post_validity_options', array(
'title' => '文章有效期',
'post_type' => ['post', 'page', 'forum_post'],
'context' => 'side',
'data_type' => 'unserialize',
));
CSF::createSection('post_validity_options', array(
'fields' => array(
array(
'id' => 'post_validity_time',
'type' => 'date',
'desc' => '设置内容的有效期,请确保格式正确,例如:<code>2020-10-10 23:59:59</code></br>到期后将会自动删除此此内容',
'settings' => array(
'dateFormat' => 'yy-mm-dd 23:59:59',
'changeMonth' => true,
'changeYear' => true,
),
),
),
));
add_filter('the_content', 'zib_c_the_content_asdfsdf');
function zib_c_the_content_asdfsdf($content)
{
$post_id = get_the_ID();
$time = get_post_meta($post_id, 'post_validity_time', true);
if ($time) {
$end_time = date("m/d/Y H:i:s", strtotime($time));
$over = '<script type="text/javascript">window.location.reload()</script>';
$content = '<div class="badg c-red block mb20"><i class="fa fa-clock-o mr10"></i>当前内容将于<span class="" data-over-text="' . esc_attr($over) . '" data-countdown="' . $end_time . '"></span>后失效</div>' . $content;
}
return $content;
}
add_action('template_redirect', 'zib_c_template_redirect_sadfasdfkjlaksdf');
function zib_c_template_redirect_sadfasdfkjlaksdf()
{
if (is_single()) {
$post_id = get_the_ID();
$time = get_post_meta($post_id, 'post_validity_time', true);
if ($time && strtotime($time) < strtotime(current_time('Y-m-d H:i:s'))) {
//执行删除文章
wp_trash_post($post_id);
wp_safe_redirect(home_url());
exit;
}
}
}
© 版权声明
版权解释权归发文作者所有,未经允许,不得转载!侵权请举报!
THE END
暂无评论内容