Search by

pollora / wordpress-args

pollora

Manipulate WordPress arguments as an object.

Package info

github.com/Pollora/WordPressArguments

pkg:composer/pollora/wordpress-args

Statistics

Installs: 9 726

Dependents: 4

Suggesters: 0

Stars: 2

Open Issues: 1

1.1.1 2026-09-17 12:51 UTC

This package is auto-updated.

Last update: 2026-10-07 08:38:40 UTC


README

WordPress Arguments: WordPress argument arrays built from objects

Latest version Total downloads Tests License

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(): showInRest becomes show_in_rest;
  • properties whose value is null are skipped, so WordPress keeps its own defaults for them;
  • raw arguments set with setRawArgs() are merged last, with wp_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 Entity base class uses the trait, and getArgs() builds the arguments passed to post type and taxonomy registration.
  • pollora/query: PostQuery uses it to build the WP_Query arguments returned by getArguments().

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