bp_get_canonical_url( array $args = array() )

Get the canonical URL of the current page.


Parameters Parameters

$args

(array) (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()


Top ↑

Return Return

(string) Canonical URL for the current page.


Source Source

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

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 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 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 );
}


Top ↑

Changelog Changelog

Changelog
Version Description
1.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.