Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
1  
I hope Api is not your actual class name. –  kaiser Sep 2 '13 at 1:05
add comment

1 Answer

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 like require/include.

share|improve this answer
    
For me, get_template_part is the right choice in this case. –  G. M. Sep 2 '13 at 2:00
    
@G.M. Mind explaining why? –  kaiser Sep 2 '13 at 6:03
    
Search for child theme and if not find search in parent theme. Also and provide filters. –  G. M. Sep 2 '13 at 12:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.