I have developed a WordPress plugin which renders some dynamic html and javascript content. I would like to know how I should modify it so that it can be hooked to a single page in my wordpress application.
An idea I had was to make a bind the plugin function to a custom hook, and call this hook on the page, however, I don't know how to call the hook from a specific page. Or hook it to the part of the page you want and then use a conditional statement to only render if i is the correct page? This seems annoying because I want to be able to use my plugin on any page without modifying it. Is there a way I can pass the desired page to it as an argument?
I am looking for the "best practice" solution so I apologize if the question is a bit broad.
If this will be a public plugin, you won't be able to "bake in" the pages/posts on which it's supposed to show up.
You'll either need to:
1) Add an options page of some sort. Take a look at The Option Page Docs for more information
2) Add a Meta Box to the post types you want it to apply to that has an option in it that sets a meta value on the post.
With the information provided, I'd lean towards #2, then you can just check if the meta value is set, and if so, display your plugin's code:
if( is_single() && get_post_meta( get_the_ID(), 'run_amin_hakem_plugin', true ) == true ){
// Do your thing
}
You could then add the above code to an appropriate hook, like wp_head
, wp_footer
, or wherever your output is supposed to be displayed.
There are multiple hooks that can help you with rendering the desired html/js. The very first that comes to mind are
wp_head
wp_footer
the_content
the_title
If there is a sidebar you can add widget control plugin and show a widget on certain page.
If you end up using any of the above mentioned hooks or any other hook, you can do conditional check using one of the following:
is_page_template
\\ Given that you know the custom template in use. is_page
\\ You will need to know page id/slug or title Then there are many checks that will check if it is loading single.php or any of the taxonomy etc.
This should be a good example of targeting based on post id
add_action( 'the_content', 'my_override_function' );
function my_override_function($content) {
global $post;
if ($post->ID == 123) {
return 'Hello my friend<br>'.$content;
} else {
return $content;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.