I'm creating a WordPress plugin that will pull content from an API and display it dynamically on a WordPress blog like it's native content. An example of this content would be company locations. I already have a short code that pulls the list of locations into the preferred page, but we also want to dynamically generate location detail pages with data from the API.

So... /locations/ is a WordPress page with a [locations] short code that pulls the list of locations from the API. DONE & WORKING.

We want /locations/location-detail/123-my-location-name/ (or something really similar) to build a dynamic page using the DEFAULT page template.

From the research I've done (and I'm very new to WP plugin development), we could possibly accomplish this by creating a custom endpoint that will send the endpoint "location-detail" with the value "123-my-location-name" to my plugin.

I want the plugin to then generate a "fake" page with content from my API and display it to the visitor. I found this code from a blog post, but I can't seem to get it in my plugin which is class-based (OOP).

// added for rewrite URL for front end
            $endpoint = 'wizard';
            add_action('init', 'endpoint');       
            add_action( 'template_redirect', 'template_redirect' );
            add_filter( 'the_posts', 'the_posts' );
// added for rewrite URL for front end

 function endpoint(){
                $endpoint = 'wizard';
                add_rewrite_endpoint('wizard', EP_ROOT);  // it will create endpoint to access it from frontend

function the_posts( $posts ){
                $endpoint = 'wizard';           
                $value = get_query_var($endpoint);

                // Check if our endpoint is being used
                if( $value ){
                    // Perform the necessary actions
                    // Create a fake page
                    $post = array(
                        'ID' => 1,
                        'post_author' => 1,
                        'post_date' => current_time('mysql'),
                        'post_date_gmt' => current_time('mysql', 1),
                        'post_content' => $html,
                        'post_title' => 'Post Title / Plugin Title',
                        'post_status' => 'static',
                        'comment_status' => 'closed',
                        'ping_status' => 'closed',
                        'post_name' => $endpoint . '/' . $value,
                        'post_parent' => 0,
                        'post_type' => 'page'
                    $posts = array( (object) $post );

function template_redirect(){

I'm hitting a brick wall here, I'm not at all familiar with WordPress plugin development and can't seem to get a proof of concept working. If anyone could get me a simple snippet of code that creates a dynamic page with content from the plugin (even dummy text) I could take it from there and get the rest of the plugin written.

Any help, advice, or suggestions would be greatly appreciated!

  • 1
    if your desired path is location-detail appended to a page, then your endpoint should be named location-detail and the endpoint mask should be EP_PAGES – Milo Nov 15 '13 at 22:47
  • Thanks, I changed that but the above code doesn't work either way. I just get a page not found error when the add_filter('the_posts'...) is ran. I'm not sure how to successfully overwrite the page content. – Brandon Eley Nov 18 '13 at 15:13
  • trying to overwrite a page's content is a strange (and very error-prone) way to try to do this, why not just use your shortcode for both cases? detect whether the request is for the base page or a detail page in your shortcode handler and output either the list or detail based on that. also remember to flush rewrite rules after making any changes to rewrites. – Milo Nov 18 '13 at 16:57
  • We couldn't use short codes because they also pull in the page content above the short code. We ended up using a combination of rewrite rules in the plugin and a custom page template in the theme to get what we wanted on the location detail pages. It was the only way we could set the page header, content, etc without overwriting the page's content. – Brandon Eley Jul 22 '14 at 14:38

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.