boxybird / morph
A plugin for building WordPress components with Alpine.js.
Requires
- illuminate/encryption: ^8.0
- symfony/http-foundation: ^5.4
- dev-master
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.21
- v0.2.20
- v0.2.19
- v0.2.18
- v0.2.17
- v0.2.16
- v0.2.15
- v0.2.14
- v0.2.13
- v0.2.12
- v0.2.11
- v0.2.10
- v0.2.9
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
This package is auto-updated.
Last update: 2025-01-10 17:37:00 UTC
README
A WordPress theme package for building Laravel Livewire inspired WordPress components with Alpine.js.
Demos
https://wp-morph.andrewrhyand.com/
Demos Theme
https://github.com/boxybird/morph-example-theme
Installation
Install the package via composer.
cd wp-content/themes/your-theme
composer require boxybird/morph
Location: /wp-config.php
define('BB_MORPH_HASH_KEY', 'SOME_RANDOM_16_CHARACTER_STRING');
Location: /your-theme/functions.php
require_once __DIR__ . '/vendor/autoload.php';
Location: /wp-admin/options-permalink.php
Visit and refresh permalinks by clicking "Save Changes" button
Important
By default, this package pre-bundles Alpine. If you site already has Alpine installed, you should dequeue this packages version to avoid conflicts.
Note: A minimum version of Alpine 3.11.0 is required.
Location: /your-theme/functions.php
add_action('wp_enqueue_scripts', function () { wp_dequeue_script('bb-alpine'); });
Hooks
$wpMorph({ ... }, { onStart: () => {}, onResponse: res => {}, onSuccess: data => {}, onError: error => {}, onFinish: () => {}, })
By default, the morph_component('example');
function will look for a matching file of name you passed in. E.g. /your-theme/morph/components/example.php
.
You can override this root folder path by using the morph/component/path
filter.
add_filter('morph/component/path', function ($path) { $path = get_template_directory() . '/custom-folder/'; return $path; });
Example Counters
Location: /your-theme/index.php
<?php get_header(); ?> <?php morph_component('counter.class'); ?> <?php morph_component('counter.procedural'); ?> <?php get_footer(); ?>
Location: /your-theme/morph/components/counter/class.php
<?php // Class [$count] = morph_render(new class { public $count; public function __construct() { $this->count = (int) get_option('count', 0); } public function increment() { update_option('count', ++$this->count); } }); ?> <div x-data> <span><?= $count; ?></span> <button x-on:click="$wpMorph('increment')">Increment</button> </div>
Location: /your-theme/morph/components/counter/procedural.php
<?php // Procedural $count = (int) get_option('count', 0); if ($_POST['increment'] ?? false) { update_option('count', ++$count); } ?> <div x-data> <span><?= $count; ?></span> <button x-on:click="$wpMorph('increment')">Increment</button> </div>
Example Event/Listener
Location: /your-theme/index.php
<?php get_header(); ?> <?php morph_component('event'); ?> <?php morph_component('listener'); ?> <?php get_footer(); ?>
Location: /your-theme/morph/components/event.php
<div x-data> <button x-on:click="$dispatch('notify', 'A notification from the event.php component.')" >Dispatch Event</button> </div>
Location: /your-theme/morph/components/listener.php
<?php $notification = $_POST['notification'] ?? null; ?> <div x-data x-on:notify.window="$wpMorph({ notification: $event.detail })"> <p><?= $notification; ?></p> </div>
Example Global Listener
Location: /your-theme/some-js-file.js
document.addEventListener('notify', function ({ detail }) { console.log(detail) // A notification from the event.php component. });
More Examples
https://github.com/boxybird/morph-example-theme/tree/master/resources/components