I have a client with a fully fleshed out and functional WordPress website with a child theme. I am creating a plugin for them to take in form data from not-logged-in users and send it off to an external API.
At first, I thought I would just put the form in a shortcode, and stick that shortcode in a page, not unlike an ecommerce plugin that creates a ‘store’ page.
The client wants this form to be styled completely differently than the rest of the website. Like a secondary Theme. And I would like the entire project to be bundled in a Plugin so future changes to the primary theme have no effect on the Plugin.
I have found some ways to hack the WordPress process by checking $_SERVER['REQUEST_URI'] in a add_action('init','foo'). That loads my template using load_template(), but also loads the theme header/body/footer after creating a nice Frankenstein’s Monster of a page.
I am hoping someone on here knows a more stable way to overload the Theme from a Plugin for a couple of urls.
If you need to see more code, please ask. I don’t know what functions if any will be helpful.
EDIT: My solution hinged mainly on recognizing that you need to return false after load_template() otherwise WordPress will continue loading a template from the theme.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There doesn’t appear to be any good hook to switch out the Header or Footer templates. Since you need to change both the header and footer of the theme, it might be best to make a page template in your Child Theme. Something like this:
<?PHP
/**
* Template Name: Plugin XYZ
* plugin-xyz.php
*/
// Will look for a header-plugin-xyz.php file.
get_header( 'plugin-xyz' );
// Arbitrary action hook for your plugin.
do_action( 'plugin_xyz_before_content' );
if( have_content() ) {
while( have_posts() ) {
the_post();
the_title( '<h1 class="entry-title">', '</h1>' );
the_content();
}
}
// Arbitrary action hook for your plugin.
do_action( 'plugin_xyz_after_content' );
// Will look for a footer-plugin-xyz.php file.
get_footer( 'plugin-xyz' );
Creating and calling header-plugin-xyz.php and footer-plugin-xyz.php in your Child Theme will allow you to load in theme-related elements and styles while still making them different than the rest of the website. By making this a page template, you can assign this to a specific page and load in page assets like content or related metadata. You could add hooks throughout this template for your plugin to hook into and modify or inject further templates or content.
The alternative, to keep everything contained in the plugin is to create a template without calling get_header() and get_footer(). You would then need to create your own header and footer elements ( so that wp_head() and wp_footer() get called ). You could create an Endpoint (Make Tutorial) and use load_template hook to call your custom template when loading your custom endpoint.
If you have a Child Theme and this is a one-off plugin then I would suggest the first. If you want to expand this functionality out to other themes then I would suggest not trying to override the theme header and footer as there aren’t good hook to manage these.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0