WordPress Conditional Tag if URL Contains a String

This is a useful workaround for times when it’s not convenient or possible to use standard conditional tags to insert new elements to a page.

I found this while working with the WMPUDEV Fundraising plugin. My client wanted to be able to request additional information from donors. I thought it would be simple enough to add a Gravity Form.

Either OrUnfortunately, the ‘thank-you’ page was generated dynamically by the plugin, and there wasn’t a page template file to modify, several lines in the main PHP file output the ‘thank-you’ page. So, I couldn’t use the WordPress conditional tag to select by page template.

In addition, each ‘thank-you’ page was unique – generated using post-ID associated with the donators donation. That also eliminated the standard WordPress conditional tags to select by page ID.

But – each ‘thank-you’ page url shared a common structure. The URLs looked like this:

http://domain.com/fundraisers/fundraiser-name/thank-you/?pledge_id=randomdigits

No other pages on the website were ever (likely) to use the phrase ‘thank-you.’ So, I searched out a way to use that phrase within the url as the criteria for a conditional statement. With help from this post, I was able to setup a tidy solution:

[PHP]

add_action(“genesis_entry_content”, “jpro_add_donor_form”, // for Genesis Themes, add the action to content
25); // priority 25 results in the form registering below the standard page content
function jpro_add_donor_form() { // the function

$uri = $_SERVER[“REQUEST_URI”]; // get the URL
$uri_array = split(“/”,$uri); // split the URL at each slash to get an array
$uri_first = $uri_array[3]; // select the third split

if ($uri_first == ‘thank-you’){ // create our if statement

// echo the Form shortcode and it’s container
echo ‘ 

‘. do_shortcode( ‘

Oops! We could not locate your form.

‘ ) .’

‘;

}
}

[/PHP]