I created a plugin that will render a shortcode that is entered to a page.
This Shortcode shall override the theme’s page template and use the one that I included on the plugin
Here’s my code:
api.php
class Api{
public static $logger = null;
function Api() {
add_shortcode('my_shortcode', array(&$this, 'my_shortcode_function'));
}
function my_shortcode_function($atts,$content = null)
{
add_filter( 'template_include', 'custom_view');
}
function custom_view()
{
$template = plugin_dir_path( __FILE__ ) . 'custom-page.php';
return $template;
}
}
add_action('init', 'apiInit', 10);
function apiInit() {
global $api;
if (class_exists('Api')){
$api = new Api();
}
}
and here’s my custom-page.php (which I am 100% sure that is in the correct path / directory that I am pointing in the code)
<?php
/**
* Response View Template
* File: custom-page.php
*
*/
echo 'I am here!';
<?
Tried to debug each function it goes through my_shortcode_function() but does not go through custom view function.
Cheers!
The
template_include
filter runs much earlier and is used to load the main requests template.Your content will be called from within
the_content()
,$GLOBALS['post']->post_content
or whatever you use to display it. Point is, that you can’t use the filter as it won’t trigger. Try to simply use something likerequire/include
.Since template_include fires very early even before add_shorcode. This is my solution to the problem.