Join the Voxel Guide Community!

Get Involved

Add Strip URL dynamic tag mod

Description/Instructions

Add this snippet into your child theme’s functions.php file to make this mod work in your Voxel 1.5 dynamic tags. This will remove the content of the url before the last slash.

Instructions

Add this to your functions file in child theme.

// STRIP url from all but last slash content
add_action('voxel/dynamic-data/modifiers', function( $modifiers ) {
class Strip_Before_Last_Slash extends \Voxel\Dynamic_Data\Modifiers\Base_Modifier {
public function get_label(): string {
return _x('Strip Before Last Slash', 'modifiers', 'voxel-backend');
}
public function get_key(): string {
return 'strip_before_last_slash';
}
public function expects(): array {
return [ static::TYPE_STRING ];
}
protected function define_args(): void {
// No arguments needed for this modifier
}
public function apply( string $value ) {
// Validate that the value is a valid URL
if ( filter_var( $value, FILTER_VALIDATE_URL ) === false ) {
return esc_url( $value );
}
// Remove query parameters if present
$value = strtok( $value, '?' );
// Find the position of the last slash and extract the substring after it
$last_slash_pos = strrpos( $value, '/' );
// If no slash is found, return the original value
if ( $last_slash_pos === false ) {
return esc_url( $value );
}
// Extract everything after the last slash
$modified_value = substr( $value, $last_slash_pos + 1 );
// Return the modified value with proper URL escaping
return esc_url( $modified_value );
}
}
$modifiers['strip_before_last_slash'] = Strip_Before_Last_Slash::class;
return $modifiers;
});

  • PHP
Copy Code

Let's Chat About this Snippet

Chat Toggle
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Ask me anything about Voxel!
Send
Powered by AI24