WordPress.org

Ready to get started?Download WordPress

Forums

How to use slug within a plugin? (12 posts)

  1. nvaughan84
    Member
    Posted 1 year ago #

    Hi,

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

    Products
    -------
    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

    Thanks

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

  2. s_ha_dum
    Member
    Posted 1 year ago #

    What part of this problem are you trying to solve?

  3. nvaughan84
    Member
    Posted 1 year ago #

    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 :)

  4. s_ha_dum
    Member
    Posted 1 year ago #

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

  5. nvaughan84
    Member
    Posted 1 year ago #

    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'

    Cheers

  6. s_ha_dum
    Member
    Posted 1 year ago #

    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.

  7. nvaughan84
    Member
    Posted 1 year ago #

    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.

    Cheers,

    Nick

  8. s_ha_dum
    Member
    Posted 1 year ago #

    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?

  9. nvaughan84
    Member
    Posted 1 year ago #

    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.

    eg http://www.mysite.com/products/small-box-of-chocolates

    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

  10. s_ha_dum
    Member
    Posted 1 year ago #

    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 "http://please-use-real-domain-names-because-fake-ones-make-helping-hard.com/products/", 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.
  11. nvaughan84
    Member
    Posted 1 year ago #

    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

  12. s_ha_dum
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags