I see this subject comes up regularly but have not seen an answer to my specific requirement yet.

I'm trying to use a WordPress page as a template and insert database-sourced content (about 20 or so fields of text, including image file names) based on an ID passed as a URL parameter (and index to my database). e.g. www.example.com/examplepage/?pid=123

Before WordPress, I could do this in PHP easily by executing some code to get a database record and then writing out HTML interspersed with those fields.

I have a plugin in WordPress that allows me to do some PHP code snippets on page, but that's in the page body and I believe the header has already been written out. The header has fields like title and meta desc that I'd like to be populated by dynamic content.

I've seen plug-ins for CMS-like management of real estate listings, movies, etc. but my database handling is a but unusual so I have to take a custom build approach.

I understand that I may need to do some work in the functions.php script for my theme in order to dig into WordPress' page rendering, but I'd like to be careful not to disturb general pages/posts on my site. It's just this one special page that will accept a parameters and load the appropriate content.

Some advice on the steps I need to take would be most appreciated.

  • So, your question is how to set or modify the title and meta tags for a page template?
    – cybmeta
    Apr 18, 2015 at 8:32
  • Not just the title and meta tags, but population of content in the page body too. All from content picked up in a single DB query searching by pid. Apr 18, 2015 at 8:39
  • You said that you already know how to populate content using PHP, there is nothing stopping you to include that PHP code in a page template.
    – cybmeta
    Apr 18, 2015 at 8:50

3 Answers 3


As you are building a page template, you can insert in the content of that template whatever you want and use any PHP snippet you want. So, you can continue doing it as you was doing in PHP. For example, this could be your page template:

Template Name: My template



    if( isset( $_GET['pid'] ) ) {

        // Populate your dynamic content here



<?php get_footer(); ?>

But you are correct about title and meta tags of the document, they may be already set. Here you need to hook into wp_title filter and wp_head action, using is_page_template function to check if you are in the page template.

For example, suppose that your page tempalte file name is something like page-mytemplate.php and it is located in the root of your theme:

add_filter( 'wp_title', function( $title, $sep ) {

    if( is_page_template( 'page-mytemplate.php' ) ) {

        // Modify the $title here
        $title = 'my new title';


    return $title;

}, 10, 2 );

add_action( 'wp_head', function() {

    if( is_page_template( 'page-mytemplate.php' ) ) {

        // Echo/print your meta tags here
        echo '<meta name=....>';


} );

The problem

The are a big problem with <meta> tags. WordPress has not a standard way to manage <meta> tags of the docuemnt. If you use any plugin that add <meta> tags, you won't be able to override them unless the plugin offer a way to do it.


  • Thanks for that. I found a reference to WordPress page templates and will give that a try. Re. your notes about <meta> tags, I suspect my Yoast plugin is going to get in the way. I would Vote++ but I'm a newb and don't have voting rights yet! Apr 18, 2015 at 12:00
  • I'm sorry but I don't understand you. You said that you was using a page template, so I assumed you know how to build a page template; I don't understand what you are saying now about a reference to page templates. I think I answered exactly what you asked, so I think my answer should be marked as accepted; if not, please tell me why this answer doesn't respond your question. By the way: your actual reputation is enough to accept answers.
    – cybmeta
    Apr 18, 2015 at 13:59
  • Maybe I misunderstood you. You said "use a WordPress page as a template" and I thought you was talking about WordPress page templates; that's why I started the answer with "As you are building a page template".
    – cybmeta
    Apr 18, 2015 at 14:35
  • Sorry... new to StackExchange and assumed voting was the nod of approval. I've now found the tick/check mark. You're correct that I used the word "template" generically in my question, but I now see that my best way forward would be to use a WordPress "Specialized Page Template" which works for one page in particular. I can easily build my PHP database work in there. It would be nice to keep the layout design in the page and inject my database fields... I suspect short codes may be my friend for this? Thanks again for the comprehensive answer. Apr 20, 2015 at 16:59
  • As a page template is a PHP file, you can build it as you want. With or without shortcodes. Use shortcodes if you want to print information anywhere else, not only in the page template. I mean, you could use a PHP function in the page template and use that function also as callback for a shortcode; but shortcodes are not required at all for a page template. Here you can see how a sortcode is just a placeholder for a PHP function and how you can use them separately.
    – cybmeta
    Apr 20, 2015 at 17:16

I might be missing something but:

If you only need the 'pid' for example and it is part of the URL then its fairly simple?

In the page template 'examplepage' just do a:

$mypid = $_GET['pid'];

in there and then carry on with the SQL query based on that value and echo out a response.

If you want something a little more ajax based then when the user lands on 'examplepage' take the 'pid' and add as a class or data attribute on an empty div. Say called: 'results-container'

<?php $mypid = $_GET['pid']; ?>
<div class"results-container" mypid="<?php echo $mypid; ?>"></div>

And then have a js script execute on document ready that reads the mypid attribute on that div and does a seperate fetch. This way the page can load and show a 'fetching' status and some preliminary content while the results load in.

Hope that made sense.

-Typed with fat fingers on a mobile phone! aaaaah.

  • If the dynamic content was only in the body, then you're right, that would be it. But I also need to override things in the header like page title and meta description. Apr 18, 2015 at 8:27

I have done this myself just recently. This may be late to the party, but in the event other want to know how, these were my steps in a wordpress environment

  1. Create a function that read the URL parameter, such as:

    function details_Page($atts)
      global $wpdb; 
     // Get the database record for this details
     $DatabaseId = $_GET['pid'];
     if(!is_numeric ($DatabaseId))
       header('Location: /missing-page/');  

Now create a shortcode for this function or use it in your page template. Functionally speaking, this is another way to do a page template.

         add_shortcode('DETAILS_PAGE', 'details_Page');

Now add the shortcode to the specifically defined page (or template).

To change the header to match you data, making sure the add_action call is in the primary loop or functions.php. Otherwise you will have a race condition.

 add_action( 'wp_head', 'MMD_listings_add_custom_meta', 10 );
 function add_custom_meta()
   $slug = basename(get_permalink());  // I use for the particular page
   if(  $slug == 'details')
     $Name = $_GET[ 'Name' ];
     $Desc = $_GET[ 'Desc' ];
     $Logo = $_GET[ 'Logo' ];
    <meta content="<?php echo $Name; ?>>"/>
    <meta content="<?php echo $Desc; ?>">

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.