0

I'm working on a custom plugin where the user/customer will be purchasing a ticket (it'll be a woo product) to take a quiz and win some prize...

So suppose the URL of a product/quiz would be like mydomain.tld/product/play-and-win-samsung-galaxy-s22

When the user purchases a ticket to participate in the quiz, I want to redirect them to a page like mydomain.tld/product/play-and-win-samsung-galaxy-s22/play where they will actually take the quiz. Needless to say that in that page the necessary checks will be taking place (like whether the user is logged in and has purchased a ticket)...

So how can I create such a dynamic page (not a PT Page), that's dynamic? By dynamic I mean, it'll be 100% programmatically created. No shortcodes attached to a PT Page, nothing. Just pure code... How do we start such a task? Thank you for any suggestions.

1 Answer 1

1

You need to register a 'rewrite endpoint' using add_rewrite_endpoint():

<?php
add_action(
    'init',
    function() {
        add_rewrite_endpoint( 'play', EP_PERMALINK );
    }
);

Now, after flushing permalinks, in whatever template or code you're using for your products you can check this to determine whether the /play version of the URL is being accessed:

<?php
if ( false !== get_query_var( 'play', false ) ) {
    // True for mydomain.tld/product/play-and-win-samsung-galaxy-s22/play
} else {
    // True for mydomain.tld/product/play-and-win-samsung-galaxy-s22
}
4
  • I did what you suggested and with the help of this plugin I saw that there were added 5 rewrite rules one for product, and 4 for post. Ideally I'd like to add only 1 for the product Post Type, as that's where it'll be used for. In the following comment you will find the 5 rewrites that were added:
    – Faye D.
    Jun 9, 2022 at 14:41
  • 1: lottery/([^/]+)/somethingtotest(/(.*))?/?$ index.php?product=$matches[1]&somethingtotest=$matches[3] product 2: ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/somethingtotest(/(.*))?/?$ index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&somethingtotest=$matches[6] post 3: ([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/somethingtotest(/(.*))?/?$ index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&somethingtotest=$matches[5] post
    – Faye D.
    Jun 9, 2022 at 14:43
  • 4: ([0-9]{4})/([0-9]{1,2})/somethingtotest(/(.*))?/?$ index.php?year=$matches[1]&monthnum=$matches[2]&somethingtotest=$matches[4] post 5: ([0-9]{4})/somethingtotest(/(.*))?/?$ index.php?year=$matches[1]&somethingtotest=$matches[3] post
    – Faye D.
    Jun 9, 2022 at 14:43
  • BTW, somethingtotest is the name of the endpoint I added, for testing purposes obviously...
    – Faye D.
    Jun 9, 2022 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.