这将为您提供大部分途径,但实际的搜索并不完美。It would be great if someone who knows more about $wpdb can help get a better result.
如果这是我的原始帖子标题:SOPA is dead, woot!
然后只要一个标题“SOPA”就会找到它,就像一个标题“已经死了”一样but not “叮咚,女巫死了”或“SOPA是个坏主意”。
但是,基本前提是在“检查标题”中添加一个按钮,按下时,标题将使用jQuery发布,并通过AJAX处理以返回匹配的标题,不包括当前帖子。如果您能够接受此内容并对其进行修改以返回更好的匹配项,请共享!
下面是基本插件代码,后面是jQuery,它应该包含在插件文件夹中一个名为duptitles的文件中。js公司
/*
Plugin Name: Duplicate Titles Check
Description: Prevent user\'s using similar post titles.
Version: 1.0
Author: 5t3ph
Author URI: http://stephscharf.me
*/
//jQuery to send AJAX request - only available on the post editing page
function dup_titles_enqueue_scripts( $hook ) {
if( !in_array( $hook, array( \'post.php\', \'post-new.php\' ) ) )
return;
wp_enqueue_script(
\'duptitles\',
plugins_url( \'/duptitles.js\', __FILE__ ),
array( \'jquery\' )
);
}
add_action( \'admin_enqueue_scripts\', \'dup_titles_enqueue_scripts\', 2000 );
// Invoke baked-in WP ajax goodness
// Codex: http://codex.wordpress.org/AJAX_in_Plugins
add_action(\'wp_ajax_title_check\', \'title_check_callback\');
function title_check_callback() {
function title_check() {
$title = $_POST[\'post_title\'];
$post_id = $_POST[\'post_id\'];
global $wpdb;
$sim_titles = "SELECT post_title
FROM $wpdb->posts
WHERE post_status = \'publish\' AND post_type = \'post\'
AND post_title LIKE \'%{$title}%\'
AND ID != {$post_id}";
$sim_results = $wpdb->get_results($sim_titles);
if($sim_results) {
$titles = \'<ul>\';
foreach ( $sim_results as $the_title )
{
$titles .= \'<li>\'.$the_title->post_title.\'</li>\';
}
$titles .= \'</ul>\';
return $titles;
} else {
return \'<p>This title is unique</p>\';
}
}
echo title_check();
die();
}
开始jQuery for
duptitles.jsjQuery(document).ready(function($){
// Post function
function checkTitle(title, id) {
var data = {
action: \'title_check\',
post_title: title,
post_id: id
};
//var ajaxurl = \'wp-admin/admin-ajax.php\';
$.post(ajaxurl, data, function(response) {
$(\'#message\').remove();
$(\'#poststuff\').prepend(\'<div id=\\"message\\" class=\\"updated fade\\"><p>\'+response+\'</p></div>\');
});
};
// Add button to "Check Titles" below title field in post editor
$(\'#edit-slug-box\').append(\'<span id="check-title-btn"><a class="button" href="#">Check Title</a></span>\');
// Click function to initiate post function
$(\'#check-title-btn a\').click(function() {
var title = $(\'#title\').val();
var id = $(\'#post_ID\').val();
checkTitle(title, id);
});
});