Meta Tags for Gallery2 and WordPress

When Gallery2 is embedded in WordPress there is no standard capability to generate page meta descriptions and keyword tags for individual Gallery2 albums or photos.  The WPG2 plugin creates one WordPress page, the WPG2 page, in which all Gallery2 content is rendered.  Each time a Gallery2 page is rendered by WordPress we need to be able to generate unique meta tags so that search engines can index and identify our site Gallery2 pages. Without the capability to uniquely tag individual Gallery2 pages our photo galleries may not be indexed.

To generate meta tags for each WordPress Gallery2 page, the first step is to generate meta tags in the Gallery2 header.  In many cases the meta description tag is based upon the Gallery2 page description or title and the meta keyword tag is based upon the Gallery2 page keywords.  In my case I follow similar technique for the page description, but for keywords I want to automatically generate these values from the album contents if keywords were not specified.

Some postings on the Internet show how to do this. Meta tags are inserted into your Gallery2 theme.tpl file near the beginning, after the generation of the <title> directive. I have modified the code to default the keyword values from the Gallery2 summary text and album title. I do not assign Gallery2 keywords to album and photo pages. Instead, my pages use the summary text as a keyword to identify the album contents.


    {* If Gallery doesn't provide a header, we use the album/photo title (or filename) *}
    {if empty($head.title)}

    {* For meta tags, if no keywords for the page we default to use the title and summary text from all children in the page*}
    {assign var="keywordlist" value=$theme.item.title|markup:strip|default:$theme.item.pathComponent}
    {if count($theme.children) > 0}
       {foreach from=$theme.children item=child}
          {if !empty($child.summary)}
            {if !empty($child.title)}
 	       {assign var="keywordlist" value=$keywordlist|cat:", "}
 	       {assign var="keywordlist" value=$keywordlist|cat:$child.title|markup:strip|entitytruncate:1000}
            {assign var="keywordlist" value=$keywordlist|cat:", "}
	    {assign var="keywordlist" value=$keywordlist|cat:$child.summary|markup:strip|entitytruncate:1000}

    <meta name="description" content="{$theme.item.description|markup:strip|truncate:180:'...'|default:$theme.parent.title|markup:strip|default:$theme.item.pathComponent}" />
    <meta name="keywords" content="{$theme.item.keywords|markup:strip|default:$keywordlist}" />

The next step, once the <meta> tags are being generated in the Gallery2 HTML <head> section, is to write a new function in the WPG2 plugin to extract these tags from the Gallery2 page and insert them into the WordPress header. We will use this function in the WordPress header template when we want to write the Gallery2 meta tags. Note, this code isolates only the meta tags which have the ‘name=’ directive. This selects only the meta tags identified above and does not bring forward any other Gallery2 meta tags of different types that may duplicate existing WordPress tags. Edit the file and add the following code:

* Function Hook on WordPress Header
* WP action to add the Gallery2 meta tags to the WP header
* @param NULL
* @return NULL
function g2_addmeta() {

	if (!defined('G2INIT')) {
		$ret = g2_login();
		if ($ret) {
			echo '<h2>' . __('Fatal G2 error', 'wpg2') . '</h2> ' . __("Here's the error from G2: ", 'wpg2') . $ret->getAsHtml();

	global $g2data;

	if (defined('WPG2PAGE')) {
		// Merge Gallery2 HEAD Outputs Into WP Header 
		if (isset($g2data['headHtml'])) {
			list($g2_title, $g2_css, $g2_javascript, $g2_meta) = GalleryEmbed::parseHead($g2data['headHtml']);
			foreach ($g2_meta as $wpg2_meta) { 
				if (strpos("$wpg2_meta","name=")) { echo "$wpg2_meta rn"; }
	// Add BTEV Event Message
	if (function_exists('btev_trigger_error')) {
		btev_trigger_error('WPG2 ADDED G2 HEADER TO WP HEADER', E_USER_NOTICE, __FILE__);

Lastly, you need to find the section in your WordPress header template where meta tags are written. In my case I am using the Atahualpa theme and this theme has put some hooks into the header generation to call special functions. I updated bfa_meta_tags.php and inserted the call to our new function g2_addmeta() in the section where meta tags were generated for static and single post pages. Note, for this solution to work correctly you do not want to define meta tags or descriptions within your WordPress theme for the WPG2 page, otherwise you will generate both the theme meta tags and the Gallery2 meta tags.


// META DESCRIPTION & KEYWORDS Tag for single post pages and static pages:
if ( is_single() OR is_page() ) {
	$bfa_meta_description = get_post_meta($post->ID, 'bfa_ata_meta_description', true);
	$bfa_meta_keywords = get_post_meta($post->ID, 'bfa_ata_meta_keywords', true);
	if ( $bfa_meta_description != '' ) {
		echo "<meta name="description" content="" .
    	htmlentities($bfa_meta_description,ENT_QUOTES,'UTF-8') . "" />n";
	if ( $bfa_meta_keywords != '' ) {
		echo "<meta name="keywords" content="" .
    	htmlentities($bfa_meta_keywords,ENT_QUOTES,'UTF-8') . "" />n";
	// META DESCRIPTION Tag for WPG2 pages:
	g2_addmeta() ;