WordPress.org

Ready to get started?Download WordPress

Forums

Change slug with custom field in wordpress (6 posts)

  1. another day
    Member
    Posted 1 year ago #

    I want to change the slug of a post with a custom field.

    In example, if the custom field is "keyword" my post link will become: mysite.com/keyword.

    If the custom field is empty, i want to generate a random key like mysite.com/xV5f7A.

    How can i do that without change the wordpress core?

    Any ideas are welcome! Thanks.

  2. s_ha_dum
    Member
    Posted 1 year ago #

    You need to be careful messing with post slugs. If you change those after a post is published you will generate broken links.

    So, now I've warned you. My conscience is clear. :) Hook into wp_insert_post_data or save_post

    http://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Example

  3. melfelk
    Member
    Posted 1 year ago #

    Actually, I don't think it breaks the link anymore. I change slugs all the time, and WP is smart enough to tell the difference. Not sure when this changed, but I know it was just talked about at WordCamp in SF on Saturday. "It just works" was the comment.

  4. s_ha_dum
    Member
    Posted 1 year ago #

    There is a function in query.php that tries to compensate and does do a pretty good. I've been playing with it this morning. The system tracks old slugs in wp_postmeta under meta_key '_wp_old_slug'. That was much needed.

  5. another day
    Member
    Posted 1 year ago #

    I tried your solution since two day and i don't success. I lost all my hair. Can you give me an example?

  6. s_ha_dum
    Member
    Posted 1 year ago #

    What does your code look like?

Topic Closed

This topic has been closed to new replies.

About this Topic