I want to load the page.php
template with the content I specify (using variables and not from the database) if the request contains a particular query string.
So lets say a user requests example.com/?var1=str1
the page template should be displayed with the title and content I specify using variables.
This is the pseudo code of what I’m trying to achieve
<?php
function my_page_function() {
if($_REQUEST['var1'] == "str1")
{
$title="This will be the title of the default page template";
$content="This content will be displayed on the default page template.";
//Load the page.php here with the title and content specified in the variables above
}
}
add_action("template_redirect","my_page_function");
?>
I wish to use this code in a plugin, so it should work with any theme’s page.php.
You can achieve that with filters on
the_content
andthe_title
:Create the template file within your Plugin, e.g.
template-pluginname.php
, then hook intotemplate_include
and tell WordPress to use it:Note: this requires that you register your query variable using
add_query_arg()
, but is safer than relying on$_REQUEST
directly.Edit
Based on this comment:
If you are absolutely intent on overriding the Post Title and Post Content, then you can use the
the_title
andthe_content
filters: