seo_optimization

Lazyest Gallery and SEO Plugins

This post describes how to properly display Lazyest Gallery titles and urls by SEO plugins. I have tested the most popular SEO plugins: All in One SEO Pack (AIO) by Michael Torbert, and, (my favorite) WordPress SEO (WPSEO) by Joost de Valk

Lazyest Gallery shows captions of subfolders in the browser title. This is a standard feature. However, SEO plugins will overwrite the title, leaving only the 'SEO title'  for your gallery page. You can re-enable the Lazyest Gallery titles by adding some functions to your theme.

How to change titles

The title displayed in your browser is defined in the html header by the <title></title> tags. In your theme header.php, it probably shows something like:
<title><?php wp_title( '|', true, 'right' );  bloginfo( 'name' );  ?></title>.
If you are using WPSEO, you better remove the bloginfo( 'name' ); part to prevent forced title rewrites. Forced title rewrite buffers the html output for your page and simply replaces the content between the title tags by the SEO title. There is no way for us to put our gallery title back in. So, be sure to only leave the call to wp_title.

WPSEO uses a filter for wp_title to display the SEO title. Lazyest Gallery uses the same filter, however, WPSEO overwrites the Lazyest Gallery title. The trick is to call this filter after WPSEO has changed the title. To do that, add this code to functions.php in your theme:

if ( isset( $lg_gallery )  )
  add_filter('wp_title',  array( &$lg_gallery, 'wp_title' ), 20, 3 );

It checks if the Lazyest Gallery has loaded and adds the filter again, but after WPSEO has called it (priority 20 vs 10). You can find more info on filters in the WordPress codex.

AIO uses forced title rewrites, but -luckely for us- applies its own filter, called 'aioseop_title_page' before displaying the title. Add this code to functions.php to utilize this filter:

function my_gallery_title( $title )  {
  global $lg_gallery;
  return $lg_gallery->wp_title( $title, '|', 'right' );
}
if ( isset( $lg_gallery ) )
  add_filter('aioseop_title_page', 'my_gallery_title' );

It checks if Lazyest Gallery has loaded and adds a filter for the AIO title. The filter function calls wp_title() with the arguments for the separator '|' and the direction 'right' the subpages should display. This will, in turn, trigger the standard Lazyest gallery title. The Demo Gallery shows the SEO title as defined for WPSEO, but includes the folder titles.

Canonical URLs

While we are talking about SEO, let's not forget canonical urls. A canonical url is a way to hint the search engines to point different urls to original content. This is to prevent 'duplicate content' errors. Only the original page will be indexed. The SEO plugins will insert a link in the page header like this:
<link rel="canonical" href="http://www.example.com/gallery" />
But this is not what we want for the gallery. The SEO plugin takes the gallery page url, but does not use the folders and subfolders.
AIO provides the filter 'aioseop_canonical_url' for the canonical url. Put the following code in functions.php to trigger this filter:

 
function my_gallery_canonical( $url ) {
  global $lg_gallery, $lg_offset, $lg_diroffset;
  if ( isset( $lg_gallery ) ) {  
    if ( $lg_gallery->is_gallery() ) {
      $lg_gallery->valid();
      $url = trailingslashit( $lg_gallery->get_option( 'gallery_prev' ) );
      if ( isset( $lg_gallery->file ) ) {
        if ( 'TRUE' != $lg_gallery->get_option('use_permalinks') ) {			
          $url = add_query_arg( 'file', $lg_gallery->file, $url );	
        } else {
          $url .= $lg_gallery->file;
        }			
        $url = trailingslashit( $url );		
      }
    }
    if ( isset( $lg_offset ) ) {
      if ( $lg_offset != 0 )
        $url = add_query_arg( 'lg_offset', $lg_offset, $url );
    }	 
    if ( isset( $lg_diroffset) ){
      if ( $lg_diroffset != 0 )
        $url = add_query_arg( 'lg_diroffset', $lg_diroffset, $url );
    }  	
  }
  return( $url );
}
  add_filter('aioseop_canonical_url', 'my_gallery_canonical' ); // only for AIOSEO
  add_filter('wpseo_canonical', 'my_gallery_canonical' ); // only for WPSEO

It adds the filter only if Lazyest Gallery has loaded. The function first checks if we are on the gallery page. If not, it returns the url provided by AIO. For the gallery page, it takes the requested url and strips all query variables except the variable used by lazyest gallery. After that, it checks if we are on a comment page and strips the comment-page part, which leaves our (sub)folder url.
If you examine the source for the Demo Gallery, you will see the (relative) canonical url for the folders.

One thought on “Lazyest Gallery and SEO Plugins

  1. Pingback: How to set Lazyest Gallery title and canonical URL for SEP plugins | Brimosoft

Comments are closed.