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.
composer require pollora/wordpress-argsRequires PHP 8.2+ and illuminate/support 12 or 13. Building the arguments calls WordPress's wp_parse_args(), so it runs inside WordPress.
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);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.
| 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.
- 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().
vendor/bin/pestContributions are welcome: see the contributing guide. Report security issues privately, as described in the security policy.
WordPress Arguments is open-source software licensed under the MIT license. © RuBee group