1. Go to this page and download the library: Download wp-plus/wp-oop-base library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wp-plus / wp-oop-base example snippets
use WpPlus\WpOopBase\Plugin\AbstractPlugin;
class MyPlugin extends AbstractPlugin
{
protected function main(): void
{
// Initialize your plugin hooks and functionality here
add_action('init', [$this, 'initialize']);
}
public function initialize(): void
{
// Plugin initialization logic
}
}
// Initialize the plugin
MyPlugin::runInstance();
// or
// MyPlugin::getInstance()->run();
use WpPlus\WpOopBase\Hook\AbstractHook;
class MyCustomHook extends AbstractHook
{
public function getName(): string
{
return 'wp_head';
}
public function getPriority(): int
{
return 20;
}
protected function callback(...$args): mixed
{
echo '<meta name="custom" content="my-plugin">';
return null;
}
}
// Register the hook
MyCustomHook::registerInstance();
use WpPlus\WpOopBase\Shortcode\AbstractShortcode;
class MyShortcode extends AbstractShortcode
{
public function getTag(): string
{
return 'my_shortcode';
}
protected function getSupportedAttributes(): array
{
return [
'title' => 'Default Title',
'color' => 'blue'
];
}
protected function doShortcode(array $attributes, string|null $content): string
{
return sprintf(
'<div style="color: %s"><h3>%s</h3>%s</div>',
esc_attr($attributes['color']),
esc_html($attributes['title']),
$content ? wpautop($content) : ''
);
}
}
// Register the shortcode
(new MyShortcode())->register();
use WpPlus\WpOopBase\Custom\PostType\AbstractCustomPostType;
class ProductPostType extends AbstractCustomPostType
{
public static function getPostType(): string
{
return 'product';
}
protected function getConfig(): array
{
return [
'labels' => [
'name' => 'Products',
'singular_name' => 'Product'
],
'public' => true,
'has_archive' => true,
'supports' => ['title', 'editor', 'thumbnail']
];
}
}
// Register the post type
ProductPostType::registerInstance();
$plugin = MyPlugin::getInstance();
// Register components directly
MyHook::registerInstance();
// Run plugins directly
MyPlugin::runInstance();
$component->register(); // Register with WordPress
$component->unregister(); // Remove from WordPress
use WpPlus\WpOopBase\Helper\DateTime;
// Format timestamp with WordPress timezone
$formatted = DateTime::format(time(), 'Y-m-d H:i:s');
// MySQL datetime format
$mysql = DateTime::format(time(), 'mysql');
use WpPlus\WpOopBase\Helper\Post;
// Get post by slug
$post = Post::getPostBySlug('my-post-slug', 'product');
// Get post ID by slug
$id = Post::getPostIdBySlug('my-post-slug', 'product');
use WpPlus\WpOopBase\Support\WpDbTrait;
class MyClass
{
use WpDbTrait;
public function getSomething(): array
{
return $this->wpdb()->get_results("SELECT * FROM ...");
}
}
use WpPlus\WpOopBase\Plugin\AbstractExtensiblePlugin;
class MainPlugin extends AbstractExtensiblePlugin
{
protected function main(): void
{
// Add sub-plugins
$this->addPlugin(SubPlugin1::class);
$this->addPlugin(new SubPlugin2());
}
}
MainPlugin::getInstance()->run(); // Runs main plugin and all sub-plugins
use WpPlus\WpOopBase\Hook\AbstractHook;
use WpPlus\WpOopBase\Hook\AbstractMultiHook;
class PostStatusFunctionality extends AbstractMultiHook
{
public function __construct(AbstractHook ...$hooks)
{
// Hook for when post is published
$hooks[] = new class extends AbstractHook {
public function getName(): string
{
return 'publish_post';
}
protected function callback(...$args): mixed
{
// Handle post publication
error_log('Post published: ' . $args[0]->ID);
return null;
}
};
// Hook for when post is trashed
$hooks[] = new class extends AbstractHook {
public function getName(): string
{
return 'wp_trash_post';
}
protected function callback(...$args): mixed
{
// Handle post deletion
error_log('Post trashed: ' . $args[0]);
return null;
}
};
parent::__construct(...$hooks);
}
}
// Register all related hooks at once
(new PostStatusFunctionality())->register();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.