Wallogit.com
2017 © Pedro Peláez
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)
You can install with composer, (*3)
composer require thezombieguy/wpc 2.0
More information is available in the docs folder., (*4)
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;
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)
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)