0

I am a WordPress and PHP newbie, or at least I haven't coded any PHP since PHP 5 (about 12 years).

Our site needs a custom thank you page with a form embedded. I have done all the research I can and written the following code. Unfortunately I can't test it yet, if I could I would post it on Code Review rather than here.

The Thank You page and the Declined page have been created. . The code is grammatically correct.

Questions:

  1. Is everything I need to code here?
  2. Is the code missing any WordPress best practices?
  3. Is the code breaking any WordPress standards?
  4. Is there a better way to build the target URL with permalinks?

I will be adding the following code to my child theme functions.php

/**
 * 
 * If the credit card was declined redirect to the declined page.
 * Get the billing email address and the number of items in the cart and use them
 * to prefill the input fields in the Name Tag Builder form on the Thank You page.
 *
 * @param int        $order_id  The order ID provided by the woocommerce checkout page.
 *
 * @return string
 */
add_action( 'woocommerce_thankyou', 'bhhs74_thankkyou_add_params_redirect', 1 ); 
function bhhs74_thankkyou_add_params_redirect( $order_id ) {

    $order = wc_get_order( $order_id );

    if ( $order->has_status( 'failed' ) ) {
        $url = site_url( $path = '', $scheme = null )  . "/bhhs-declined/";
        wp_redirect( $url );
    }

    $billing_email = sanitize_email($order->get_billing_email());
    $ticket_count = (int) $order->get_item_count();

    $url = site_url( $path = '', $scheme = null )  . "/thank-you/";
    add_query_arg( 
        array(
            'bhhs74_order_email' => $creditCardEmail,
            'bhhs74_order_item_count' => $ticketCount,
        ), 
        $url 
    );

    wp_redirect( $url );

}
3
  • I see 4 questions, some open ended, can you narrow this down to a single question, and one that can be answered factually without opinions/subjectiveness? Note as well that 3rd party product/plugin questions such as those about WooCommerce are offtopic here and not in this stacks scope
    – Tom J Nowell
    Commented Jul 1 at 14:06
  • @TomJNowell I am not interested in the WooCommerce side, I will assume that the order object exists. I am only interested in the use of sanitization, url construction and redirection to a page.
    – pacmaninbw
    Commented Jul 1 at 14:18
  • I've written an answer but keep in mind this isn't a discussion forum, it's a Q&A site, you need to phrase your question in such a way that it can be answered factually, you're expected to be able to mark an answer as correct, not just because it was a useful opinion, but because it fully answers the question for all users with that question objectively
    – Tom J Nowell
    Commented Jul 1 at 14:22

1 Answer 1

1

Is everything I need to code here?

If it works then yes!

If it does not work then no there is stuff missing.

Only you can answer this by opening the site and checking if it does what it's supposed to do.

Is the code missing any WordPress best practices?

This is a very open ended question and cannot be answered concretely or correctly ( super subjective ).

Note that because this is a WooCommerce filter it might have other things that can't be talked about here specific to that plugin.

There is 1 thing though: When you call wp_redirect you have to call exit; immediatley afterwards.

Is the code breaking any WordPress standards?

This is a very open ended question. The most charitable interpretation is, does this code pass the WPCS ruleset from the PHPCodeSniffer tool?

This is the output using the WPCS v3.1 ruleset:

❯ phpcs wpse-test.php --standard="WordPress"

FILE: wpse-test.php
------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 37 ERRORS AND 4 WARNINGS AFFECTING 22 LINES
------------------------------------------------------------------------------------------------------------------------------------------------------
  1 | ERROR   | [ ] There must be no blank lines before the file comment
  4 | ERROR   | [x] Whitespace found at end of line
 12 | ERROR   | [ ] There must be exactly one blank line after the file comment
 12 | ERROR   | [ ] Missing @package tag in file comment
 13 | ERROR   | [x] Whitespace found at end of line
 14 | ERROR   | [ ] Missing doc comment for function bhhs74_thankkyou_add_params_redirect()
 16 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 18 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 19 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 19 | ERROR   | [ ] Assignments must be the first block of code on a line
 19 | ERROR   | [x] Concat operator must be surrounded by a single space
 19 | ERROR   | [x] String "/bhhs-declined/" does not require double quotes; use single quotes instead
 20 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 20 | WARNING | [ ] wp_redirect() found. Using wp_safe_redirect(), along with the "allowed_redirect_hosts" filter if needed, can help avoid any
    |         |     chances of malicious redirects within code. It is also important to remember to call exit() after a redirect so that no other
    |         |     unwanted code is executed.
 21 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 23 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 23 | ERROR   | [x] Expected 1 spaces after opening parenthesis; 0 found
 23 | ERROR   | [x] Expected 1 spaces before closing parenthesis; 0 found
 24 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 24 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
 26 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 26 | ERROR   | [ ] Assignments must be the first block of code on a line
 26 | ERROR   | [x] Concat operator must be surrounded by a single space
 26 | ERROR   | [x] String "/thank-you/" does not require double quotes; use single quotes instead
 27 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 27 | ERROR   | [x] Whitespace found at end of line
 28 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 29 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 29 | WARNING | [x] Array double arrow not aligned correctly; expected 6 space(s) between "'bhhs74_order_email'" and double arrow, but found 1.
 29 | ERROR   | [ ] Variable "$creditCardEmail" is not in valid snake_case format, try "$credit_card_email"
 30 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 30 | ERROR   | [ ] Variable "$ticketCount" is not in valid snake_case format, try "$ticket_count"
 31 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 31 | ERROR   | [x] Whitespace found at end of line
 32 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 32 | ERROR   | [x] Whitespace found at end of line
 33 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 35 | ERROR   | [x] Tabs must be used to indent lines; spaces are not allowed
 35 | WARNING | [ ] wp_redirect() found. Using wp_safe_redirect(), along with the "allowed_redirect_hosts" filter if needed, can help avoid any
    |         |     chances of malicious redirects within code. It is also important to remember to call exit() after a redirect so that no other
    |         |     unwanted code is executed.
 37 | ERROR   | [x] Expected 1 newline at end of file; 0 found
 37 | ERROR   | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 31 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 112ms; Memory: 10MB

Is there a better way to build the target URL with permalinks?

The way the URLs are being generated is super weird and unnecessary. Take this line:

$url = site_url( $path = '', $scheme = null )  . "/thank-you/";

$path = ''; is super weird, it looks like this was copy pasted from the reference doc.

This would be the cleaner equivalent:

$url = site_url( '', null )  . '/thank-you/';

and since those are the default values they can be removed:

$url = site_url()  . '/thank-you/';

But even this has issues as the path is being appended separately despite there being a dedicated parameter for it, so this is the more correct version:

$url = site_url( 'thank-you' );

The same goes for rest_url and home_url etc..

You could try to use get_permalink to fetch the full URL of the thankyou page but this would require you to either store the post ID of the thankyou page, or query the database to find it. I don't think either of these are optimal.

2

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.