bp_get_canonical_url

Get the canonical URL of the current page.

Description

(string) bp_get_canonical_url( (array) $args = array() ); 

Returns (string)

Canonical URL for the current page.

Parameters (1)

0. $args — Optional. (array) => array()
Array of arguments.

Options

    ;        


    Usage

    1. if ( !function_exists( 'bp_get_canonical_url' ) ) { 
    2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-catchuri.php'; 
    3.  
    4. // Optional array of arguments. 
    5. $args = array(); 
    6.  
    7. // NOTICE! Understand what this does before running. 
    8. $result = bp_get_canonical_url($args); 
    9.  

    Defined (1)

    The function is defined in the following location(s).

    /bp-core/bp-core-catchuri.php  
    1. function bp_get_canonical_url( $args = array() ) { 
    2.  
    3. // For non-BP content, return the requested url, and let WP do the work. 
    4. if ( bp_is_blog_page() ) { 
    5.  
    6. $bp =buddypress); 
    7.  
    8. $defaults = array( 
    9. 'include_query_args' => false // Include URL arguments, eg ?foo=bar&foo2=bar2. 
    10. ); 
    11. $r = wp_parse_args( $args, $defaults ); 
    12. extract( $r ); 
    13.  
    14. // Special case: when a BuddyPress directory (eg example.com/members) 
    15. // is set to be the front page, ensure that the current canonical URL 
    16. // is the home page URL. 
    17. if ( 'page' == get_option( 'show_on_front' ) && $page_on_front = (int) get_option( 'page_on_front' ) ) { 
    18. $front_page_component = array_search( $page_on_front, bp_core_get_directory_page_ids() ); 
    19.  
    20. /** 
    21. * If requesting the front page component directory, canonical 
    22. * URL is the front page. We detect whether we're detecting a 
    23. * component *directory* by checking that bp_current_action() 
    24. * is empty - ie, this not a single item, a feed, or an item 
    25. * type directory. 
    26. */ 
    27. if ( false !== $front_page_component && bp_is_current_component( $front_page_component ) && ! bp_current_action() && ! bp_get_current_member_type() ) { 
    28. $bp->canonical_stack['canonical_url'] = trailingslashit( bp_get_root_domain() ); 
    29.  
    30. // Except when the front page is set to the registration page 
    31. // and the current user is logged in. In this case we send to 
    32. // the members directory to avoid redirect loops. 
    33. } elseif ( bp_is_register_page() && 'register' == $front_page_component && is_user_logged_in() ) { 
    34.  
    35. /** 
    36. * Filters the logged in register page redirect URL. 
    37. * @since 1.5.1 
    38. * @param string $value URL to redirect logged in members to. 
    39. */ 
    40.  
    41. if ( empty( $bp->canonical_stack['canonical_url'] ) ) { 
    42. // Build the URL in the address bar. 
    43. $requested_url = bp_get_requested_url(); 
    44.  
    45. // Stash query args. 
    46. $url_stack = explode( '?', $requested_url ); 
    47.  
    48. // Build the canonical URL out of the redirect stack. 
    49. if ( isset( $bp->canonical_stack['base_url'] ) ) 
    50. $url_stack[0] = $bp->canonical_stack['base_url']; 
    51.  
    52. if ( isset( $bp->canonical_stack['component'] ) ) 
    53. $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['component'] ); 
    54.  
    55. if ( isset( $bp->canonical_stack['action'] ) ) 
    56. $url_stack[0] = trailingslashit( $url_stack[0] . $bp->canonical_stack['action'] ); 
    57.  
    58. if ( !empty( $bp->canonical_stack['action_variables'] ) ) { 
    59. foreach( (array) $bp->canonical_stack['action_variables'] as $av ) { 
    60. $url_stack[0] = trailingslashit( $url_stack[0] . $av ); 
    61.  
    62. // Add trailing slash. 
    63. $url_stack[0] = trailingslashit( $url_stack[0] ); 
    64.  
    65. // Stash in the $bp global. 
    66. $bp->canonical_stack['canonical_url'] = implode( '?', $url_stack ); 
    67.  
    68. $canonical_url = $bp->canonical_stack['canonical_url']; 
    69.  
    70. if ( !$include_query_args ) { 
    71. $canonical_url = array_reverse( explode( '?', $canonical_url ) ); 
    72. $canonical_url = array_pop( $canonical_url ); 
    73.  
    74. /** 
    75. * Filters the canonical url of the current page. 
    76. * @since 1.6.0 
    77. * @param string $canonical_url Canonical URL of the current page. 
    78. * @param array $args Array of arguments to help determine canonical URL. 
    79. */ 
    80. return apply_filters( 'bp_get_canonical_url', $canonical_url, $args );