Some stuff have been changed, and I got no time to update docs. Updates coming soon.
The framework for WP plugin creators.
Features:
- PSR-4 - No more annoying
require
frenzy. - Use
composer
to install and manage PHP dependencies. - Use TypeScript or JavaScript to create front end scripts, and
npm
to install packages and extend functionalities. - Enjoy
webpack
optimized build for production.
- A Wordpress installation
- Composer installed
- Node.JS and npm installed (LTS recommended)
- Project startup documentation
- Developing guidelines documentation
- JavaScript/TypeScript development documentation
- Custom post types implementation + Documentation
-
wp-json
API implementation + Documentation - React usage documentation
- Sass (.scss) support + documentation
-
wp-config.php
setup - Production deploy documentation
- Composer usage documentation
- i18n support
- Hooks documentation
- Clone this repository into
plugins
folder of a development machine, andcd
intolib
folder.
# @wp-content/plugins
git clone https://github.com/obrunopolo/wp-zeus-framework.git ./zeus-framework
cd zeus-framework/lib
- Install dependencies and run first build:
npm run update-dev
- Start creating
After first project build, the main plugin folder has 4 subfolders:
includes
: contains standalone .php files, such as theFunctions
andConstants
files. Also holds the JavaScript files that are actually loaded into the view.lib
: contains development files. This folder could be omitted in production environments. Holds the JavaScript/TypeScript source files and is the place for running terminal commands.src
: the PHP source code, and the root ofZeus
namespace. Holds the PSR-4 compliant code, and should be responsible for most of the plugin functionality.vendor
: contains the autoload file and the installed PHP dependencies, generated bycomposer
.
The project is intended to be flexible to the developer, but also suggests usage of MVC architecture and reinforces OOP.
To encapsulate functionalities, we must have a Controller
class that has a defined scope. For this example, let's say we need to add a greeting to the current user, when logged in, in the single post page, before the post content.
This could be achieved if we had a User
controller, using the filter the_content
, like this:
src/Controllers/User.php
<?php
namespace Zeus\Controllers;
use Zeus\Framework\Contracts\Controller;
use Zeus\Framework\Contracts\Singleton;
class User extends Singleton implements Controller
{
function filterPostContent($content) {
$greeting = "";
if (is_user_logged_in()) {
$user = wp_get_current_user();
$greeting = "<p>Hello, {$user->first_name}</p>";
}
return $greeting . $content;
}
public function run()
{
add_filter("the_content", [$this, "filterPostContent"]);
}
}
Then, add the new controller to the main App
class:
src/App.php
<?php
namespace Zeus;
use Zeus\Controllers\User;
class App
{
// {{...}}
/** @var User */
public $user;
// {{...}}
public function run()
{
// Instantiate controllers here
// {{...}}
$this->user = User::getInstance();
// {{...}}
}
}
Now you should see the greeting, if logged in.