VEIT JOSEF SCHNEIDER
UX Design | Visual Design | Audio Design

Strip URL content before the last slash mod in Voxel

Category:

Tags:

Sometimes, we only need the last part of a URL—perhaps for display purposes or to use it in other parts of our theme. Instead of manually extracting it each time, we can create a simple function in our functions.php file to automate the process. In this tutorial, I’ll show you how to add a custom modifier to your Voxel child theme that strips all content from a URL before the last slash.

Here’s a quick mod to achieve this:

BEWARE: You need to have Voxel 1.5 installed to make those new mods work.

Step-by-Step Guide:

  1. Open Your Child Theme’s functions.php File: You’ll need to access your child theme’s functions.php file. This file is typically located under /wp-content/themes/your-child-theme/.
  2. Add the Custom Code: Copy the following code and paste it into the functions.php file. This code defines a new modifier to strip everything from a URL before the last slash, ensuring you only get the part of the URL that follows the last slash.

The Code:

// 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;
});

How It Works:

  • The Function: This code defines a new class Strip_Before_Last_Slash inside a voxel/dynamic-data/modifiers action hook. The function first checks if the value passed is a valid URL and then extracts everything after the last slash. If no slash is found, it simply returns the original URL.
  • Returning the URL: It returns only the part of the URL after the last slash, effectively stripping any extra content. For example, for the URL https://example.com/page/subpage, it will return subpage.

Why Use This Modifier?

  • Simplicity: This method automates the URL manipulation, so you don’t have to manually extract parts of the URL each time.
  • Reusability: Once you add it, you can use this modifier throughout your theme wherever you need it.
  • Performance: It works directly in the backend, ensuring that the modification happens in a streamlined manner.

How to Use:

Once you’ve added this code to your functions.php file, you can apply this modifier using the appropriate dynamic tags within the Voxel theme.

For example, if you’re working with dynamic data in your theme and want to apply this modifier to a URL field, simply use the dynamic tag strip_before_last_slash.

This is a quick and effective way to extract only the last part of a URL in WordPress using Voxel. By adding this simple function to your functions.php file, you save time and simplify your code. Now you can handle URLs more efficiently and get just the data you need!

If you like my tutorials and snippets, and want to motivate me, or just say thanks,
feel free to support me by buying me a tea (as I do not drink coffee) :)

This website may contain affiliate links. If you click on a link and make a purchase, I may earn a small commission at no additional cost to you. This helps support the time and effort put into maintaining this website. I only recommend products and services I genuinely believe will be helpful to you. Thank you for your support!

Created by Veit Josef Schneider | ©2007-2025 All rights reserved