WordPress development made easy: AJAX requests, Model-View-Controller, Restrict User Access, Performance Tuning, Manage Roles, Custom Post Types, etc.
Are you a WordPress developer? Then you are probably struggling with the same stuff that I use too truggle every day.
$ composer require alesanchezr/wpas-wordpress-dash:dev-master
$ php vendor/alesanchezr/wpas-wordpress-dash/run.php <your_theme_directory_name>
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.
Alejandro Sanchez
Repository website: https://github.com/alesanchezr/wpas-wordpress-dash
About me: alesanchezr.com