Skip to content

WPfaker Addon Development

Addon Plugin Structure

Minimal directory layout for a WPfaker addon WordPress plugin:

my-wpfaker-addon/
├── my-wpfaker-addon.php          # Plugin entry point
├── src/
│   ├── Adapters/
│   │   └── MyAdapter.php         # FieldPluginAdapterInterface implementation
│   └── AiProviders/
│       └── MyAiProvider.php      # AiProviderInterface implementation (optional)
├── tests/
│   └── Unit/
│       └── MyAdapterTest.php
├── composer.json
└── readme.txt

Entry Point Pattern

The addon MUST wait for the wpfaker_loaded action before registering anything. This ensures WPfaker classes and contracts are available.

php
<?php
/**
 * Plugin Name: My WPfaker Addon
 */

add_action('wpfaker_loaded', function (string $version) {
    // Register a custom field adapter
    add_action('wpfaker_register_adapters', function () {
        \WPFaker\Services\AdapterFactory::register('my_plugin', MyAdapter::class);
    });

    // Register a custom AI provider (optional)
    add_action('wpfaker_register_ai_providers', function () {
        \WPFaker\Services\AiProviderFactory::register('my_ai', MyAiProvider::class, 'My AI Service');
    });
});

Registering a Custom Field Adapter

Your adapter must implement \WPFaker\Contracts\FieldPluginAdapterInterface. The adapter constructor receives a FakerService instance.

Required Methods

MethodReturnPurpose
isActive()boolReturn true if your field plugin is active
getName()stringHuman-readable plugin name
getVersion()stringPlugin version string
getCustomPostTypes()arrayCPTs registered by the plugin
getCustomTaxonomies()arrayTaxonomies registered by the plugin
getFieldGroups()arrayAll field group configs
getFieldGroupsForPostType(string $postType)arrayField groups for a specific CPT
getFields($fieldGroupId)arrayFields within a group
getFieldsForPostType(string $postType)arrayAll fields for a CPT
saveFieldValue(int $postId, string $fieldName, $value)boolSave a field value
getFieldConfig(string $fieldNameOrKey)?arrayGet field config by name
mapFieldTypeToFaker(array $field)stringMap field type to Faker method
getSupportedFieldTypes()arrayList of supported field type slugs
setCptContext(string $slug, ?string $label)selfSet CPT context for detection

Registration

php
\WPFaker\Services\AdapterFactory::register('my_plugin', MyAdapter::class);

Registering a Custom AI Provider

Your provider must implement \WPFaker\Contracts\AiProviderInterface. Custom providers auto-appear in the Settings dropdown once registered.

Required Methods

MethodReturnPurpose
getProviderKey()stringUnique key
getProviderName()stringDisplay name for Settings UI
isAvailable()boolHas API key + is enabled
isEnabled()boolIs enabled regardless of key
getMaskedApiKey()stringMasked API key for display
detectFieldType(...)?arraySingle field detection
detectFieldsBatch(...)?arrayBatch field detection
detectFieldsBatchWithConfig(...)?arrayBatch with faker configs
generateTitleTemplates(...)?arrayGenerate title templates
generateCptContext(...)?arrayGenerate CPT context
testConnection()arrayTest API connection

Registration

php
\WPFaker\Services\AiProviderFactory::register('my_ai', MyAiProvider::class, 'My AI Service');

Available Hooks

Actions

HookParametersPurpose
wpfaker_loadedstring $versionWPfaker fully loaded, addon entry point
wpfaker_register_adaptersnoneTime to register custom adapters
wpfaker_register_ai_providersnoneTime to register custom AI providers
wpfaker_activatenonePlugin activated
wpfaker_deactivatebool $deleteDataPlugin deactivated
wpfaker_after_generate_itemstring $type, int $id, int $index, int $total, array $optionsAfter each item generated
wpfaker_after_generatestring $type, array $ids, array $optionsAfter all items in batch

Filters

HookParametersPurpose
wpfaker_post_dataarray $postData, string $postType, int $index, int $total, array $optionsModify post data before wp_insert_post
wpfaker_field_valuemixed $value, array $config, string $postType, int $postIdModify field value before save
wpfaker_adapter_priorityarray $priorityChange adapter resolution order
wpfaker_field_name_detection_enabledbool $enabledEnable/disable field name detection

Testing Pattern

Use Pest PHP with source-check patterns for adapter tests. Use AdapterFactory::resetRegistry() between tests for isolation:

php
beforeEach(function () {
    \WPFaker\Services\AdapterFactory::resetRegistry();
});

it('registers the adapter', function () {
    \WPFaker\Services\AdapterFactory::register('my_plugin', MyAdapter::class);

    expect(\WPFaker\Services\AdapterFactory::has('my_plugin'))->toBeTrue();
});

Key Classes

  • \WPFaker\Services\AdapterFactory — Registry for field plugin adapters
  • \WPFaker\Services\AiProviderFactory — Registry for AI providers
  • \WPFaker\Contracts\FieldPluginAdapterInterface — Contract for field adapters
  • \WPFaker\Contracts\AiProviderInterface — Contract for AI providers
  • \WPFaker\Services\FakerService — Core faker data generation service (injected into adapters)

Released under the GPL2 License. wpfaker.com