2

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.

1
  • 1
    Are you properly registering your query variable, or just relying on $_REQUEST? Sep 14, 2013 at 19:30

2 Answers 2

3

You can achieve that with filters on the_content and the_title:

function wpa_content_filter( $content ) {
    if( isset( $_REQUEST['var1'] ) && $_REQUEST['var1'] == "str1" ) {
        return 'This content will be displayed on the default page template.';
    }
    return $content;
}
add_filter( 'the_content', 'wpa_content_filter', 999 );
4
  • Thanks Milo. You're almost close to what I want but the only problem is this content is displayed using the index.php template. How do I show this using the page.php template?
    – A.Jesin
    Sep 15, 2013 at 8:59
  • You'll have to force a specific template via the template redirect action or one of the template filters. but keep in mind that a theme may not have a page.php template, it's not required for a correctly functioning theme.
    – Milo
    Sep 15, 2013 at 14:41
  • +1, since this is the end result I came to as well, too. What I'm confused about, though, is why and how the content is being displayed using the index.php template file, rather than using page.php. Sep 15, 2013 at 22:33
  • Thanks for your time @Milo and Chip. I found what I wanted here I didn't know the exact word (virtual page) of what I wanted. After googling for a long time I found the phrase "without having the page stored in the database" highlighted in the above link which was what I exactly wanted.
    – A.Jesin
    Sep 16, 2013 at 16:02
2

Create the template file within your Plugin, e.g. template-pluginname.php, then hook into template_include and tell WordPress to use it:

function wpse114181_template_include( $template ) {
    return ( '' != get_query_var( 'plugin_key' ) ? plugin_dir_path( __FILE__ ) . 'template-plugin.php' : $template );
}
add_filter( 'template_include', 'wpse114181_template_include' );

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:

The only problem with this code is that as I said earlier I want to plugin to work properly with any theme. So if I design a template file myself it wouldn't look like the currently activated theme's page.php

If you are absolutely intent on overriding the Post Title and Post Content, then you can use the the_title and the_content filters:

function wpse114181_filter_the_title( $title ) {
    if ( is_page() && '' != get_query_var( 'plugin_key' ) ) {
        return 'Your Post Title Here';
    } else {
        return $title;
    }
}
add_filter( 'the_title', 'wpse114181_filter_the_title' );

function wpse114181_filter_the_content( $content ) {
    if ( is_page() && '' != get_query_var( 'plugin_key' ) ) {
        return 'Your Post Content Here';
    } else {
        return $content;
    }
}
add_filter( 'the_content', 'wpse114181_filter_the_content' );
5
  • +1 as relying on $_REQUEST won't work in CLI environments, during cron jobs, etc.
    – kaiser
    Sep 14, 2013 at 20:40
  • Thanks Chip. The only problem with this code is that as I said earlier I want to plugin to work properly with any theme. So if I design a template file myself it wouldn't look like the currently activated theme's page.php
    – A.Jesin
    Sep 15, 2013 at 8:54
  • See edited answer. You can use the the_title and the_content filters. Sep 15, 2013 at 9:48
  • Thanks a lot Chip.<br/>You've used the is_page() function so the request URL has to contain a page_id too right? Something like example.com/?page_id=2&plugin_key=abc <br />But I want the code to "load the page template" without a page_id being requested.<br />In other words the URL will contain only the custom query variable.
    – A.Jesin
    Sep 15, 2013 at 10:35
  • Inside the Loop (where these filters are applied), the $query object is already set - which means that query conditionals such as is_page() are also already set. Sep 15, 2013 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.