Support » Plugins » How to use slug within a plugin?

  • Hi,

    I need to create a products plugin that will show products based on a slug. The database will be something like this:0

    id, INT
    title, varchar(200)
    description, TEXT
    slug, varchar(200)

    A slug will have the form product-name (eg small-box-of-chocolates).

    When someone clicks the product it will load the page /small-box-of-chocolates which will load the content from the product with slug small-box-of-chocolates

    Can someone give me a few pointers please


    [No bumping. If it’s that urgent, consider hiring someone.]

Viewing 11 replies - 1 through 11 (of 11 total)
  • What part of this problem are you trying to solve?

    The main thing is the ability to read the url and use this to search the products table. If it finds a matching slug in the products table it will load a template. Within that template I could then display the product details based on the slug

    Thanks 🙂

    You are still not giving enough information. How is this plugin supposed to work?

    I basically want it to store products in a database. There will be a table for products, one for categories and one to associate products with one or more categories.

    There will be a function within the plugin to list categories. Clicking a category will list it’s products and clicking a product will show it’s details.

    The only thing I’m struggling with is, when a product is clicked, getting the url and comparing it with the slug of a product. It will then load a product template and load the product details.

    I guess I’m asking, at what point does wordpress get the url and determine whether it’s a page, post or whatever or whether to load 404.php. It’s at this point I can say ‘also check my table to see whether the url (slug part) belongs to a product. If so, load template product.php’


    A really good case could be made for custom post types, but that is another story.

    What is “template product.php”? Is that a theme template? Something in the plugin? Something standalone?

    I honestly don’t have an answer for you. pre_get_posts may do it.

    Hi there,

    yeah, products.php would be a theme template that is loaded if the url/slug refers to a product type (rather than a post). Within this product template I could then pull the specific product information from the product table.



    By the time you get to pre_get_posts WordPress will have parsed the request and sorted it out some for you. That should work. You could interrupt things much sooner, say at init, look at the $_SERVER array and save the server some processing.

    How do you know if the URL refers to a product type?

    I was planning on storing the url as a slug in the database so each product would have a unique slug.

    I was then going to check the URL to see if this matched with a slug from the products table and if so load the associated product.


    this would then match with a slug in the products table small-box-of-chocolates.

    At this point I could load a template within the theme directory which would contain code to look up the relevant product (with slug small-box-of-chocolates) and display it’s data.

    Can I use pre_get_posts() to do this? If there’s a match between URL and a product slug then I need to to load the product.php template file located in the theme directory

    1. I have to re-iterate that there is a really good case for custom post types here
    2. If all of your products are at “”, you don’t really need to check the slugs at all. You only need to check for the ‘products’ part, which is good because otherwise you are running an extra MySQL query every time a page loads. However, I do think you are going to have a lot of trouble because your ‘products’ pages look like WordPress pages but aren’t (again, good case for custom post types). I expect you are going to have to mess around with Wp_Rewrite.

    If I were to use Custom Post Types then could I store the additional data in it’s own table rather than use wp_postmeta?

    For example a product might have the following attributes associated with it: size, brand, weight etc,

    With custom post types it’s normal to store this additional data in the wp_postmeta table with a reference to the wp_post id. I would rather keep this data separate from the wp_postmeta. Is this possible? How would I add the additional fields to the admin panel and save them in a table other than wp_postmeta?

    Thanks for your help

    If I were to use Custom Post Types then could I store the additional data in it’s own table rather than use wp_postmeta?

    Yes, you can. You can store data anywhere you want, except for the data the WordPress needs to operate, of course. If you are building a plugin for release and you want the plugin to be available here, you have a lot more to worry about.

    Based on what you are telling me, I’d think you probably want a custom post type without any default interface at all– that is, without the post-like menu panel on the left. Just make your own control menu for your post type.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to use slug within a plugin?’ is closed to new replies.