Are you a WordPress developer? Then you are probably struggling with the same stuff that I use too truggle every day.
- MVC Pattern implementation (Model-View-Controller) in WordPress.
- Create API's with WordPress very fast.
- Require the library with composer (NOTE: You must be in your Wordpress directory before running this command. The installer will attempt to create your theme in ./wp-content/<your_theme_directory_name> )
$ composer require alesanchezr/wpas-wordpress-dash:dev-master
- Create a new theme using the installation script. Or select an already created theme (it will try to create the folder structure automatically)
$ php vendor/alesanchezr/wpas-wordpress-dash/run.php <your_theme_directory_name>
- Update the WPASController according to your needs in functions.php
use \WPAS\Controller\WPASController;
$controller = new WPASController([
//Here you specify the path to your consollers folder
'namespace' => 'php\\Controllers\\'
]);
Note: This library expects your theme to load the vendor/autoload.php file in your functions.php. A good way of doing that is:
/**
* Autoload for PHP Composer and definition of the ABSPATH
*/
//defining the absolute path for the wordpress instalation.
if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/');
//including composer autoload
require ABSPATH."vendor/autoload.php";
If you are working with Flywheel hosting (and/or Flywheel Local), you will need to require the path in the following way:
if(!strpos($_SERVER['SERVER_NAME'], '.local')){
require 'vendor/autoload.php';
}else{
require ABSPATH . 'vendor/autoload.php';
}
This is due to the fact that their folder structure separates your content and plugins from the root Wordpress install after you push your site live.
Instanciate the PostTypeManager:
$typeManager = new \WPAS\Types\PostTypesManager([
'namespace' => '\php\Types\\' \\this will be the path to your models folder
]);
Define your type in functions.php
//You can react a new custom post type and specify his class
$typeManager->newType(['type' => 'your_type_slug', 'class' => 'AnyPostTypeModelClass'])->register();
Define your type class in the types folder:
namespace php\Types;
class AnyPostTypeModelClass extends \WPAS\Types\BasePostType{
//any method here
}
Note: you HAVE to extend from the BasePostType class, that is not optional.
Continue reading about the models
Create your Controller classes and bind them to your views, pages, categories, posts, etc.
//Here we are saying that we have a class Course.php with a function getCourseInfo that fetches the data needed to render any custom post tipe course
$controller->route([ 'slug' => 'Single:course', 'controller' => 'Course' ]);
Our Course.php controller class will look like this:
namespace php\Controllers;
class Course{
public function renderCourse(){
$args = [];
$args['course'] = WP_Query(['post_type' => 'course', 'param2' => 'value2', ...);
return $args; //Always return an Array type
}
}
Continue reading about implementing MVC on your wordpress
On you wo-config.php file add the following constant:
define('WP_DEBUG_CONTEXT', true);
It will add a top bar with the current template being used.
- Add WordPress roles programatically.
- Restrict Role Access to particular pages, posts, categories, etc.
- Create and manage all your custom post types in just a few lines of code.
- Hit 100% on the Google Page Speed test.
- Messaging notification system for the WordPress admin user, using the WordPress standards.
- Create new Visual Composer components for your theme in just 5 lines fo code.
- Extend Gravity Forms functionality.
Alejandro Sanchez
Repository website: https://github.com/alesanchezr/wpas-wordpress-dash
About me: alesanchezr.com