When we add new post in wordpress, after supplying the post title, the slug is generated automatically. I need to edit that auto generation module so that i can add some arbitrary number in the end of the slug automatically. How to do it?
Leave a Reply
You must be logged in to post a comment.
Don’t use the hard-coded version that the OP used here. When he did that, there was not a filter available. More recently, since 3.3, a filter was added.
However this method will change the slug every time you save the post… Which was what I was hoping for…
EDIT:
This kind of works for limiting the generation to just once. The only drawback is that it creates one version when ajax runs after creating the title, then it creates another, permanent slug when the post is saved.
Write a plugin to hook into the
wp_insert_post_data
filter so you can update the slug before the post is sent for insertion into the database:Note that this function requires that you allow WordPress to auto-generate the slug first, meaning you must not enter your own slug before generating, and it cannot update existing posts with the number.
Test this : (paste it into functions.php)
Untested in that scenario, but I have already used it, should work with a minimum of changes, but try and test it REALLY Carefully .
In any Case, don’t use
rand()
here or something alike, since the function must return the same link for the same post every time, otherwise you will have some serious problems.Have fun!
You should operate with
wp_ajax_sample-permalink
action andname_save_pre
filter.More examples here: https://wordpress.stackexchange.com/a/190314/42702