2017 © Pedro Peláez
 

library wpc

image

thezombieguy/wpc

  • Tuesday, March 6, 2018
  • by thezombieguy
  • Repository
  • 1 Watchers
  • 1 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Wordpress Custom Theme library

This library is intended to give WordPress developers a little extra functionality when building custom themes., (*1)

This library includes advanced theme templates with variable injection, API wrapper for easier endpoint development, and a simple caching system that stores cached objects to the file system., (*2)

Install

You can install with composer, (*3)

composer require thezombieguy/wpc 2.0

Usage

More information is available in the docs folder., (*4)

Theme

First create a template in your theme folder. For this example, we will create templates/test.php, (*5)

<?php print $test; ?>

And now, in one of your page templates, add the following code., (*6)

<?php print wpc_theme('templates/test', array('test' => 'hello world')); ?>

The wpc_theme function takes 2 parameters. The location of the template relative to your wordpress theme directory (not that you do not need to provide the php extension, just the name), and a set of variables that you want to pass to your template., (*7)

Each array variable will be passed to the template and extracted as its own variable. You may also access the Theme class directly by invoking the \WPC\Theme class., (*8)

$template = 'templates\test'
$theme = new \WPC\Theme($template);
$theme->set('test', 'hello world');
$content = $theme->fetch();
print $content;

Cache

Create a new cache object. This will create a wpc_cache folder in your uploads folder if it doesn't already exist. Make sure you have the correct permissions., (*9)

$cache = new Cache(); 

Now cache some data to the filesystem., (*10)

$cache->set('myCacheData', array('fruit' => 'apple')); 

Once it is cached, you can retrieve it later., (*11)

$myCacheData = $cache->get('myCacheData');

Cache returns an object when calling cached data. $myCacheData->time represents when this ws cached. $myCacheData->data is the data you put into the cache., (*12)

stdClass Object
(
    [data] => Array
        (
            [fruit] => apple
        )

    [time] => 1489257366
)

You can check the $myCacheData->time and after a certain amount of time, you can delete and recache trhe object again with updated information., (*13)

You can also wipe the cache folder with, (*14)

$cache->clear();

This will destory all cached objects., (*15)

API

Create endpoints that will call back to a custom PHP class., (*16)

First create a callback class that will handle your endpoint., (*17)

class MyClass
{ 
    public function myMethod($args)
    {
        wp_json_send($args);
    }
}

Now create an array of endpoints to wish to register. Note the handler/callback parameters in the url string must match you class/method created above., (*18)

$endpoint = array(
    'regex' => '^api/numbers/([0-9]+)/([0-9]+)',
    'redirect' => 'index.php?__api=1&handler=MyClass&callback=myMethod&uid=$matches[1]&prize_id=$matches[2]',
    'after' => 'top',
)

$endpoints[] = $endpoint;

new WPC\Api($endpoints);

Now go and update your permalinks in WordPress or you will not see this in action. Go to Settings->Permalinks and click Save changes., (*19)

Now when you go to your endpoint url http://example.com/api/numbers/1/2 you will see a json string output with the values you specified., (*20)

The redirect string MUST contain the __api, handler, and callback variables or your endpoint will not execute., (*21)

The Versions

06/03 2018

v1.0

1.0.0.0

  Sources   Download

19/03 2017

dev-master

9999999-dev

Custom WordPress features to allow for caching, theming and simple API endpoint creation.

  Sources   Download

MIT

by Bryan Trudel

11/03 2017

v2.0

2.0.0.0

Custom WordPress features to allow for caching, theming and simple API endpoint creation.

  Sources   Download

MIT

by Bryan Trudel

11/03 2017

dev-development

dev-development

Custom WordPress features to allow for caching, theming and simple API endpoint creation.

  Sources   Download

MIT

by Bryan Trudel