PHP code example of nghh / lib-wordpress

1. Go to this page and download the library: Download nghh/lib-wordpress 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/ */

    

nghh / lib-wordpress example snippets


// in your theme e.g. functions.php
use Nghh\Lib\Wordpress\Utils\WP_Router;

// Optional args. If you use namespace, pass it like this
$args = [
    'namespace' => __NAMESPACE__ . '\Controllers\\',
    'env'       => 'local'
];

(new WP_Router($args))->registerHooks();

// A controller can look like this e.g. Controllers/SingularController.php
namespace Nghh\Theme\Controllers;

use Nghh\Theme\Models\Post;
use Nghh\Theme\Models\Page;
use Nghh\Theme\Models\Attachment;

class SingularController extends BaseController {

    public function page()
    {
        echo $this->view('pages.singular.page', ['Page' => new Page()]);
    }

    public function post()
    {
        echo $this->view('pages.singular.post', ['Post' => new Post()]);
    }

    public function attachment()
    {
        echo $this->view('pages.singular.attachment', ['Attachment' => new Attachment()]);
    }
}


add_filter('nghh/lib/router', 'nghh_modify_controller', 10, 2);

function nghh_modify_controller($controller, $wp_query)
{
    /**
     * $controller['name] = 'singular'
     * $controller['action] = 'post'
     * 
     * => calls SingularController::post()
    */

    return $controller;
}

// functions.php
use Nghh\Lib\Wordpress\Utils\Config;

/**
 * Init config class in your functions.php
 * and pass full path to config dir and environemt
 */
Config::instance()->init('/path/to/config/dir', 'local');

// config/app.php
return [
    'version' => '0.0.1'
];

// You can then get config data with helper class function like this:
\Nghh\Lib\Wordpress\func\config('app.version');

// => 0.0.1