Skip to:
Content

BuddyPress.org

Changeset 5954


Ignore:
Timestamp:
03/31/2012 02:40:55 PM (14 years ago)
Author:
djpaul
Message:

Correct rel=canonical tags for BuddyPress content. Fixes #4087

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-catchuri.php

    r5952 r5954  
    591591
    592592/**
     593 * Output rel=canonical header tag for BuddyPress content
     594 *
     595 * @since 1.6
     596 */
     597function bp_rel_canonical() {
     598        // Build the URL in the address bar
     599        $requested_url  = is_ssl() ? 'https://' : 'http://';
     600        $requested_url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
     601
     602        // Stash query args
     603        $url_stack      = explode( '?', $requested_url );
     604
     605        // Build the canonical URL out of the redirect stack
     606        if ( isset( $bp->canonical_stack['base_url'] ) )
     607                $url_stack[0] = $bp->canonical_stack['base_url'];
     608
     609        if ( isset( $bp->canonical_stack['component'] ) )
     610                $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['component'] );
     611
     612        if ( isset( $bp->canonical_stack['action'] ) )
     613                $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['action'] );
     614
     615        if ( !empty( $bp->canonical_stack['action_variables'] ) ) {
     616                foreach( (array) $bp->canonical_stack['action_variables'] as $av ) {
     617                        $url_stack[0] = trailingslashit( $url_stack[0] . $av );
     618                }
     619        }
     620
     621        // Add trailing slash
     622        $url_stack[0] = trailingslashit( $url_stack[0] );
     623
     624        // Output rel=canonical tag
     625        echo "<link rel='canonical' href='" . esc_attr( $url_stack[0] ) . "' />\n";
     626}
     627
     628/**
    593629 * Remove WordPress's really awesome canonical redirect if we are trying to load
    594630 * BuddyPress specific content. Avoids issues with WordPress thinking that a
     
    607643add_action( 'bp_init', '_bp_maybe_remove_redirect_canonical' );
    608644
     645/**
     646 * Remove WordPress's rel=canonical HTML tag if we are trying to load BuddyPress
     647 * specific content.
     648 *
     649 * This function should be considered temporary, and may be removed without
     650 * notice in future versions of BuddyPress.
     651 *
     652 * @since 1.6
     653 */
     654function _bp_maybe_remove_rel_canonical() {
     655        if ( ! bp_is_blog_page() && ! is_404() ) {
     656                remove_action( 'wp_head', 'rel_canonical' );
     657                add_action( 'bp_head', 'bp_rel_canonical' );
     658        }
     659}
     660add_action( 'wp_head', '_bp_maybe_remove_rel_canonical', 8 );
    609661?>
Note: See TracChangeset for help on using the changeset viewer.