pollora/wordpress-args
WordPress Arguments turns an object into the arguments array that WordPress functions expect (register_post_type(), register_taxonomy(), WP_Query…). Add the ArgumentHelper trait to a class, give it one property per argument, and it builds the array for you: property names become snake_case keys, unset properties are left out, and raw arguments can be merged on top. It is for package authors who want a fluent, typed API over WordPress arguments instead of hand-written arrays.
Part of Pollora, the Laravel framework for WordPress. In a Pollora project it is already installed, as the base of pollora/query and pollora/entity.
Installation
composer require pollora/wordpress-args
Requires PHP 8.2+ and illuminate/support 12 or 13. Building the arguments calls WordPress's wp_parse_args(), so it runs inside WordPress.
Quick start
use Pollora\WordPressArgs\ArgumentHelper;
class BookPostType
{
use ArgumentHelper;
private ?bool $public = null;
private ?bool $showInRest = null;
private ?string $menuIcon = null;
public function public(): self
{
$this->public = true;
return $this;
}
public function showInRest(): self
{
$this->showInRest = true;
return $this;
}
public function menuIcon(string $icon): self
{
$this->menuIcon = $icon;
return $this;
}
public function getArgs(): array
{
return $this->buildArguments();
}
}
$args = (new BookPostType)
->public()
->showInRest()
->setRawArgs(['has_archive' => 'books'])
->getArgs();
// [
// 'public' => true,
// 'show_in_rest' => true,
// 'has_archive' => 'books',
// ]
register_post_type('book', $args);
How it works
ArgumentHelper reads the properties declared on the class (all visibilities):
- each property name is converted to snake_case with Laravel's
Str::snake():showInRestbecomesshow_in_rest; - properties whose value is
nullare skipped, so WordPress keeps its own defaults for them; - raw arguments set with
setRawArgs()are merged last, withwp_parse_args(), and win over the properties of the same name.
Methods
| Method | Visibility | Returns |
|---|---|---|
extractArgumentFromProperties() |
public | The arguments built from the properties only. |
setRawArgs(array $rawArgs) |
public | $this. Stores arguments to merge on top. |
getRawArgs() |
public | The raw arguments, or null. |
buildArguments() |
protected | The properties' arguments merged with the raw ones. Expose it from your class (for example as getArgs()). |
rawArgs is the trait's own property and is never turned into an argument.
In the wild
- pollora/entity: its
Entitybase class uses the trait, andgetArgs()builds the arguments passed to post type and taxonomy registration. - pollora/query:
PostQueryuses it to build theWP_Queryarguments returned bygetArguments().
Testing
vendor/bin/pest
Contributing
Contributions are welcome: see the contributing guide. Report security issues privately, as described in the security policy.
License
WordPress Arguments is open-source software licensed under the MIT license. © RuBee group
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 1.1.1 | ^8.2 | ^12.0 | | MIT | |
| 1.1.0 | ^8.2 | ^12.0 | | GPL-2.0-or-later | |
| 1.0 | ^11.0 | GPL-2.0-or-later |