Skip to content

PHP examples

Runnable PHP samples for the CoverKit plugin. They are meant for development: load them from Media → CoverKit Examples in wp-admin, or require the helpers from tests.

  • Class: CoverKit\Examplesincludes/class-coverkit-examples.php
  • Menu: Media → CoverKit Examples (manage_options).
  • Behavior: Lists every *.php file in examples/ except constants.php and example-bootstrap.php. Selecting a file includes it and shows its output (or invokes a matching coverkit_*, example_*, or test_* function if the file defines one but prints nothing). Errors are caught and shown as admin notices.
  • Define COVERKIT_LOAD_EXAMPLES as false (e.g. in wp-config.php) to disable the admin page.
  • Release builds may omit the entire examples/ directory; the rest of the plugin must not require those files (only the gated Examples class references that folder).
FileRole
examples/example-bootstrap.phpcoverkit_examples_resolve_ids() returns the first published coverkit template ID and a published post ID; coverkit_examples_resolve_ids_clear_cache() clears the cache (tests).
examples/constants.phpDefines COVERKIT_EXAMPLE_TEMPLATE_ID, COVERKIT_EXAMPLE_POST_ID, and alternate IDs when not already defined (uses the bootstrap resolver).

Example scripts should start with:

defined( 'ABSPATH' ) || exit;
require_once __DIR__ . '/constants.php';

Use fully qualified classes (e.g. \CoverKit\Renderer::generate()) unless the file lives in the CoverKit namespace (prefer global functions + \CoverKit\ for consistency with the admin loader).

Renderer entry points (as used in examples)

Section titled “Renderer entry points (as used in examples)”
  • CoverKit\Renderer::generate( $use_case_id, $template_id, $post_id, $options ) — primary generation keyed by use case id (opengraph, custom ids).
  • CoverKit\Renderer::generate_from_content( $content, $post_id, $meta, $options ) — template post ID, block content, or block object; preferred for previews and direct template runs.
FilePurpose
simple-demo.phpRegistry smoke test, template apply_data, and Renderer::generate.
use-case-usage.phpRunnable OpenGraph sample; custom Use_Case registration shown as a copy-paste init snippet (not executed here).
renderer-usage.phpWalkthrough: Renderer::generate vs generate_from_content, canvas string; coverkit_after_generate as copy-paste snippet only.
field-mapping-demo.php_coverkit_use_cases meta + generation (writes template meta — dev only).
example-usage.phpUsage “cookbook”: use case meta, hooks, batch/cache/error patterns as escaped snippets; one live Renderer::generate demo (no update_option or hook registration on load).
example-template.phpReference layout for new example files (docblock, constants, one coverkit_* entry).
apply-raw-data-test.phpShort demo replacing removed apply_raw_data() with generate_from_content().
rest-api-image-test.phpREST / content-string style generation.
canvas-srcset-test.phpCanvas / responsive image experiments (explicit bottom invoke).
test-implementation.phpImplementation checks (typed helpers, \CoverKit\ class references).
feature-image-refactor-test.phpFeature image on-demand collection; creates/deletes temporary posts (explicit bottom invoke).

PHPUnit reuses example-bootstrap.php (see tests/php/ExampleBootstrapTest.php) and asserts Renderer::generate exists (RendererGenerateApiTest.php). Full image generation against a real DB is optional and not part of the default stub bootstrap.


← Codebase index · PHP backend