I would like WordPress to handle an incoming url with a variable parameter and not throw a 404. Basically, an incoming url could look like this:
http://mysite.com/custom-post-type/some-post/?promo=12er34
I then pass that last segment into a signup form for promotions. Obviously, hitting this url directly would throw a 404.
Is there anyway to setup a conditional in my custom post type that would allow a match if the incoming url has an additional specified segment?
In your custom post-type template, you could do this:
$code = isset( $_GET[ 'promo' ] ) ? sanitize_text_field( $_GET[ 'promo' ] ) : '';
By the way, the parameter in your url should have a ?
before promo
.
Like this: http://mysite.com/custom-post-type/some-post/?promo=12er34
Update:
If you want to get this value regardless of whether you're on a custom post-type or the default post-type, try this in your functions.php
:
function get_promo_code( $wp ) {
if( ! is_single() )
return;
// Do whatever you want with the promo code here.
$code = isset( $_GET[ 'promo' ] ) ? sanitize_text_field( $_GET[ 'promo' ] ) : '';
}
add_action( 'wp', 'get_promo_code' );