Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Dynamic Page Title and Description WordPress

I am using php via a custom shortcode on a page to create dynamic content from a mysql database. I need to alter the title (html header) and meta description from values I pull from the database. The mysql data is pulled by selecting a row based on a POST value. i.e. page?id=123. I am open to creating a page template to accomplish this.

I am fairly new to wordpress but familiar with php, scripting, html, css, mysql, ftp etc.. so I don't need a lot of hand holding.

Answers (4)

2012-06-05

Romel Apuya answers:

I can surely help you with this.


chrisrth comments:

will you be suggesting a plugin or straight forward code edit?

just so we are clear... I call mypage?id=123, then I use shortcodes ui to run php to query a database and I want to use that data (which is delivered dynamically using the plugin) to alter the title and description. If we can keep it simple and avoid a bloated plugin I would prefer that but all options are open at this point.

2012-06-05

Arnav Joy answers:

if you are ready to make page template then i suggest to make it and use folowing plugin , it will give you option to change all this

http://wordpress.org/extend/plugins/seo-ultimate/


Arnav Joy comments:

but if you still want to use this then you can use wp_title() function to change the title , see the following:-

http://codex.wordpress.org/Function_Reference/wp_title


you can also achieve this using meta field see this

http://digwp.com/2010/04/custom-page-titles/


let me know if you need my help in building all this


Arnav Joy comments:

please see this url for some info of custom fields in wordpress
http://codex.wordpress.org/Custom_Fields


chrisrth comments:

just so we are clear... I call mypage?id=123, then I use shortcodes ui to run php to query a database and I want to use that data (which is delivered dynamically using the plugin) to alter the title and description. If we can keep it simple and avoid a bloated plugin I would prefer that but all options are open at this point.


Arnav Joy comments:

so i think you should create custom fields for it at pages
say you create two custom fields

1. for title (page_meta_title)
2. for meta description (page_meta_description)

now to call it you can use following in header.php

<?php
// $_GET['id'] id is the id of the page you want to fetch info
if(isset($_GET['id'])){
$page_meta_title = get_post_meta($_GET['id'],'page_meta_title',true);
$page_meta_description = get_post_meta($_GET['id'],'page_meta_description',true);
}


echo "<title>";
if(isset($_GET['id']) && !empty($page_meta_title))
echo $page_meta_title; // this will be based on the custom field's value of page
else
wp_title( '|', true, 'right' ); // this will be default
echo "</title>";

if(isset($_GET['id']) && !empty($page_meta_description))
echo '<meta name="description" content="'.$page_meta_description.'" />';
else
echo '<meta name="description" content="write description here for all other pages" />';

?>


chrisrth comments:

this looks the most straightforward. I will test and let award accordingly thanks.

2012-06-05

Jatin Soni answers:

Alright! as I understood your requirement is that you want to alter title and meta tag as per your page here e.g. if there is page?id=123 loaded than title and meta tags will be different than normal(other pages).

So there are few ways to do that but easiest way to do is below. This code you need to place into <head> tag in header.php where title and metatags located <titlte></title>

if(is_page()) {
// your content here
} else {
default content
}

now you can call your page with id, name or slug. so code will be like this

if(is_page('123')) {

//this is new title

} else {

//the default title

}


You can assigne variable and can create function too like

function my_custom_header($id, $newtitle, $defaulttitle){

if(is_page($ids,$newtitle,$defaulttitle)) {
//this is new title
$newtitle;
} else {
//the default title
$defaulttitle;
}
}


Above function is an example you may need to tweak little bit. but only if else also will work.

If you want to use the same thing for post and not for the page than use is_single() instead of is_page()


Okay below code I have tested for post and page both and it works completely fine.

global $page, $paged;

if (is_single('1039')) {
echo 'THis is not a post | ';
} elseif (is_page('797')) {
echo 'All Pages';
} else {
wp_title( '|', true, 'right' );

// Add the blog name.
bloginfo( 'name' );
}


chrisrth comments:

this is good but I guess I would have to move my query to header.php so I can pull the title and description values from the DB. the way I have it coded now is that the queries are running in a shortcode and probably too late to override the title at that point?


Jatin Soni comments:

Okay, here I found really nice code may be it will works for you check this.

http://digwp.com/2010/04/custom-page-titles/

2012-06-05

Francisco Javier Carazo Gil answers:

Hi Chrisrth,

I suppose you have a SQL table with: post_id, custom_title. I suggest you to use meta info in posts.

This have many advantages. All will be included in WP structure and you will use WP API.

Also, you will be able to use custom box into insert and edit post, so I think you should use it.

Take it: http://codex.wordpress.org/Custom_Fields

If you have a SQL table filled with data, don't worry. You can do an easy PHP script to migrate it using WP API, exactlu this function:


add_post_meta($post_id, $meta_key, $meta_value, $unique);


You will use your $meta_key, i. e. "custom_title" and $meta_value will be the value of this. $unique will be true if you want only 1 custom title per post or false (default) if you want be able to set more than 1 title.