• Hello,
    I created a hook to update the post slug (post_name).
    As far as I can see it works fine.
    Do I need some “Sanitizing” of the post_name? Like sanitize_title(get_the_title( $post_id ) ) ?

    		$update_slug = array(
    			'ID' => $post_id,
    			'post_name' => get_the_title( $post_id )
    		);
    		wp_update_post( $update_slug );

    Thanks for some clearing answers,
    Cheers,
    Denis

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You should be OK without, the title would have been sanitized prior to placing in the DB. However, since the slug is part of an URL and the title is not intended to be, run the value through rawurlencode()

    What is it about slugs that you find objectionable? I ask because blindly setting the title as slug can cause problems. Using rawurlencode() probably would address most problems, but may do things to the title related to your objections, so done properly, you may not be solving the problem. What is not solved is the possibility of the same slug being applied to more than one post. This must be addressed somehow.

    It may be that wp_update_post() might still address these, IDK. What causes your code to execute? A hook I assume, so which hook? Depending on the hook used and your intent, there may be a better way to do this. But if wp_update_post() addresses what I see as potential problems, what I have in mind may not. I may be simplifying the overall code, but causing more work for you. I can determine the answers if I know more of what you’re really trying to do.

    You could also disregard my concerns and proceed as is. It’s your site after all 😉

    Thread Starter DenisCGN

    (@deniscgn)

    @bcworkz

    Hi bcworkz,

    I use this code above…and it seems to work very well. If titles are double it adds a -2. and if I use for example German charachters the will be change to oe,ae & ue even if I use something like ‘#<> the titel will be changed the way I enter it the first time in a new post.

    So I think, the sanitizing is done in the wp_update_post function.

    Cheers,
    Denis

    Moderator bcworkz

    (@bcworkz)

    Yes, you’re fine the way it is. I was mainly inquiring because using wp_update_post() just to change the slug is not very efficient. But being efficient means getting more involved in little details. It’s probably not worth considering unless your site gets so large and heavily used little things like this start to make a difference. You can address it then 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Post_Name and wp_update_post’ is closed to new replies.