0

I want to send traffic of a specific URL pattern to be processed by my plugin. Based on that pattern, I will pull some custom post type information from WP, gather external info via JSON, aggregate it, and spit out the info raw (no theming, etc). I want to do this all within my plugin without having to update core or theme templates. However, I'm having a hard time figuring out how to pass control to my plugin along with the correct URL pattern info. I've looked at add_rewrite_rule, but eventually dismissed that as it seems exclusively for redirecting to index.php. add_rewrite_endpoint seems promising, but I'm having a hard time putting together how that would work. Can anyone point me in the right direction? Or is there a better (or best practice) way to do this?

Update:

I'm working on the code, hampered by my lack of knowledge of the lifecycle of a plugin/page. Below is my code. I thought this was working, but it's now giving me very unreliable results. It's clear to me that my rewrite endpoint should be added on activation so it's not added multiple times. I'm also flushing the rewrite rules only on activation and deactivation. It's much less clear to me the right action to attach the other actions and filters to. I seem to get results where the render function is getting used on every page (regardless of URL) OR the endpoint doesn't seem to work at all. Quite variable results. Also, even when the endpoint seems to be working, I get no vars returned. Can anyone provide some guidance?

class My_Class { 

    public static function mc_init() {
        add_filter( 'request', 'My_Class::mc_set_feed_var');
        add_action( 'template_redirect', 'My_Class::mc_render');
    }

    public static function mc_activate() {
        self::mc_add_rewrite_endpoint();
        flush_rewrite_rules();
    }

    public static function mc_deactivate() {
        flush_rewrite_rules();
    }

    public static function mc_add_rewrite_endpoint() {
        add_rewrite_endpoint('custom-endpoint', EP_ALL);
    }

    public static function mc_render(){
        echo "hello world";
        exit();
    }

    public static function mc_set_feed_var( $vars )
    {
        if( isset( $vars['custom-endpoint'] )) $vars['custom-endpoint'] = true;
        return $vars;
    }
}

register_activation_hook(__FILE__, 'My_Class::mc_activate');
register_deactivation_hook(__FILE__, 'My_Class::mc_deactivate');
add_action('init', 'My_Class::mc_init');
  • 1
    What is the pattern? Is this something that should display on the front end? Is it an AJAX request? Is it a kind of statistics or logging package? What? – s_ha_dum Jun 10 '15 at 22:36
  • @s_ha_dum - The pattern isn't important other than being a signifier to the plugin. So, something like '^/custom/category/'. 'custom' would tell it to go to my plugin, 'category' would be used by my plugin to gather items of a custom post type having that category. The result (as output by the plugin) would be a front-end displaying of information, though the URL would only be used by those who specifically know it. The display would just be information wrapped however I decide to wrap it. – Nicole Jun 10 '15 at 23:12
  • Then you want an endpoint. – s_ha_dum Jun 10 '15 at 23:28
  • For any poor souls who happen upon this later, I ended up going a completely different route (adding a feed instead). More info: wordpress.stackexchange.com/questions/191824/… – Nicole Jun 18 '15 at 22:06

Your Answer

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

Browse other questions tagged or ask your own question.