Skip to:
Content

BuddyPress.org

Changeset 10402


Ignore:
Timestamp:
12/13/2015 04:45:43 PM (11 years ago)
Author:
imath
Message:

Make sure document/page title tags are correctly generated for BuddyPress pages

  • Remove all the BuddyPress process to create BuddyPress title parts from bp_modify_page_title() and create the new function

bp_get_title_parts() and put this process here. This allowes plugin/theme to directly use it if needed.

  • Keep the filter on wp_title so that themes not supporting the title-tag feature or breadcrumb plugins can still easily get BuddyPress title parts.
  • Filter document_title_parts with a new function bp_modify_document_title_parts() to build the BuddyPress title parts if needed.
  • Both functions bp_modify_page_title() and bp_modify_document_title_parts() are using bp_get_title_parts().

Props slaFFik, mercime, r-a-y, DJPaul

See #6675 (branch 2.4)

Location:
branches/2.4/src/bp-core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.4/src/bp-core/bp-core-filters.php

    r10276 r10402  
    661661 */
    662662function bp_modify_page_title( $title = '', $sep = '»', $seplocation = 'right' ) {
    663         global $bp, $paged, $page, $_wp_theme_features;
    664 
    665         // If this is not a BP page, just return the title produced by WP
    666         if ( bp_is_blog_page() ) {
     663        global $paged, $page, $_wp_theme_features;
     664
     665        // Get the BuddyPress title parts
     666        $bp_title_parts = bp_get_title_parts( $seplocation );
     667
     668        // If not set, simply return the original title
     669        if ( ! $bp_title_parts ) {
    667670                return $title;
    668         }
    669 
    670         // If this is a 404, let WordPress handle it
    671         if ( is_404() ) {
    672                 return $title;
    673         }
    674 
    675         // If this is the front page of the site, return WP's title
    676         if ( is_front_page() || is_home() ) {
    677                 return $title;
    678         }
    679 
    680         // Return WP's title if not a BuddyPress page
    681         if ( ! is_buddypress() ) {
    682                 return $title;
    683         }
    684 
    685         // Setup an empty title parts array
    686         $title_parts = array();
    687 
    688         // Is there a displayed user, and do they have a name?
    689         $displayed_user_name = bp_get_displayed_user_fullname();
    690 
    691         // Displayed user
    692         if ( ! empty( $displayed_user_name ) && ! is_404() ) {
    693 
    694                 // Get the component's ID to try and get its name
    695                 $component_id = $component_name = bp_current_component();
    696 
    697                 // Set empty subnav name
    698                 $component_subnav_name = '';
    699 
    700                 // Use the component nav name
    701                 if ( ! empty( $bp->bp_nav[$component_id] ) ) {
    702                         $component_name = _bp_strip_spans_from_title( $bp->bp_nav[ $component_id ]['name'] );
    703 
    704                 // Fall back on the component ID
    705                 } elseif ( ! empty( $bp->{$component_id}->id ) ) {
    706                         $component_name = ucwords( $bp->{$component_id}->id );
    707                 }
    708 
    709                 // Append action name if we're on a member component sub-page
    710                 if ( ! empty( $bp->bp_options_nav[ $component_id ] ) && ! empty( $bp->canonical_stack['action'] ) ) {
    711                         $component_subnav_name = wp_filter_object_list( $bp->bp_options_nav[ $component_id ], array( 'slug' => bp_current_action() ), 'and', 'name' );
    712 
    713                         if ( ! empty( $component_subnav_name ) ) {
    714                                 $component_subnav_name = array_shift( $component_subnav_name );
    715                         }
    716                 }
    717 
    718                 // If on the user profile's landing page, just use the fullname
    719                 if ( bp_is_current_component( $bp->default_component ) && ( bp_get_requested_url() === bp_displayed_user_domain() ) ) {
    720                         $title_parts[] = $displayed_user_name;
    721 
    722                 // Use component name on member pages
    723                 } else {
    724                         $title_parts = array_merge( $title_parts, array_map( 'strip_tags', array(
    725                                 $displayed_user_name,
    726                                 $component_name,
    727                         ) ) );
    728 
    729                         // If we have a subnav name, add it separately for localization
    730                         if ( ! empty( $component_subnav_name ) ) {
    731                                 $title_parts[] = strip_tags( $component_subnav_name );
    732                         }
    733                 }
    734 
    735         // A single group
    736         } elseif ( bp_is_active( 'groups' ) && ! empty( $bp->groups->current_group ) && ! empty( $bp->bp_options_nav[ $bp->groups->current_group->slug ] ) ) {
    737                 $subnav      = isset( $bp->bp_options_nav[ $bp->groups->current_group->slug ][ bp_current_action() ]['name'] ) ? $bp->bp_options_nav[ $bp->groups->current_group->slug ][ bp_current_action() ]['name'] : '';
    738                 $title_parts = array( $bp->bp_options_title, $subnav );
    739 
    740         // A single item from a component other than groups
    741         } elseif ( bp_is_single_item() ) {
    742                 $title_parts = array( $bp->bp_options_title, $bp->bp_options_nav[ bp_current_item() ][ bp_current_action() ]['name'] );
    743 
    744         // An index or directory
    745         } elseif ( bp_is_directory() ) {
    746                 $current_component = bp_current_component();
    747 
    748                 // No current component (when does this happen?)
    749                 $title_parts = array( _x( 'Directory', 'component directory title', 'buddypress' ) );
    750 
    751                 if ( ! empty( $current_component ) ) {
    752                         $title_parts = array( bp_get_directory_title( $current_component ) );
    753                 }
    754 
    755         // Sign up page
    756         } elseif ( bp_is_register_page() ) {
    757                 $title_parts = array( __( 'Create an Account', 'buddypress' ) );
    758 
    759         // Activation page
    760         } elseif ( bp_is_activation_page() ) {
    761                 $title_parts = array( __( 'Activate Your Account', 'buddypress' ) );
    762 
    763         // Group creation page
    764         } elseif ( bp_is_group_create() ) {
    765                 $title_parts = array( __( 'Create a Group', 'buddypress' ) );
    766 
    767         // Blog creation page
    768         } elseif ( bp_is_create_blog() ) {
    769                 $title_parts = array( __( 'Create a Site', 'buddypress' ) );
    770         }
    771 
    772         // Strip spans
    773         $title_parts = array_map( '_bp_strip_spans_from_title', $title_parts );
    774 
    775         // sep on right, so reverse the order
    776         if ( 'right' == $seplocation ) {
    777                 $title_parts = array_reverse( $title_parts );
    778671        }
    779672
     
    791684        // Append the site title to title parts if theme supports title tag
    792685        if ( true === $title_tag_compatibility ) {
    793                 $title_parts[] = $blogname;
    794 
    795                 if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
    796                         $title_parts[] = sprintf( __( 'Page %s', 'buddypress' ), max( $paged, $page ) );
     686                $bp_title_parts['site'] = $blogname;
     687
     688                if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() && ! bp_is_single_activity() ) {
     689                        $bp_title_parts['page'] = sprintf( __( 'Page %s', 'buddypress' ), max( $paged, $page ) );
    797690                }
    798691        }
     
    802695
    803696        // Join the parts together
    804         $new_title = join( $prefix, array_filter( $title_parts ) );
     697        $new_title = join( $prefix, array_filter( $bp_title_parts ) );
    805698
    806699        // Append the prefix for pre `title-tag` compatibility
     
    810703
    811704        /**
    812          * Filters the page title for BuddyPress pages.
     705         * Filters the older 'wp_title' page title for BuddyPress pages.
    813706         *
    814707         * @since  1.5.0
     
    821714        return apply_filters( 'bp_modify_page_title', $new_title, $title, $sep, $seplocation );
    822715}
    823 add_filter( 'wp_title', 'bp_modify_page_title', 20, 3 );
    824 add_filter( 'bp_modify_page_title', 'wptexturize'     );
    825 add_filter( 'bp_modify_page_title', 'convert_chars'   );
    826 add_filter( 'bp_modify_page_title', 'esc_html'        );
    827 
    828 /**
    829  * Strip span tags out of title part strings.
    830  *
    831  * This is a temporary function for compatibility with WordPress versions
    832  * less than 4.0, and should be removed at a later date.
    833  *
    834  * @param  string $title_part
    835  *
    836  * @return string
    837  */
    838 function _bp_strip_spans_from_title( $title_part = '' ) {
    839         $title = $title_part;
    840         $span = strpos( $title, '<span' );
    841         if ( false !== $span ) {
    842                 $title = substr( $title, 0, $span - 1 );
    843         }
    844         return $title;
    845 }
     716add_filter( 'wp_title',             'bp_modify_page_title', 20, 3 );
     717add_filter( 'bp_modify_page_title', 'wptexturize'                 );
     718add_filter( 'bp_modify_page_title', 'convert_chars'               );
     719add_filter( 'bp_modify_page_title', 'esc_html'                    );
     720
     721/**
     722 * Filter the document title for BuddyPress pages.
     723 *
     724 * @since 2.4.3
     725 *
     726 * @param array $title The WordPress document title parts
     727 * @return array the unchanged title parts or the BuddyPress ones
     728 */
     729function bp_modify_document_title_parts( $title = array() ) {
     730        // Get the BuddyPress title parts
     731        $bp_title_parts = bp_get_title_parts();
     732
     733        // If not set, simply return the original title
     734        if ( ! $bp_title_parts ) {
     735                return $title;
     736        }
     737
     738        // Get the separator used by wp_get_document_title()
     739        $sep = apply_filters( 'document_title_separator', '-' );
     740
     741        // Build the BuddyPress portion of the title.
     742        // We don't need to sanitize this as WordPress will take care of it.
     743        $bp_title = array(
     744                'title' => join( " $sep ", $bp_title_parts )
     745        );
     746
     747        // Add the pagination number if needed (not sure if this is necessary).
     748        if ( isset( $title['page'] ) && ! bp_is_single_activity() ) {
     749                $bp_title['page'] = $title['page'];
     750        }
     751
     752        // Add the sitename if needed.
     753        if ( isset( $title['site'] ) ) {
     754                $bp_title['site'] = $title['site'];
     755        }
     756
     757        /**
     758         * Filters BuddyPress title parts that will be used into the document title.
     759         *
     760         * @since  2.4.3
     761         *
     762         * @param  array $bp_title   The BuddyPress page title parts.
     763         * @param  array $title      The original WordPress title parts.
     764         */
     765        return apply_filters( 'bp_modify_document_title_parts', $bp_title, $title );
     766}
     767add_filter( 'document_title_parts', 'bp_modify_document_title_parts', 20, 1 );
    846768
    847769/**
  • branches/2.4/src/bp-core/bp-core-functions.php

    r10110 r10402  
    22972297add_action( 'bp_init', 'bp_remove_adjacent_posts_rel_link' );
    22982298
     2299/**
     2300 * Strip the span count of a menu item or of a title part.
     2301 *
     2302 * @since 2.2.2
     2303 *
     2304 * @param string $title_part Title part to clean up.
     2305 * @return string
     2306 */
     2307function _bp_strip_spans_from_title( $title_part = '' ) {
     2308        $title = $title_part;
     2309        $span = strpos( $title, '<span' );
     2310        if ( false !== $span ) {
     2311                $title = substr( $title, 0, $span - 1 );
     2312        }
     2313        return trim( $title );
     2314}
     2315
    22992316/** Nav Menu ******************************************************************/
    23002317
     
    23482365
    23492366                // Remove <span>number</span>
    2350                 $item_name = preg_replace( '/([.0-9]+)/', '', $bp_item['name'] );
    2351                 $item_name = trim( strip_tags( $item_name ) );
     2367                $item_name = _bp_strip_spans_from_title( $bp_item['name'] );
    23522368
    23532369                $page_args[ $bp_item['slug'] ] = (object) array(
  • branches/2.4/src/bp-core/bp-core-template.php

    r10209 r10402  
    27952795function bp_is_register_page() {
    27962796        return (bool) bp_is_current_component( 'register' );
     2797}
     2798
     2799/**
     2800 * Get the title parts of the BuddyPress displayed page
     2801 *
     2802 * @since 2.4.3
     2803 *
     2804 * @param string $seplocation
     2805 * @return array the title parts
     2806 */
     2807function bp_get_title_parts( $seplocation = 'right' ) {
     2808        $bp = buddypress();
     2809
     2810        // Defaults to an empty array
     2811        $bp_title_parts = array();
     2812
     2813        // If this is not a BP page, return the empty array.
     2814        if ( bp_is_blog_page() ) {
     2815                return $bp_title_parts;
     2816        }
     2817
     2818        // If this is a 404, return the empty array.
     2819        if ( is_404() ) {
     2820                return $bp_title_parts;
     2821        }
     2822
     2823        // If this is the front page of the site, return the empty array.
     2824        if ( is_front_page() || is_home() ) {
     2825                return $bp_title_parts;
     2826        }
     2827
     2828        // Return the empty array if not a BuddyPress page.
     2829        if ( ! is_buddypress() ) {
     2830                return $bp_title_parts;
     2831        }
     2832
     2833        // Now we can build the BP Title Parts
     2834        // Is there a displayed user, and do they have a name?
     2835        $displayed_user_name = bp_get_displayed_user_fullname();
     2836
     2837        // Displayed user.
     2838        if ( ! empty( $displayed_user_name ) && ! is_404() ) {
     2839
     2840                // Get the component's ID to try and get its name.
     2841                $component_id = $component_name = bp_current_component();
     2842
     2843                // Set empty subnav name.
     2844                $component_subnav_name = '';
     2845
     2846                // Use the component nav name.
     2847                if ( ! empty( $bp->bp_nav[$component_id] ) ) {
     2848                        $component_name = _bp_strip_spans_from_title( $bp->bp_nav[ $component_id ]['name'] );
     2849
     2850                // Fall back on the component ID.
     2851                } elseif ( ! empty( $bp->{$component_id}->id ) ) {
     2852                        $component_name = ucwords( $bp->{$component_id}->id );
     2853                }
     2854
     2855                // Append action name if we're on a member component sub-page.
     2856                if ( ! empty( $bp->bp_options_nav[ $component_id ] ) && ! empty( $bp->canonical_stack['action'] ) ) {
     2857                        $component_subnav_name = wp_filter_object_list( $bp->bp_options_nav[ $component_id ], array( 'slug' => bp_current_action() ), 'and', 'name' );
     2858
     2859                        if ( ! empty( $component_subnav_name ) ) {
     2860                                $component_subnav_name = array_shift( $component_subnav_name );
     2861                        }
     2862                }
     2863
     2864                // If on the user profile's landing page, just use the fullname.
     2865                if ( bp_is_current_component( $bp->default_component ) && ( bp_get_requested_url() === bp_displayed_user_domain() ) ) {
     2866                        $bp_title_parts[] = $displayed_user_name;
     2867
     2868                // Use component name on member pages.
     2869                } else {
     2870                        $bp_title_parts = array_merge( $bp_title_parts, array_map( 'strip_tags', array(
     2871                                $displayed_user_name,
     2872                                $component_name,
     2873                        ) ) );
     2874
     2875                        // If we have a subnav name, add it separately for localization.
     2876                        if ( ! empty( $component_subnav_name ) ) {
     2877                                $bp_title_parts[] = strip_tags( $component_subnav_name );
     2878                        }
     2879                }
     2880
     2881        // A single group.
     2882        } elseif ( bp_is_active( 'groups' ) && ! empty( $bp->groups->current_group ) && ! empty( $bp->bp_options_nav[ $bp->groups->current_group->slug ] ) ) {
     2883                $subnav      = isset( $bp->bp_options_nav[ $bp->groups->current_group->slug ][ bp_current_action() ]['name'] ) ? $bp->bp_options_nav[ $bp->groups->current_group->slug ][ bp_current_action() ]['name'] : '';
     2884                $bp_title_parts = array( $bp->bp_options_title, $subnav );
     2885
     2886        // A single item from a component other than groups.
     2887        } elseif ( bp_is_single_item() ) {
     2888                $bp_title_parts = array( $bp->bp_options_title, $bp->bp_options_nav[ bp_current_item() ][ bp_current_action() ]['name'] );
     2889
     2890        // An index or directory.
     2891        } elseif ( bp_is_directory() ) {
     2892                $current_component = bp_current_component();
     2893
     2894                // No current component (when does this happen?).
     2895                $bp_title_parts = array( _x( 'Directory', 'component directory title', 'buddypress' ) );
     2896
     2897                if ( ! empty( $current_component ) ) {
     2898                        $bp_title_parts = array( bp_get_directory_title( $current_component ) );
     2899                }
     2900
     2901        // Sign up page.
     2902        } elseif ( bp_is_register_page() ) {
     2903                $bp_title_parts = array( __( 'Create an Account', 'buddypress' ) );
     2904
     2905        // Activation page.
     2906        } elseif ( bp_is_activation_page() ) {
     2907                $bp_title_parts = array( __( 'Activate Your Account', 'buddypress' ) );
     2908
     2909        // Group creation page.
     2910        } elseif ( bp_is_group_create() ) {
     2911                $bp_title_parts = array( __( 'Create a Group', 'buddypress' ) );
     2912
     2913        // Blog creation page.
     2914        } elseif ( bp_is_create_blog() ) {
     2915                $bp_title_parts = array( __( 'Create a Site', 'buddypress' ) );
     2916        }
     2917
     2918        // Strip spans.
     2919        $bp_title_parts = array_map( '_bp_strip_spans_from_title', $bp_title_parts );
     2920
     2921        // Sep on right, so reverse the order.
     2922        if ( 'right' === $seplocation ) {
     2923                $bp_title_parts = array_reverse( $bp_title_parts );
     2924        }
     2925
     2926        /**
     2927         * Filter BuddyPress title parts before joining.
     2928         *
     2929         * @since 2.4.3
     2930         *
     2931         * @param  array $bp_title_parts Current BuddyPress title parts
     2932         * @return array
     2933         */
     2934        return (array) apply_filters( 'bp_get_title_parts', $bp_title_parts );
    27972935}
    27982936
Note: See TracChangeset for help on using the changeset viewer.