WordPress.org

Ready to get started?Download WordPress

Forums

Custom slugs for plugin (2 posts)

  1. scheda
    Member
    Posted 1 year ago #

    I'm building a plugin that'll use custom slugs to essentially create a URL shortener.

    Right now I've got a new table that'll house the slugs so that when someone goes to, say, domain.com/xyz, I need to be able to look up xyz in the table to see if it exists.

    If so, my plugin will run a particular function.

    I've got everything I need created except one bit...

    How to get the slug.

    The problem is, if you go to domain.com/xyz, it'll load a 404 page.

    I may need to rework everything I've got to create a custom post type, but I'm not sure if that's the best solution.

    Is there a common practice for something like this?

    Thanks in advance for your help!

  2. bcworkz
    Member
    Posted 1 year ago #

    You could handle it like wordpress does permalinks. It has .htaccess rewrite rules that essentially say if the request is not in the server file structure, send it to WP for handling. Then WP examines the request to see if matches the permalink structure that resolves to an actual entry. Only if unsuccessful is a 404 returned.

    You would have to have .htaccess forward such a request to your url parser instead of wordpress. Then only if the url does not exist in your table, pass the request on to WP for further processing, assuming you are using permalinks. Otherwise, immediately return a 404 page.

Topic Closed

This topic has been closed to new replies.

About this Topic