roots/support
General-purpose helper functions used across Roots WordPress projects. Includes Laravel-style env() lookup, value() for closures, utilities to add/remove callbacks across multiple WordPress actions/filters, and a wp_die() wrapper with Roots defaults.
Installation Add the package via Composer:
composer require roots/support
No additional configuration is required—it’s a standalone helper library.
First Use Case Quickly sanitize user input or generate slugs:
use Roots\Support\Str;
$sanitized = Str::sanitize('user@input.com'); // "userinputcom"
$slug = Str::slug('Hello World!'); // "hello-world"
Where to Look First
Roots\Support\Str, Roots\Support\Arr, Roots\Support\Path, and Roots\Support\Env.app/Helpers.php for common aliases.String Manipulation
Use Str for consistent string handling (e.g., truncation, sanitization, or formatting):
$truncated = Str::limit('A very long string...', 20); // "A very long strin..."
$titleCase = Str::title('hello world'); // "Hello World"
Array Utilities
Leverage Arr for deep array operations (e.g., merging, plucking, or filtering):
$merged = Arr::merge(['a' => 1], ['b' => 2]); // ['a' => 1, 'b' => 2]
$values = Arr::pluck($users, 'name'); // Extract all 'name' values
Path Handling
Use Path for filesystem operations (e.g., resolving paths or checking existence):
$absolutePath = Path::make('app/Helpers.php')->absolute();
if (Path::exists($absolutePath)) { ... }
Environment Handling
Use Env for environment variable access, with fallback to Laravel's Env:
$value = Env::get('APP_ENV'); // Falls back to Illuminate\Support\Env::get()
WordPress-Specific Helpers If integrated into a Roots WordPress project, use helpers like:
use Roots\Support\WordPress;
$postTitle = WordPress\getPostTitle($postId);
$featuredImage = WordPress\getFeaturedImageUrl($postId);
// app/Providers/AppServiceProvider.php
public function boot()
{
$this->app->singleton(Str::class, fn() => new Str());
$this->app->singleton(Arr::class, fn() => new Arr());
$this->app->singleton(Path::class, fn() => new Path());
$this->app->singleton(Env::class, fn() => new Env());
}
use Roots\Support\Str;
class CustomStr extends Str {
public static function customMethod($input) { ... }
}
$this->partialMock(Str::class, ['slug']);
Namespace Conflicts
roots/support may clash with Laravel’s built-in helpers (e.g., Str::of() vs. Roots\Support\Str::slug()).use Roots\Support\Str as SupportStr;
Roots\Closure was fixed to avoid conflicts with PHP's native Closure.Environment Handling Quirks
Env::get() now attempts to use Illuminate\Support\Env::get() as a fallback. Ensure compatibility:
if (class_exists('Illuminate\Support\Env')) {
$value = Env::get('APP_ENV');
}
WordPress-Specific Assumptions
WordPress\getPostTitle()) assume a WordPress environment.if (function_exists('get_post')) {
$title = WordPress\getPostTitle($postId);
}
Path Resolution Quirks
Path::make() uses Laravel’s storage_path() by default. For custom paths:
$customPath = Path::make('custom/dir')->to('/absolute/path');
Str::setDebug(true); // Logs operations to Laravel logs
Add Custom Methods Override or extend classes via traits:
use Roots\Support\Str;
trait CustomStrMethods {
public static function kebab($value) {
return Str::slug($value, '-');
}
}
class ExtendedStr extends Str {
use CustomStrMethods;
}
Configure Defaults
Set global defaults in config/support.php (if the package supports it):
'default_locale' => 'en_US',
'sanitize_regex' => '/[^a-z0-9]/i',
Leverage Laravel Facades Create facades for cleaner syntax:
// app/Facades/SupportStr.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
use Roots\Support\Str;
class SupportStr extends Facade {
protected static function getFacadeAccessor() { return Str::class; }
}
Usage:
SupportStr::slug('Hello'); // "hello"
How can I help you explore Laravel packages today?