转载的
以下代码放入function.php文件中即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | /** * 发表文章时禁止与已存在的标题相重复 * Modify from Plugin: Duplicate Title Validate * Description: this plugin help , not allow publish Duplicate Title . * Author: hasan movahed * Reviser: INLOJV * Version: 1.0 * Author URI: http://www.wallfa.com * Reviser URI: http://www.inlojv.com */ // 发表文章页面,前端抓取标题并使用AJAX发送请求 add_action( 'admin_print_footer_scripts', 'duplicate_titles_enqueue_scripts', 100 ); function duplicate_titles_enqueue_scripts() { ?> <script> jQuery(function($){ function checkTitleAjax(title, id,post_type) { var data = { action: 'title_checks', post_title: title, post_type: post_type, post_id: id }; $.post(ajaxurl, data, function(response) { $('#message').remove(); $('#poststuff').prepend('<div id="message" class="updated below-h2 fade "><p>'+response+'</p></div>'); }); }; $('#title').change(function() { var title = $('#title').val(); var id = $('#post_ID').val(); var post_type = $('#post_type').val(); checkTitleAjax(title, id,post_type); }); }); </script> <?php } // 接收前端ajax参数 add_action('wp_ajax_title_checks', 'duplicate_title_checks_callback'); function duplicate_title_checks_callback(){ global $wpdb; $title = $_POST['post_title']; $post_id = $_POST['post_id']; $titles = "SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_title = '{$title}' AND ID != {$post_id} "; $results = $wpdb->get_results($titles); if($results) { echo "<span style='color:red'>". _( '此标题已存在,请换一个标题!' , '' ) ." </span>"; } else { echo '<span style="color:green">'._('恭喜,此标题未与其他文章标题重复!' , '').'</span>'; } die(); } // 检测后端标题并且避免同名文章更新草稿 add_action( 'publish_post','duplicate_titles_wallfa_bc' ) ; function duplicate_titles_wallfa_bc( $post ){ global $wpdb ; $title = $_POST['post_title'] ; $post_id = $post ; $wtitles = "SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_title = '{$title}' AND ID != {$post_id} " ; $wresults = $wpdb->get_results( $wtitles ) ; if ( $wresults ){ $wpdb->update( $wpdb->posts, array( 'post_status' => 'draft' ), array( 'ID' => $post ) ) ; $arr_params = array( 'message' => '10', 'wallfaerror' => '1' ) ; $location = add_query_arg( $arr_params , get_edit_post_link( $post , 'url' ) ) ; wp_redirect( $location ) ; exit ; } } /// 文章提交更新后的提示 add_action( 'admin_notices', 'not_published_error_notice' ); function not_published_error_notice() { if(isset($_GET['wallfaerror']) == 1 ){ ?> <div class="updated"> <p style='color:red' ><?php _e('貌似已经存在相同标题的文章,若您使用了文章别名作为固定链接,则可以通过修改本文的固定链接来使标题不再重复!' , '') ?></p> </div> <?php } } // 禁用自动保存 add_action( 'wp_print_scripts', 'disable_autosave' ) ; function disable_autosave(){ wp_deregister_script( 'autosave' ) ; } |
原文链接:https://xiaohost.com/1753.html,转载请注明出处。
评论0