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 his 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. Do not forget to replace from to another variable.

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.

I hope you liked it and had success with the implementation.