Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Created August 17, 2024 18:17
Show Gist options
  • Save Lonsdale201/dc6768cd3a3e3f6d929e53f8c0b00043 to your computer and use it in GitHub Desktop.
Save Lonsdale201/dc6768cd3a3e3f6d929e53f8c0b00043 to your computer and use it in GitHub Desktop.
JetEngine - Dynamic visibility - Check Url Path
// place the code in the child theme functions.php or a custom code snippets plugin.
// Open an widget, dynamic visibility, and search the Check Url Path visibility under the Custom category.
// This visibility condition function checks whether the specified text is present in the current page's URL. For example:
// example.com/test/test2
// If you specify "test2" in the visibility settings (value field), the rule will be applied whenever "test2" is found anywhere in the current page's URL.
add_action( 'jet-engine/modules/dynamic-visibility/conditions/register', function( $conditions_manager ) {
class Check_URL_Path_Condition extends \Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base {
public function get_id() {
return 'check-url-path';
}
public function get_name() {
return __( 'Check URL Path', 'jet-engine' );
}
public function get_group() {
return 'custom';
}
public function check( $args = array() ) {
$value_to_check = isset( $args['value'] ) ? $args['value'] : '';
$current_url = $_SERVER['REQUEST_URI'];
$type = isset( $args['type'] ) ? $args['type'] : 'show';
$is_param_present = strpos( $current_url, $value_to_check ) !== false;
return ( 'hide' === $type ) ? !$is_param_present : $is_param_present;
}
public function is_for_fields() {
return false;
}
public function need_value_detect() {
return true;
}
}
$conditions_manager->register_condition( new Check_URL_Path_Condition() );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment