Making WordPress.org

Changeset 8202


Ignore:
Timestamp:
02/05/2019 02:45:25 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Support Forums: Output rel="canonical" meta tag for topic tags and views.

Props jonoaldersonwp, SergeyBiryukov.
Fixes #3951.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-hooks.php

    r8201 r8202  
    1919        add_filter( 'wp_insert_post_data',             array( $this, 'set_post_date_gmt_for_pending_posts' ) );
    2020        add_action( 'wp_print_footer_scripts',         array( $this, 'replace_quicktags_blockquote_button' ) );
     21
     22        // Output rel="canonical" meta tag.
     23        add_action( 'wp_head', array( $this, 'rel_canonical' ) );
    2124
    2225        // Link to create new topics atop topic list.
     
    270273
    271274    /**
     275     * Outputs rel="canonical" for topic tags and views.
     276     */
     277    public function rel_canonical() {
     278        if ( ! bbp_is_topic_tag() && ! bbp_is_single_view() ) {
     279            return;
     280        }
     281
     282        if ( bbp_is_topic_tag() ) {
     283            $canonical_url = bbp_get_topic_tag_link();
     284        } else {
     285            $canonical_url = bbp_get_view_url();
     286        }
     287
     288        // Make sure canonical has pagination if needed.
     289        $page = get_query_var( 'paged', 0 );
     290        if ( $page >= 2 ) {
     291            $canonical_url .= 'page/' . absint( $page ) . '/';
     292        }
     293
     294        if ( $canonical_url ) {
     295            echo '<link rel="canonical" href="' . esc_url( $canonical_url ) . '" />' . "\n";
     296        }
     297    }
     298
     299    /**
    272300     * Displays a link to the new topic form.
    273301     */
Note: See TracChangeset for help on using the changeset viewer.