为单个文章设置有效期,到期自动下线

综合评论8,088,541字数 333阅读1分6秒

代码放在func.php里,然后在后台编辑或新建文章

参照上图右下角,有一个文章有效期,往死里戳文章源自小武站https://1z345.cn/小武站-https://bbs.50-0.cn/791.html

保存或更新后,前台对应文章就会有一个倒计时。时间一到,自动进入回收站。文章源自小武站https://1z345.cn/小武站-https://bbs.50-0.cn/791.html

复制代码
  1. //文章有效期
  2. CSF::createMetabox('post_validity_options', array(
  3. 'title' => '文章有效期',
  4. 'post_type' => ['post', 'page', 'forum_post'],
  5. 'context' => 'side',
  6. 'data_type' => 'unserialize',
  7. ));
  8. CSF::createSection('post_validity_options', array(
  9. 'fields' => array(
  10. array(
  11. 'id' => 'post_validity_time',
  12. 'type' => 'date',
  13. 'desc' => '设置内容的有效期,请确保格式正确,例如:<code>2020-10-10 23:59:59</code></br>到期后将会自动删除此此内容',
  14. 'settings' => array(
  15. 'dateFormat' => 'yy-mm-dd 23:59:59',
  16. 'changeMonth' => true,
  17. 'changeYear' => true,
  18. ),
  19. ),
  20. ),
  21. ));
  22. add_filter('the_content', 'zib_c_the_content_asdfsdf');
  23. function zib_c_the_content_asdfsdf($content)
  24. {
  25. $post_id = get_the_ID();
  26. $time = get_post_meta($post_id, 'post_validity_time', true);
  27. if ($time) {
  28. $end_time = date("m/d/Y H:i:s", strtotime($time));
  29. $over = '<script type="text/javascript">window.location.reload()</script>';
  30. $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;
  31. }
  32. return $content;
  33. }
  34. add_action('template_redirect', 'zib_c_template_redirect_sadfasdfkjlaksdf');
  35. function zib_c_template_redirect_sadfasdfkjlaksdf()
  36. {
  37. if (is_single()) {
  38. $post_id = get_the_ID();
  39. $time = get_post_meta($post_id, 'post_validity_time', true);
  40. if ($time && strtotime($time) < strtotime(current_time('Y-m-d H:i:s'))) {
  41. //执行删除文章
  42. wp_trash_post($post_id);
  43. wp_safe_redirect(home_url());
  44. exit;
  45. }
  46. }
  47. }
文章源自小武站https://1z345.cn/小武站-https://bbs.50-0.cn/791.html文章源自小武站https://1z345.cn/小武站-https://bbs.50-0.cn/791.html
继续阅读
  • 本文由 admin 发表于2024年8月15日 00:19:22
  • 转载请务必保留本文链接:https://bbs.50-0.cn/791.html
匿名

发表评论

匿名网友
确定