10

WordPress纯代码禁止发表重复标题的文章

 3 years ago
source link: https://www.huhexian.com/34788.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

WordPress纯代码禁止发表重复标题的文章

2022-03-2913:28:19评论1574字

实所有插件后端代码都等同于加入到functions.php的代码片段,functions.php里的代码片段可以做成插件,那么反过来插件自然也可以集成到functions.php里面。

WordPress纯代码禁止发表重复标题的文章

插件主要是方便管理,但同时也增加了静态资源请求数,如果你知道怎么处理这些请求,其实用不用插件也知道该如何优化网站 —— 扯远了。 下面我就直接贴上代码版的吧。

主要修改的地方是:将js转为script标签内容,等于消除一个请求,翻译也省了,换成中文。 将下面代码复制粘贴到你主题的functions.php文件里面,这个不用解释了吧?

  1. * 发表文章时禁止与已存在的标题相重复
  2. * Modify from Plugin: Duplicate Title Validate
  3. * Description: this plugin help , not allow publish Duplicate Title .
  4. * Author: hasan movahed
  5. * Reviser: INLOJV
  6. * Version: 1.0
  7. * Author URI: wallfa .com
  8. * Reviser URI: inlojv .com
  9. // 发表文章页面,前端抓取标题并使用AJAX发送请求
  10. add_action( 'admin_print_footer_scripts', 'duplicate_titles_enqueue_scripts', 100 );
  11. function duplicate_titles_enqueue_scripts() {
  12. ?>
  13. posts WHERE post_status = 'publish' AND post_type = 'post'
  14. AND post_title = '{$title}' AND ID != {$post_id} ";
  15. $results = $wpdb->get_results($titles);
  16. if($results) {
  17. echo "". _( '此标题已存在,请换一个标题!' , '' ) ." ";
  18. } else {
  19. echo ''._('恭喜,此标题未与其他文章标题重复!' , '').'';
  20. die();
  21. // 检测后端标题并且避免同名文章更新草稿
  22. add_action( 'publish_post','duplicate_titles_wallfa_bc' ) ;
  23. function duplicate_titles_wallfa_bc( $post ){
  24. global $wpdb ;
  25. $title = $_POST['post_title'] ;
  26. $post_id = $post ;
  27. $wtitles = "SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'
  28. AND post_title = '{$title}' AND ID != {$post_id} " ;
  29. $wresults = $wpdb->get_results( $wtitles ) ;
  30. if ( $wresults ){
  31. $wpdb->update( $wpdb->posts, array( 'post_status' =>
  32. 'draft' ), array( 'ID' => $post ) ) ;
  33. $arr_params = array( 'message' => '10', 'wallfaerror' => '1' ) ;
  34. $location = add_query_arg( $arr_params , get_edit_post_link( $post , 'url' ) ) ;
  35. wp_redirect( $location ) ;
  36. exit ;
  37. /// 文章提交更新后的提示
  38. add_action( 'admin_notices', 'not_published_error_notice' );
  39. function not_published_error_notice() {
  40. if(isset($_GET['wallfaerror']) == 1 ){
  41. ?>

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK