How to define a callback for an URI?

I have a function in functions.php like so:

function myplugin_mycallback($id) {
  // do something
}

I want this function called whenever a URL like myplugin/mycallback/xxx is accessed.

Read More

How/where do I define this path so it doesn’t generate a 404?

Related posts

Leave a Reply

1 comment

  1. You can use add_feed( $url, $callback ). Despite its name it sends a text/html Content-Type.

    Basic example:

    add_action( 'init', 'wpse_50841_register_extra_page' );
    
    function wpse_50841_register_extra_page()
    {
        add_feed( 'wpse50841', 'wpse_50841_callback' );
    }
    
    function wpse_50841_callback()
    {
        print '<p>It works!</p>';
    }
    

    Visit the permalink settings page once to refresh the rewrite cache, and go to example.com/wpse50841/ or example.com/?feed=wpse50841 to see your code in action.