Skip to content

Instantly share code, notes, and snippets.

@benhuson
Created January 28, 2013 22:35
Show Gist options
  • Save benhuson/4659950 to your computer and use it in GitHub Desktop.
Save benhuson/4659950 to your computer and use it in GitHub Desktop.
Setup canonical links for WPEC products in multiple categories.
<?php
/**
* Fix canonical for posts in multiple categories
*/
class CanonicalCategoryFix {
function CanonicalCategoryFix() {
add_filter( 'wpseo_canonical', array( $this, 'canonical_permalink' ) );
add_filter( 'wpsc_change_canonical_url', array( $this, 'canonical_permalink' ) );
}
function canonical_permalink( $canonical ) {
global $post;
if ( is_single() && get_post_type() == 'wpsc-product' ) {
add_filter( 'wp_get_object_terms', array( $this, 'wp_get_object_terms_single' ) );
$canonical = get_permalink( $post->ID );
remove_filter( 'wp_get_object_terms', array( $this, 'wp_get_object_terms_single' ) );
}
return $canonical;
}
function wp_get_object_terms_single( $terms, $object_ids = null, $taxonomies = null, $args = null ) {
if ( is_array( $terms ) && count( $terms ) > 0 ) {
return array( $terms[0] );
}
return $terms;
}
}
$CanonicalCategoryFix = new CanonicalCategoryFix();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment