Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active August 9, 2018 12:06
Show Gist options
  • Save tommcfarlin/2fd7f50706c3f1767dc8b5c2b7deefff to your computer and use it in GitHub Desktop.
Save tommcfarlin/2fd7f50706c3f1767dc8b5c2b7deefff to your computer and use it in GitHub Desktop.
[WordPress] A Simple Guide for Organizing WordPress-centric Classes
<?php
namespace Acme\WordPress\Admin\MetaBox;
abstract class AbstractMetaBox
{
protected $postType;
public function __construct()
{
$this->postType = 'acme_post_type';
}
abstract public function render();
abstract public function display();
}
<?php
namespace Acme\WordPress\Admin\MetaBox;
class AcmeMetaBox extends AbstractMetaBox
{
/**
* {@inheritdoc}
*/
public function render()
{
add_meta_box(
'acme-product-image',
'Product Image',
[$this, 'display'],
$this->postType,
'side',
'default'
);
}
/**
* {@inheritdoc}
*/
public function display()
{
include_once plugin_dir_path(__FILE__).'Views/acme-product-image.php';
}
}
<div class="product-image-metabox">
<p>
<img src="<?= esc_html(get_post_meta(get_the_ID(), 'product_image', true)); ?>" alt="<?= esc_attr(get_the_title()); ?>" />
<input type="text" value="<?= esc_html(get_post_meta(get_the_ID(), 'product_image', true)); ?>" />
</p>
</div>
<testsuites>
<testsuite name="Plugin">
<directory>./tests</directory>
<exclude>./tests/phpunit</exclude>
<exclude>./src/WordPress</exclude>
</testsuite>
</testsuites>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment