bp_get_canonical_url( array $args = array() )

Get the canonical URL of the current page.

Description

Parameters

$args

(Optional) Optional array of arguments.

  • 'include_query_args'
    (bool) Whether to include current URL arguments in the canonical URL returned from the function.

Default value: array()

Return

(string) Canonical URL for the current page.

Source

File: bp-core/bp-core-catchuri.php

905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
function bp_get_canonical_url( $args = array() ) {
 
    // For non-BP content, return the requested url, and let WP do the work.
    if ( bp_is_blog_page() ) {
        return bp_get_requested_url();
    }
 
    $bp = buddypress();
 
    $defaults = array(
        'include_query_args' => false // Include URL arguments, eg ?foo=bar&foo2=bar2.
    );
    $r = wp_parse_args( $args, $defaults );
    extract( $r );
 
    // Special case: when a BuddyPress directory (eg example.com/members)
    // is set to be the front page, ensure that the current canonical URL
    // is the home page URL.
    if ( 'page' == get_option( 'show_on_front' ) && $page_on_front = (int) get_option( 'page_on_front' ) ) {
        $front_page_component = array_search( $page_on_front, bp_core_get_directory_page_ids() );
 
        /*
         * If requesting the front page component directory, canonical
         * URL is the front page. We detect whether we're detecting a
         * component *directory* by checking that bp_current_action()
         * is empty - ie, this not a single item, a feed, or an item
         * type directory.
         */
        if ( false !== $front_page_component && bp_is_current_component( $front_page_component ) && ! bp_current_action() && ! bp_get_current_member_type() ) {
            $bp->canonical_stack['canonical_url'] = trailingslashit( bp_get_root_domain() );
 
        // Except when the front page is set to the registration page
        // and the current user is logged in. In this case we send to
        // the members directory to avoid redirect loops.
        } elseif ( bp_is_register_page() && 'register' == $front_page_component && is_user_logged_in() ) {
 
            /**
             * Filters the logged in register page redirect URL.
             *
             * @since BuddyPress 1.5.1
             *
             * @param string $value URL to redirect logged in members to.
             */
            $bp->canonical_stack['canonical_url'] = apply_filters( 'bp_loggedin_register_page_redirect_to', bp_get_members_directory_permalink() );
        }
    }
 
    if ( empty( $bp->canonical_stack['canonical_url'] ) ) {
        // Build the URL in the address bar.
        $requested_url  = bp_get_requested_url();
 
        // Stash query args.
        $url_stack      = explode( '?', $requested_url );
 
        // Build the canonical URL out of the redirect stack.
        if ( isset( $bp->canonical_stack['base_url'] ) )
            $url_stack[0] = $bp->canonical_stack['base_url'];
 
        if ( isset( $bp->canonical_stack['component'] ) )
            $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['component'] );
 
        if ( isset( $bp->canonical_stack['action'] ) )
            $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['action'] );
 
        if ( !empty( $bp->canonical_stack['action_variables'] ) ) {
            foreach( (array) $bp->canonical_stack['action_variables'] as $av ) {
                $url_stack[0] = trailingslashit( $url_stack[0] . $av );
            }
        }
 
        // Add trailing slash.
        $url_stack[0] = trailingslashit( $url_stack[0] );
 
        // Stash in the $bp global.
        $bp->canonical_stack['canonical_url'] = implode( '?', $url_stack );
    }
 
    $canonical_url = $bp->canonical_stack['canonical_url'];
 
    if ( !$include_query_args ) {
        $canonical_url = array_reverse( explode( '?', $canonical_url ) );
        $canonical_url = array_pop( $canonical_url );
    }
 
    /**
     * Filters the canonical url of the current page.
     *
     * @since BuddyPress 1.6.0
     *
     * @param string $canonical_url Canonical URL of the current page.
     * @param array  $args          Array of arguments to help determine canonical URL.
     */
    return apply_filters( 'bp_get_canonical_url', $canonical_url, $args );
}

Changelog

Changelog
Version Description
BuddyPress 1.6.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.