Show Content Based on URL Parameter

Last update:

Displaying content conditionally is one of the nicest feature of Oxygen Builder. There are many baked in conditions to play with, however, URL parameters is not among them. With this code snippet you can exactly do just that.

Add Query Variable

/* Add query variable for conditional displaying */
add_action('init','add_get_val');
function add_get_val() { 
    global $wp; 
    $wp->add_query_var('from'); 
}

With this piece of code you add from query variable to the list of public query variables, as described in WordPress Code Reference. Note that you can add more variables by duplicating $wp->add_query_var(‘from’); line. (See the snippet below.) Do not forget to replace from to another variable.

/* Add query variable for conditional displaying */
add_action('init','add_get_val');
function add_get_val() { 
    global $wp; 
    $wp->add_query_var('from');
    $wp->add_query_var('to');  
}

After that you can use the WordPress function of get_query_var to retrieve the value of the query variable you have just registered.

Register Condition

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {		
	global $oxy_condition_operators;

	oxygen_vsb_register_condition(
		'URL Parameters',
		array('custom'=>true),
		array('=='),
		'urlparameters_callback',
		'Other'
	);
}

function urlparameters_callback( $value, $operator ) {
    $whatisinurl = get_query_var('from');
    global $OxygenConditions;
    return $OxygenConditions->eval_string($whatisinurl, $value, $operator);
}

The above code is an Oxygen Builder feature. In their documentation they explain what the different part of this snippet means. I just would like to highlight a few particles:

  • ‘URL Parameters’ – Display name of the condition. Your choice.
  • ‘urlparameters_callback’ – Name of the callback. Your choice. Don’t forget to replace this name in the second part of the code.
  • Don’t forget to replace from to your variable.

Register Multiple Conditions

Many times we would like to display content based on multiple URL parameters. So far I have found this as a working solution:

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {		
	global $oxy_condition_operators;

	oxygen_vsb_register_condition(
		'URL Parameter 1',
		array('custom'=>true),
		array('==', '!='),
		'urlparameters_callback_1',
		'Other'
	);
	
	oxygen_vsb_register_condition(
		'URL Parameter 2',
		array('custom'=>true),
		array('==', '!='),
		'urlparameters_callback_2',
		'Other'
	);
}

function urlparameters_callback_1( $value, $operator ) {
    $whatisinurl = get_query_var('from');
    global $OxygenConditions;
    return $OxygenConditions->eval_string($whatisinurl, $value, $operator);
}

function urlparameters_callback_2( $value, $operator ) {
    $whatisinurl = get_query_var('to');
    global $OxygenConditions;
    return $OxygenConditions->eval_string($whatisinurl, $value, $operator);
}

Please note:

  • The names of conditions are different.
  • Callback function name is different.
  • $whatisinurl variable values in the callback functions are different.

Unfortunately, I haven’t found a more compact solution, but this one will definitely work. I hope you liked it and had success with the implementation.