Hello,
As you know, WordPress automatically adds suffix in the end of permalink (post name) if a post with the same name already exists in the database.
Example:
I create: http://www.mywebsite.com/mypost
Then try to create with same title, it goes: http://www.mywebsite.com/mypost-2
I don't want to put a suffix, instead i need it to give a warning to the user saying that "the title you are trying to create is already published" . It would be nice, if this warning comes on another page and after clicking ok sends the user to the post-edit page again..
I will use this mechanism, in order to have unique titles in my database and prevent duplicating.
*There is an old "singular" plug-in which have a similar code to what i want. But it just deletes the new one without saying anything.
Any suggestions? I think i need a custom code here..
Solution - 1
Try to add this to your functions.php:
function check_post_uniqueness($id)
{
global $wpdb;
$post_title = $wpdb->get_var( $wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE ID = '{$id}' LIMIT 1") );
$wpdb->query("SELECT ID FROM {$wpdb->posts} WHERE post_title = '{$post_title}' LIMIT 1");
if ( $wpdb->num_rows > 0 ) {
echo 'A post with this title already exists.';
}
return $id;
}
add_action('publish_post', 'check_post_uniqueness', 8);
add_action('edit_post', 'check_post_uniqueness', 8);
Solution - 2
Hi Onur,
Please try this
1. Copy paste below code to function.php
// include script on admin panel
function onur_js(){
wp_register_script( 'onur', get_template_directory_uri().'/onur.js');
wp_enqueue_script( 'onur' );
}
add_action('admin_print_scripts','onur_js');
// db checking
function onur_checkslug(){
if($_POST['type'] == 'checkslug'){
// convert space to '-'
$title = str_replace(' ','-',$_POST['title']);
// use wp_query to check if givin post exists
if($_POST['post_type'] = 'post'){
// if this post
$check_post = new WP_Query("name=$title");
} else {
// if this page
$check_post = new WP_Query("pagename=$title");
}
if ( $check_post->have_posts() ) :
echo 'duplicate';
else:
echo 'no-duplicate';
endif;
} else {
echo 'error';
}
die;
}
add_action('wp_ajax_checkslug', 'onur_checkslug');
// remove suffix
function remove_unique_slug($permalink){
$clean_permalink = preg_replace("/\-\d+$/",'', $permalink);
return $clean_permalink;
}
add_filter('editable_slug','remove_unique_slug');
2. create js file onur.js place it on your active theme directory. Copy paste code below to it.
/* livequery */
(function(a){a.extend(a.fn,{livequery:function(e,d,c){var b=this,f;if(a.isFunction(e)){c=d,d=e,e=undefined}a.each(a.livequery.queries,function(g,h){if(b.selector==h.selector&&b.context==h.context&&e==h.type&&(!d||d.$lqguid==h.fn.$lqguid)&&(!c||c.$lqguid==h.fn2.$lqguid)){return(f=h)&&false}});f=f||new a.livequery(this.selector,this.context,e,d,c);f.stopped=false;f.run();return this},expire:function(e,d,c){var b=this;if(a.isFunction(e)){c=d,d=e,e=undefined}a.each(a.livequery.queries,function(f,g){if(b.selector==g.selector&&b.context==g.context&&(!e||e==g.type)&&(!d||d.$lqguid==g.fn.$lqguid)&&(!c||c.$lqguid==g.fn2.$lqguid)&&!this.stopped){a.livequery.stop(g.id)}});return this}});a.livequery=function(b,d,f,e,c){this.selector=b;this.context=d;this.type=f;this.fn=e;this.fn2=c;this.elements=[];this.stopped=false;this.id=a.livequery.queries.push(this)-1;e.$lqguid=e.$lqguid||a.livequery.guid++;if(c){c.$lqguid=c.$lqguid||a.livequery.guid++}return this};a.livequery.prototype={stop:function(){var b=this;if(this.type){this.elements.unbind(this.type,this.fn)}else{if(this.fn2){this.elements.each(function(c,d){b.fn2.apply(d)})}}this.elements=[];this.stopped=true},run:function(){if(this.stopped){return}var d=this;var e=this.elements,c=a(this.selector,this.context),b=c.not(e);this.elements=c;if(this.type){b.bind(this.type,this.fn);if(e.length>0){a.each(e,function(f,g){if(a.inArray(g,c)<0){a.event.remove(g,d.type,d.fn)}})}}else{b.each(function(){d.fn.apply(this)});if(this.fn2&&e.length>0){a.each(e,function(f,g){if(a.inArray(g,c)<0){d.fn2.apply(g)}})}}}};a.extend(a.livequery,{guid:0,queries:[],queue:[],running:false,timeout:null,checkQueue:function(){if(a.livequery.running&&a.livequery.queue.length){var b=a.livequery.queue.length;while(b--){a.livequery.queries[a.livequery.queue.shift()].run()}}},pause:function(){a.livequery.running=false},play:function(){a.livequery.running=true;a.livequery.run()},registerPlugin:function(){a.each(arguments,function(c,d){if(!a.fn[d]){return}var b=a.fn[d];a.fn[d]=function(){var e=b.apply(this,arguments);a.livequery.run();return e}})},run:function(b){if(b!=undefined){if(a.inArray(b,a.livequery.queue)<0){a.livequery.queue.push(b)}}else{a.each(a.livequery.queries,function(c){if(a.inArray(c,a.livequery.queue)<0){a.livequery.queue.push(c)}})}if(a.livequery.timeout){clearTimeout(a.livequery.timeout)}a.livequery.timeout=setTimeout(a.livequery.checkQueue,20)},stop:function(b){if(b!=undefined){a.livequery.queries[b].stop()}else{a.each(a.livequery.queries,function(c){a.livequery.queries[c].stop()})}}});a.livequery.registerPlugin("append","prepend","after","before","wrap","attr","removeAttr","addClass","removeClass","toggleClass","empty","remove");a(function(){a.livequery.play()})})(jQuery);
jQuery(document).ready(function($){
var titledata, duplicatemessage = "_________________________________________________\nWarning!\n\nPermalink of this content is already use by other post\nPlease edit the permalink before publish.\n_________________________________________________",
checkdb = function(){
var uri = window.location.toString();
if(uri.match(/&action=edit/)) return;
$.post(ajaxurl , titledata, function(message){
switch(message){
case 'duplicate':
alert(duplicatemessage);
$('#save-post,#publish').css('display','none');
break;
case 'no-duplicate':
$('#save-post,#publish').css('display','block');
break;
}
});
};
$('#edit-slug-buttons .save').livequery(function(){
$('#edit-slug-buttons .save').click(function(){
titledata = { type: 'checkslug', action: 'checkslug', title: $('#new-post-slug').val(), post_type: typenow };
checkdb();
});
});
$('#title').change(function(){
titledata = { type: 'checkslug', action: 'checkslug', title: $(this).val(), post_type: typenow };
checkdb();
})
});
Since the suffix added from title of new post this script will work on new post only.
It will check and alert you if similar slug exists, hide the publish and save draft button preventing any damage to existing post, until the permalink edited to other slug. You can have similar title but not permalink/slug.
But please notice since I use regular pattern to replace default suffix, if you have new post and using '-' + number at the end (for example 'My title-12') the number wont be included in permalink automatically(should edited manually).
Hope this help.
Christianto
Solution - 3
Hi,
This is my reply to your email requesting me to answer this question.
I did not answer this question because I think it cannot be done.
I may be wrong, but these are my reasons.
Open up your post.php under wp-includes of your WordPress core code,
you will find a function wp_unique_post_slug() at line 2771.
This is the function that is adding the suffix if permalink duplicates.
This function is being used in wp_insert_post()
There is no action or filter for you to replace it or add your own rule,
excerpt 3 filters in the if condition check. But they are being put at the last condition.
They are there for you to add more conditions and not to overwrite the suffix adding.
so if there is a duplicated permalink, the first condition of the if condition check will be met and the last filter gets ignored and a suffix is added to the permalink.
Cristiano, provided the closes working solution, although his solution does not prevent the creation of a suffix draft copy of the post, but his jQuery script did create an alert box warning and prevent user from publishing unless the user changes to a new slug.
But his answer is still not perfect, because you are still able to bypass it, by editing the draft copy and publish it, the suffix will still be added to the permalink.
Having said so much, I think eventually you will ask for a refund after wasting everybody's effort here. But I am sure someone will challenge your refund request, because I think it is only fair to reward those that put in their effort to answer your "cannot be achieved" question.
Thanks.
Denzel
Solution - 4
Possible solution from wamitchell at [[LINK href="http://wordpress.org/support/topic/admin_notices-on-save_post"]]http://wordpress.org/support/topic/admin_notices-on-save_post[[/LINK]]
HTH
|