0

I have a list of products, and I want to be able to click a link and get to a page with more information about the product, which I can do using $_GET like so:

$s .= '<p><a href='.get_site_url().'/wp-content/plugins/real-wp-shop/test.php?info='.$value->id.'>Details</a></p>';

And then in test.php I have:

if (isset($_GET['info'])) {
    echo 'string';
}

With that, I obviously just get a blank page with string written on it, but I was wondering if it would be possible to use a page temnplate on this page, so I would get string written in the area of the page template (like where I would get the output If I had a post loop on a regular page) ?

1

You can use the template_include filter to load a template file from your theme:

add_filter( 'template_include', 'my_page_template', 99 );
function my_page_template( $template ) {

    if ( isset( $_GET['info'] ) &&  $_GET['info'] == 'some_value' ) {
         $new_template = locate_template( array('my-template.php' ) );
         if ( '' != $new_template ) {
             return $new_template ;
         }
     }
     return $template;
}

If you want to check the tempalte file within any other location you can. For example, you can check if the theme/child theme has the template file you want to load, if not, load a default template file from your plugin path:

add_filter( 'template_include', 'my_page_template', 99 );
function my_page_template( $template ) {

    if ( isset( $_GET['info'] ) &&  $_GET['info'] == 'some_value' ) {

        $new_template = locate_template( array('my-template.php' ) );

        if ( '' != $new_template ) {
            //my-template.php file has been found in theme/child theme folder
            $template = $new_template ;
        } else {
            //load template callback when the file dosen't exist in theme
            $template = PATH_TO_MY_PLUGIN . '/my-template.php';
        }

    }

    return $template;

}

See also template_redirect filter.

| improve this answer | |
  • This looks good, but I can't figure it out. Where would I put this? It can't go inside test.php as it is, since it won't recognize add_filter function. – Ollicca Mindstorm Mar 30 '14 at 11:08
  • add_filter() is a function from Wordpress that add filters to differents actions and events in Wordpress. Be familiar with filter and action hooks in Wordpress is a must if you want to extend or customize Wordpress functionality and output. You can see add_filter() and add_action() in the Codex. – cybmeta Mar 30 '14 at 12:21
0

Yes you can. You can create a page template to use and in that use the following:

if (isset($_GET['info'])) {
  echo $_GET['info']
}
  1. Copy your default page template and creating a new one based on that. Make sure you change the value of the 'Template Name: XXXXX' also. The template should use the get_header and get_footer commands above/below your new code as a mimimmum to apply the website template to the page.

  2. Create a new page and apply the template to that page.

If you get stuck, let me know and I'll guide you more once I understand where you're having problems.

| improve this answer | |
  • But how would I make it default to that page? Should I change something in my link? As you can see, right now it links to a php file inside my plugin folder. Should I change that? – Ollicca Mindstorm Mar 30 '14 at 11:14
  • I think you need to have a read of these pages for an understanding of templates: codex.wordpress.org/Page_Templates & codex.wordpress.org/Stepping_into_Templates Once you have created the file, it should go in your theme folder, not the plugins folder. – TomC Mar 31 '14 at 6:43

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.