This tutorial provides the steps to register a custom condition so element’s can be output based on the slug of the current post / page.
Step 1
Install and activate Code Snippets plugin.
Go to Snippets > Add New.
Title: Post Slug Condition
if ( function_exists( 'oxygen_vsb_register_condition' ) ) {
global $oxy_condition_operators;
oxygen_vsb_register_condition(
// Condition Name
'Post Slug',
// Values: The array of pre-set values the user can choose from.
// Set the custom key's value to true to allow users to input custom values.
array(
'options'=>array(),
'custom' => true
),
// Operators
$oxy_condition_operators['string'],
// Callback Function: Name of function that will be used to handle the condition
'lit_slug_fallback',
// Condition Category: Default ones are Archive, Author, Other, Post, User
'Post'
);
}
/**
* Callback function to handle the condition.
* @param mixed $value Input value - in this case, string 'slug'
* @param string $operator Comparison operator selected by the user, either contains or does not contain
*
* @return boolean true or false.
*/
function lit_slug_fallback( $value, $operator ) {
$current_post_slug = get_post_field( 'post_name', get_post() );
$value = (string) $value;
$current_post_slug = strtolower($current_post_slug);
$value = strtolower($value);
if ($operator == "==") {
if ($current_post_slug == $value) {
return true;
} else {
return false;
}
} else if ($operator == "!=") {
if ($current_post_slug != $value) {
return true;
} else {
return false;
}
} else if ($operator == 'contains') {
if (strpos($current_post_slug, $value) !== false) {
return true;
} else {
return false;
}
} else if ($operator == 'does not contain') {
if (strpos($current_post_slug, $value) === false) {
return true;
} else {
return false;
}
}
}
Set the snippet to run everywhere. Save changes and activate.
The condition will be found under ‘Post’. If it needs to be used with a specific post type only, this can be added a seperate condition using the ‘AND’ condition type.