WpMVC Helpers provides a collection of static utility methods to simplify common WordPress tasks such as file uploads, plugin metadata, array operations, request handling, and data sanitation.
These methods are framework-agnostic and can be used across any part of your WpMVC-based plugin.
To install the Helpers package:
composer require wpmvc/helpersnamespace WpMVC\Helpers;
class HelpersRetrieves the version of a plugin based on its slug.
$version = Helpers::get_plugin_version('my-plugin');Returns null if the plugin file doesn't exist or version is not found.
Handles file upload and optionally creates a WordPress media attachment.
$attachment_id = Helpers::upload_file($_FILES['my_file']);- Returns attachment ID if
$create_attachmentistrue - Returns file metadata array if
false - Throws
Exceptionon upload failure
Deletes one or multiple media attachments by ID(s).
Helpers::delete_attachments_by_ids([10, 12]);Returns an array of successfully deleted attachment IDs.
Creates and populates a WP_REST_Request instance using global request data.
$request = Helpers::request();Populates:
- Query params from
$_GET - Body from
$_POST - Files from
$_FILES - Headers from
$_SERVER - Raw body data
Attempts to decode a JSON string if valid; returns original value if not.
$data = Helpers::maybe_json_decode('{"foo": "bar"}');Checks whether the array contains only scalar values (no nested arrays).
Helpers::is_one_level_array(['a', 'b']); // true
Helpers::is_one_level_array(['a' => ['nested']]); // falseRecursively merges two arrays.
$merged = Helpers::array_merge_deep($a, $b);Preserves nested structures instead of overwriting them.
Removes all null values from an array.
$data = Helpers::remove_null_values([
'a' => 'value',
'b' => null,
]); // ['a' => 'value']Detects the user's IP address, accounting for proxies and headers.
$ip = Helpers::get_user_ip_address();Checks in order:
HTTP_CLIENT_IPHTTP_X_FORWARDED_FORREMOTE_ADDR
Returns null if none are valid.
| Method | Purpose |
|---|---|
get_plugin_version() |
Reads plugin version from header |
upload_file() |
Uploads file + optionally creates media attachment |
delete_attachments_by_ids() |
Deletes one or more attachments by ID |
request() |
Creates a populated WP_REST_Request from global input |
maybe_json_decode() |
Safely attempts to decode JSON |
is_one_level_array() |
Checks for nested arrays |
array_merge_deep() |
Deep merges two arrays recursively |
remove_null_values() |
Removes all null values from an array |
get_user_ip_address() |
Gets real client IP considering proxies |