0

I'm developing a plugin that needs to create a page template that uses any theme's 'page.php' file. The intent is to read the source code of that theme's page.php, modify it to add additional functions, and store the modified template file in the plugins folder. The new template will then be made available on a page's 'template' dropdown (using the 'template_include' filter as described here).

The new template needs to add additional plugin scripts ('includes'), and will have a shortcode that will insert a form at the shortcode's location.

Since all themes have their own version of a 'page.php' file, I need to use the structure of the theme's page.php file (with all it's CSS and whatever content), so that using the new template on a new page will result in a page that matches the theme's 'look'.

Is there a function that will figure out the page.php template file?

Or is there a way that a shortcode can add 'includes' to a page?

6
  • If you're using a shortcode I don't understand why you need to copy the theme's template? If you need functions to handle a form submission then those don't belong in the template and you should handle it with action callbacks. Commented Oct 19, 2022 at 8:00
  • Also, if you're copying the theme's template that copy will be deleted whenever the theme is updated. Commented Oct 19, 2022 at 8:00
  • @JacobPeattie The new template will be stored in the plugin's folder, and inserted via the template_include filter. So will survive a theme update. The new template will have a shortcode. I'm not sure if there is a way to 'sense' a shortcode and then use the wp_head/wp_footer add_action to insert additional code if the shortcode is in the page's content. That might be another question, though. (The shortcode requires additional code to function properly.) Commented Oct 19, 2022 at 18:04
  • This just seems like a really problematic approach. Shortcodes typically go in post content so why are you using a shortcode in a template? And how are you determining where to put the shortcode? If you're just looking for the_content() then why not just filter that instead of copying the template? Commented Oct 19, 2022 at 18:59
  • Sorry for a misunderstanding. If the shortcode is in the content, then I need to use wp_head/wp_footer actions to add additional code to the page when displayed, using any template. So if there is a way to 'sense' a shortcode before the page is output, that would be used instead of a custom template. Commented Oct 20, 2022 at 18:36

1 Answer 1

0

Further contemplation, and looking at the template hierarchy, lets me know that every theme needs at least a page.php, or singular.php, or the index.php file is used.

So, I just need to figure out the current theme directory (via get_theme_directory function), and see if the page.php file exists. If so, use the code from that template as the basis for the new template.

If it doesn't exist, then look for and use singular.php and index.php in that order.

And if that doesn't exist, the theme has problems, and probably wouldn't work correctly.

So, there is no need to figure out what template a theme will use for a page. They will have a page.php (or a page-something.php, which I can look for).

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.