Make WP Page(s) Trigger a Plugin
-
I’d like WordPress to trigger my plugin only when navigating to certain pages.
I’ve been doing this previously by monitoring the URI for the matching page’s string, but this does not work well with varying permalink styles. So using the data available in The Loop seemed a better opportunity.
I can use: add_action(‘the_content’, “my_plugin”); to start the plugin every time and only run my content if the page’s slug is a match. But since I’m now replacing the content, if the page is not a match, nothing is displayed unless I provide the original page/post’s content. The closest I’ve come is to use: else get_the_content but that does not format according to the site’s theme, tags, etc.
<?php /* Plugin Name: MyPlugin Plugin Info: ... ... */ add_action('the_content', "MyPlugin"); function MyPlugin() { global $wp_query; $post_obj = $wp_query->get_queried_object(); $slug = $post_obj->post_name; if($slug == "load-this-page") { <- run the plugin code -> } else { // display the non-plugin page/post content echo get_the_content(); }
Anyone solved this approach? The only solutions I’ve found require users to do extra steps like installing phpExec to run the plugin from the pages or updating templates and function files. Ideally the plugin would just be activated and set up with the pages to key off of.
- The topic ‘Make WP Page(s) Trigger a Plugin’ is closed to new replies.