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' 
        }
}

Create a plugin with this exact code. Activate the plugin and test typing in the browser http://your_wordpress_site.com/customop_uri_path.

The registers functions deals with permalink structure, and are executed just in the plugin activation or plugin deactivation (to clean the permalinks entries, in case of not using anymore the plugin).

The real magic is done by the following code. There are two functions: one used in the plugin activation to create a new rewrite rule and add a permalink entry, and the other constrols the output display. The get_query_var function scans for uri arguments if there are your custom uri path created previously. If so, then, it executes the code to write your custom output. DON’T FORGET THE FINAL EXIT, or it will fall in the not found content page.

Scans for any ocurrence of ‘customop_uri_path’ in the code to customize your own uri address of your custom output. Honestly talking, I still did not understood whats going on in the add_rewrite_tag and add_permastruct arguments. So, just replace with your own uri path.

Also, assure that your apache rewrite module are working properly and your WordPress installation are setted to use the permalinks, or you will not see the module works.

Leave a Comment