antonanton antonanton - 1 month ago 21
PHP Question

Filter implode title and separator string wordpress

I would like to replace part of this string:

$title = implode( " $sep ", array_filter( $title ) );


Full function here:

function wp_get_document_title() {
$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
return $title;
}

global $page, $paged;

$title = array(
'title' => '',
);

if ( is_404() ) {
$title['title'] = __( 'Page not found' );
} elseif ( is_search() ) {
$title['title'] = sprintf( __( 'Search Results for “%s”' ), get_search_query() );
} elseif ( is_front_page() ) {
$title['title'] = get_bloginfo( 'name', 'display' );
} elseif ( is_post_type_archive() ) {
$title['title'] = post_type_archive_title( '', false );
} elseif ( is_tax() ) {
$title['title'] = single_term_title( '', false );
} elseif ( is_home() || is_singular() ) {
$title['title'] = single_post_title( '', false );
} elseif ( is_category() || is_tag() ) {
$title['title'] = single_term_title( '', false );
} elseif ( is_author() && $author = get_queried_object() ) {
$title['title'] = $author->display_name;
} elseif ( is_year() ) {
$title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );
} elseif ( is_month() ) {
$title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) );

} elseif ( is_day() ) {
$title['title'] = get_the_date();
}

if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
$title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
}

if ( is_front_page() ) {
$title['tagline'] = get_bloginfo( 'description', 'display' );
} else {
$title['site'] = get_bloginfo( 'name', 'display' );
}

$sep = apply_filters( 'document_title_separator', '-' );

$title = apply_filters( 'document_title_parts', $title );

$title = implode( " $sep ", array_filter( $title ) );
$title = wptexturize( $title );
$title = convert_chars( $title );
$title = esc_html( $title );
$title = capital_P_dangit( $title );

return $title;
}

function _wp_render_title_tag() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}


However, this is under the
wp-includes/general-template.php
and I would like to filter this through my
functions.php
so as not to affect the core file.

Is there a way I could filter out the spaces before and after the
$sep
function? Essentially I want the Title to change for SEO reasons and only want the separator gone on the front page.. something along these lines (this does not work of course):

<?php if ( is_front_page() ) {
$title = implode( "", array_filter( $title ) );
} else {
$title = implode( " $sep ", array_filter( $title ) );
} ?>


Is there a way to filter that or use
str_replace
?

Answer

Try the document_title_separator filter and pre_get_document_title in WordPress 4.4+.

Setting the separator to something like $% then using it in conjunction with a preg_replace. You can then match the pattern with the added white spaces for example '/ \$% /' against the whole title.

In functions.php:

<?php
add_filter('pre_get_document_title', 'theme_mod_title');

function theme_mod_title() {

    add_filter('document_title_separator', function() {
        return '$%';
    });

    remove_filter('pre_get_document_title', 'theme_mod_title');

    $pattern = '/ \$% /';
    $desired = '-';
    $title = wp_get_document_title();

    return preg_replace($pattern, $desired, $title);
}