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/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.
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/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.
Share Improve this question edited Apr 18, 2015 at 7:06 Mayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges asked Apr 18, 2015 at 6:39 Warren VickWarren Vick 631 gold badge1 silver badge7 bronze badges 3- So, your question is how to set or modify the title and meta tags for a page template? – cybmeta Commented 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. – Warren Vick Commented 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 Commented Apr 18, 2015 at 8:50
3 Answers
Reset to default 3As 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:
<?php
/*
Template Name: My template
*/
get_header();
?>
<main>
<?php
if( isset( $_GET['pid'] ) ) {
// Populate your dynamic content here
}
?>
</main>
<?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.
Reference
- wp_title filter
- wp_head action
- is_page_template() function
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.
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
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)) { // GO TO MISSING PAGE. PAGE IS NOT VALID. header('Location: /missing-page/'); return; } GENERATE YOUR PAGE CODE HERE }
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; ?>">
<?PHP
}
}