Duplicate Post Name In WordPress
by Hui Wang
In WordPress 2.8.4, from the function wp_unique_post_slug in line 1730 in wp-include/post.php, we can see that the post_name is not allowed to be duplicated.
Given the desired slug and some post details computes a unique slug for the post.
For instance,(the idea comes from perfectlyclear)
2008/07/dress-of-the-day.html is a page that exists and is viewable on the site
but if I try to add a new post of the same title (note the different dates)
2009/08/dress-of-the-day.html the moment you hit “update post” the URL would changes to2009/06/dress-of-the-day-1.html
and if you have lots of posts entitled dress-of-the-day.html, then the increments keep getting larger i.e. -2 -3 etc.
So to achieve our goal, we have to trim the suffix before it is written to the database while updating or adding a post with a duplicated name.
In this case, I’d like to introduce you the conception of filter in WordPress.
Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data (such as adding it to the database or sending it to the browser screen). Filters sit between the database and the browser (when WordPress is generating pages), and between the browser and the database (when WordPress is adding new posts and comments to the database); most input and output in WordPress passes through at least one filter. WordPress does some filtering by default, and your plugin can add its own filtering.
Please see further explanation in the following short code.
1 2 3 4 5 6 7 8 9 10 11 | <?php add_filter( 'wp_insert_post_data' , 'wh_duplicat_post_name' , '99' ); function wh_duplicate_post_name( $data) { $wh_post_name=$data['post_name']; if(substr($wh_post_name, -2)=="-2") $data['post_name']=substr($wh_post_name, 0, -2); return $data; } ?> |
To get the code executed, you just simply add it the function.php file in your current theme root directory.
Hopefully it works for you!
Comments
Hello,
When I save the post I get the following Error:
Warning: Missing argument 2 for wh_duplicate_post_name() in /Volumes/ServerData/Web/wordpress/wp-content/themes/s-b/functions.php on line 23
Warning: Missing argument 2 for wh_duplicate_post_name() in /Volumes/ServerData/Web/wordpress/wp-content/themes/s-b/functions.php on line 23
Warning: Cannot modify header information – headers already sent by (output started at /Volumes/ServerData/Web/wordpress/wp-content/themes/s-b/functions.php:23) in /Volumes/ServerData/Web/wordpress/wp-includes/pluggable.php on line 865
Any idea what could be the problem?
Thanks!
Aldo
@Aldo – Hello, Aldo. Regarding for the last warning, you may refer to this post http://www.hui-wang.info/2009/07/warning-cannot-modify-header-information-headers-already-sent/
Feel free to let met know if you still have trouble.
Thanks!
The Header Error is invoked by the first error:
Warning: Missing argument 2 for wh_duplicate_post_name() in /Volumes/ServerData/Web/wordpress/wp-content/themes/s-b/functions.php on line 23
Warning: Missing argument 2 for wh_duplicate_post_name() in /Volumes/ServerData/Web/wordpress/wp-content/themes/s-b/functions.php on line 23
This happens while saving the post.
Any idea?
@Aldo – could you remove the second argument for the function wh_duplicate_post_name and have another try?
The function should look like as follows:
function wh_duplicate_post_name($data)
This works!
Thanks!
Nice site!
Thanks, rhinoplasty. I am very glad that you like it.
I don’t know If I said it already but …This blog rocks! I gotta say, that I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks,
A definite great read..Jim Bean
@JimmyBean – Hello, Jimmy. Thanks for your kind comment. In fact, the purpose of this log is to exchange opinions between readers like you and me. So I hope the site can get us focused on articles other than advertisements. I am very glad the site is giving you this kind of impression.
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘wh_duplicat_post_name’ was given in /home5/referat1/public_html/watchmovies/wp-includes/plugin.php on line 166
Warning: array_keys() [function.array-keys]: The first argument should be an array in /home5/referat1/public_html/watchmovies/wp-includes/wp-db.php on line 739
Warning: Invalid argument supplied for foreach() in /home5/referat1/public_html/watchmovies/wp-includes/wp-db.php on line 741
Warning: implode() [function.implode]: Invalid arguments passed in /home5/referat1/public_html/watchmovies/wp-includes/wp-db.php on line 750
Warning: Cannot modify header information – headers already sent by (output started at /home5/referat1/public_html/watchmovies/wp-content/themes/wm/functions.php:1) in /home5/referat1/public_html/watchmovies/wp-includes/classes.php on line 1586
i use WordPress 2.8.6 and that happens while saving the post.
Hello, thanks for your feedback. Please look at the last warning. I suppose that it’s not the problem of the small function you’ve added. You can refer to another post Warning: Cannot modify header information – headers already sent. Hope it helps.
To fix the errors you’re getting, fix the word ‘duplicat_post_name’. It’s supposed to be ‘duplicate_post_name’.
Good work, and thanks.
[code]
[/code]
Could anybody give an example of how to implement this?
@Drew: Hi Drew, just put this piece of code in your function.php file which is your current theme floder. If you like, wp-content/theme/your_theme. Good luck.
my cousin broke her nose on a bad bike accident and he got a nose job.”*,
asians like to have rhinoplasty because they want taller and thinner noses.’”"