0

I am creating a custom plugin that connects to an external API and pulls in data about a company's locations. Currently, the plugin has a shortcode that I put on a "Locations" WordPress page that lists all the locations.

Essentially I'm just hitting the API, pulling back the data, generating some HTML and returning it to the short code. This works fine for the locations page (/locations), as it has the WordPress page's title and copy above the list. But for the locations detail page I need a "blank" canvas.

I tried using add_rewrite_rule() to create the detail page, and sending the additional query vars through url segments. That works, but sending the rewrite rule to the wordpress page means the page title and content still appear.

How should I architect this plugin so that I can link to a URL ie. /locations/address/id that doesn't really exist in WP, but rather just builds dynamically. We'd like this to use the "page" or "post" if possible, but trick WordPress into using our page title and content HTML.

Any thoughts?

2 Answers 2

0

You would use the wp_title and the_title filters as appropriate to modify that. Content? Well, you can re-use the content and place the shortcode after, or the shortcode can otherwise modify the_content...

0
1

I've dealt with this problem recently.

I would recommend adding a hook as early as possible to recognise custom page requests, like so:

// Function to run before page generation begins
add_action('init',array('MyPluginClass','MyInitPublicFunction'));

Then that function should look something like:

public function MyInitPublicFunction() {
// Custom tag we will be using to recognize page requests
add_rewrite_tag('%mycustomtag%','([^/]+)');
// Custom rewrite rule to hijack page generation
add_rewrite_rule('locations/address/([^/]+)/?$','index.php?mycustomtag=$matches[1]','top');
// Update WP rewrite rules without writing a new .htaccess file
flush_rewrite_rules(false);
}

Then later on in your plugin:

// Allow access to Wordpress query variables
global $wp_query;
// Retrieve Wordpress query variables
$query_vars = $wp_query->query_vars;
// Check if requesting our custom page 
if(!empty($query_vars['mycustomtag'])) {
// CUSTOM PAGE REQUESTED!
$custom_id = $query_vars['mycustomtag'];
die('<html><head><title>Not a Wordpress Page</title></head><body>Content here</body></html>');
}

That's not the tidiest way to end the code, and you can overwrite the title/etc using Wordpress hooks, but you only seem to want to provide pages that don't look like they're being generated in Wordpress, so why not bypass the theming engine entirely and generate your own page? You can still use Wordpress commands to retrieve data, just make sure your code runs and ends page generation before any output begins.

Don't forget! You can add a custom rewrite rule at any point (for instance, on plugin activation) as long as you use "flush_rewrite_rules" to push it into the database. This means you can have a plugin config that allows you to set up specific custom pages, or you can use a page with shortcode on to activate rewrite rules for future visits to that page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.