How can I output completely custom content with plugin while still having header, footer, sidebar etc [closed]

What I want to do is make some url rewrites that will be handled by classes in my plugin. When one of these urls is matched I want to hit an external api and then put out custom html with the content of api response while still having footer/header/sidebar etc from the current active theme.

I’ve been testing different methods for a couple of days and seems like outputing my code and the exiting before wordpress runs the default queries could be a good way to do this, but I’m not sure how to use the header/footer from the theme.

Answer

Buddypress use custom post type to achieve that. You may not need a custom post type to do that. You can use a page as handler for your plugin and rewrite URLs so that your requested URL is redirected to that page.

Then from your plugin, you can use action hook template_redirect. As you are using plugin, you do it like this

add_action('template_redirect', array(&$this,'my_custom_template'));

Now from my_custom_template you can check the page id and serve a different template from your plugin or add filter the_content if you only want to change the content of the page.

function my_custom_template(){
    if( is_page($my_handler_page_id) ){
        add_filter('the_content', array(&$this, 'my_custom_content'));
    }
}

Now serve your desired content from my_custom_content function

function my_custom_content($content){
    $content = "<p>This is custom content</p>";
    .....
    return $content;
}

You can also set query_var when rewriting URL to take different argument and sent different content checking that query_var values.

Attribution
Source : Link , Question Author : Leylu , Answer Author : sakibmoon

Leave a Comment