PHP code example of qtgye / wp-template-wrapper

1. Go to this page and download the library: Download qtgye/wp-template-wrapper 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/ */

    

qtgye / wp-template-wrapper example snippets


// src/routes.php

add_filter( 'template_routes', function () {

	return array(
		$template_slug => $callable,
	);

});

// single.php

use QtGye\TemplateWrapper\Wrapper;

<h1>This is an awesome post!</h1>


	Wrapper::

// functions.php

function pper::

 class MyControllerClass {
	
	// Using the $post global
	static function about_page ( $post ) {

		$data = [];

		$data['id'] = $post->ID;
		$data['slug'] = $post->name;

		// The variables $id and $slug are now available in the template
		return $data;
 
	}
 }
 

 // src/global.php

 add_filter('template_global_data', function ( $predefined_globals )
 {
	// For some reason, use $wp_roles global
	global $wp_roles;
	
	// $wp_roles variable is now ready to be used in the templates
	return compact('wp_roles');
});
 

	// src/routes.php

	add_action( 'ajax_routes', function () {

		return array(
			// Matches ?action=items
			'items' => [ "MyController", "ajax" ],
		);

	});
 

 class MyController {
	
	// Handling ?action=items&filter=all&page=3
	// The argument names should match the GET parameter names. Hence, order is irrelevant.
	static function ajax ( $page, $filter, $post ) {
		
		// $filter === 'all'
		// $page === '3'
		$response = [
			'items' => get_filtered_posts( $filter, $page),
			'current_id' => $post->ID,
		];
		
		// Returned data is sent as json-encoded response.
		return $response;
 
	}
 }