0

I've googled myself to death with this problem for hours now, so I'm trying to get some help by creating my own question hoping you can help me :)

I have written a Plugin which needs to output its content on a blank page.

I could use a shortcut, but then template header and footer will be loaded to. I don't want to create a (blank) custom template file. This would need more work for the user then just installing the Plugin.

So I found this code on stackexchange which creates a permalink and outputs the content to it: (direct Link: Generate custom output for page/URL in a plugin)

<?php
/*
Plugin Name: Custom output
Description: A module to test the custom output. To test: http://your_wordpress_site.com/customop_uri_path
Version: 1.0
Author: Danilo Silva
Author URI: http://danilocgsilva.me
*/

// Write a new permalink entry on code activation
register_activation_hook( __FILE__, 'customop_activation' );
function customop_activation() {
        customop_custom_output();
        flush_rewrite_rules(); // Update the permalink entries in the database, so the permalink structure needn't be redone every page load
}

// If the plugin is deactivated, clean the permalink structure
register_deactivation_hook( __FILE__, 'customop_deactivation' );
function customop_deactivation() {
        flush_rewrite_rules();
}


// And now, the code that do the magic!!!
// This code create a new permalink entry
add_action( 'init', 'customop_custom_output' );
function customop_custom_output() {
        add_rewrite_tag( '%customop_uri_path%', '([^/]+)' );
        add_permastruct( 'customop_uri_path', '/%customop_uri_path%' );
}

// The following controls the output content
add_action( 'template_redirect', 'customop_display' );
function customop_display() {
        if ($query_var = get_query_var('customop_uri_path')) {
                header("Content-Type: text/plain");
                echo 'This is my custom content!!!!';
                exit; // Don't forget the exit. If so, WordPress will continue executing the template rendering and will not fing anything, throwing the 'not found page' 
        }
}

This example works very well, except it does not only output the content to "customop_uri_path", it creates the output on ALL pages except the home-page. So the whole website gets screwed up.

I have created a new help-thread in order to get more ideas on how to create such an output for my plugin.

Can I load a specific template file just for the plugin, so the shortcode will also generate a blank page? Or is there a mistake in the code-example?

I'm thankful for all the help you can get me.

  • this sounds like you are more in a need of doing proper debugging regarding what happens in customop_display than have some general issue. At least to me the code looks conceptually right. – Mark Kaplun May 11 '18 at 6:14
-1

You can use the "page_template" filter to load a custom template for a given page. It will replace the template used whenever the "page" template is called.

Remember to add the template file somewhere on your plugin folder.

add_filter( 'page_template', 'custom_template_function' );
function custom_template_function( $page_template )
{
    if ( is_page( 'my-custom-page-slug' ) ) {
        $page_template = dirname( __FILE__ ) . '/custom-template.php';
    }
    return $page_template;
}
  • downvoted as it do not actually answer the question. If you mean to suggest to use pages instead of rewrite rules, it is not very clear from the answer. – Mark Kaplun May 11 '18 at 6:16
  • His last question was "Can I load a specific template file just for the plugin, so the shortcode will also generate a blank page? Or is there a mistake in the code-example?" Isn't what I suggest, loading a specific template file just for the plugin? Your comment doesn't make sense to me @MarkKaplun – Filcp May 11 '18 at 6:44
  • he do not want the user to have to have any interaction in order to create the page. With your solution the user has to create a page and configure a specific slug in it. You can do it automatically on plugin activation, but right now that part is missing from the answer. In any case, regardless of the downvote, this will display the page like any other page in the admin, and the user might remove it by mistake, so this is not an optimal solution. – Mark Kaplun May 11 '18 at 9:06
  • Thanks for all the fast answers guys :) I tried the solution Flilipe posted and it works as a pre-solution. I still have to create a page named after the custom page slug and my client could accidentally delete it, but it will be acceptable for a showcase version. I will look into futher and deeper debugging of the rewrite rules. – Foogle May 11 '18 at 11:55
  • I won't close the thread in case somebody has a better solution. I'll keep you updated on the "rewrite rules" version debugging. – Foogle May 11 '18 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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