Help writing custom function with rewrite endpoint that generates a page from dynamic content

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.

Read More

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
                    remove_all_actions('the_content');
                    // 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!

Related posts