How to assign a Permalink to a Function

In Drupal, you can assign a URI to a function that gets called, like this:

function mymodule_menu() {
    return array(
        'custom_uri' => array(
            'page callback' => 'my_custom_function',
            [...]
        );
    );
}

This then tells Drupal to call the function my_custom_function whenever the url is http://www.mysite.com/custom_uri.

In WordPress I’m trying to accomplish the same idea. I want to have a custom URI that somehow calls a function to render output.

From what I’ve found I can use WP_Rewrite to turn a permalink into a query string and then check for the query string in my plugin. Going that route, how would I display my own content in the site’s theme?

The other option I’ve seen is to do it via a shortcode, but then that means either the user generates a page or I do it programmatically (and then probably use WP_Rewrite to set up a custom permalink). This seems messy and I’d rather the user not have to worry about doing any work.

Are there any more options?

1 Answer
1

Create an endpoint. That is a custom URL that will be handled by a callback.

Examples are How can I use WordPress functions in my stylesheet? and Is it possible to request several post types from a feed?. See also add_rewrite_endpoint() and A (Mostly) Complete Guide to the WordPress Rewrite API.

Leave a Comment