Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp_title() doc appears broken #1624

Open
smileBeda opened this issue Jul 3, 2024 · 5 comments
Open

wp_title() doc appears broken #1624

smileBeda opened this issue Jul 3, 2024 · 5 comments
Assignees
Labels
developer documentation (DevHub) Improvements or additions to developer documentation self-assigned [Status] Review Issue in review [Status] To do Issue marked as Todo

Comments

@smileBeda
Copy link

smileBeda commented Jul 3, 2024

Issue Description

This documentation appears broken. There should be at least some $args description, source code...

URL of the Page with the Issue

https://developer.wordpress.org/reference/functions/wp_title/

Why is this a problem?

It is unclear what that function even is, when there is no context.

Suggested Fix

  • I checked the code, the docbloc is there, technically this should be automatically generated by the parser... if that for some reason is not working, below attache the information to add
  • I believe there usually also is source code section
/**
 * Displays or retrieves page title for all areas of blog.
 *
 * By default, the page title will display the separator before the page title,
 * so that the blog title will be before the page title. This is not good for
 * title display, since the blog title shows up on most tabs and not what is
 * important, which is the page that the user is looking at.
 *
 * There are also SEO benefits to having the blog title after or to the 'right'
 * of the page title. However, it is mostly common sense to have the blog title
 * to the right with most browsers supporting tabs. You can achieve this by
 * using the seplocation parameter and setting the value to 'right'. This change
 * was introduced around 2.5.0, in case backward compatibility of themes is
 * important.
 *
 * @since 1.0.0
 *
 * @global WP_Locale $wp_locale WordPress date and time locale object.
 *
 * @param string $sep         Optional. How to separate the various items within the page title.
 *                            Default '»'.
 * @param bool   $display     Optional. Whether to display or retrieve title. Default true.
 * @param string $seplocation Optional. Location of the separator (either 'left' or 'right').
 * @return string|void String when `$display` is false, nothing otherwise.
 */

There also should be a bold notice stating that this function is not suggested anymore and instead, add_theme_support should be used.

@smileBeda smileBeda added the [Status] To do Issue marked as Todo label Jul 3, 2024
Copy link

github-actions bot commented Jul 3, 2024

Heads up @WordPress/docs-issues-coordinators, we have a new issue open. Time to use 'em labels.

@Leonardus-Nugraha Leonardus-Nugraha added the developer documentation (DevHub) Improvements or additions to developer documentation label Jul 16, 2024
@benazeer-ben
Copy link

/assign

Copy link

Hey @benazeer-ben, thanks for your interest in this issue! 🍪🍪🍪
If you have any questions, do not hesitate to ask them in our #docs Slack channel.
Enjoy and happy contributing ❤️

@benazeerhassan1909
Copy link

Hi Team,

Here is the updated documentation for wp_title()

wp_title()

Displays or retrieves the page title for all areas of a blog.

🔔 Deprecated: This function is deprecated as of WordPress 4.4. Instead, use add_theme_support( 'title-tag' ).

Description

The wp_title() function generates the title of a page or post for display in the browser tab or SEO purposes.

By default, the separator ($sep) is placed before the page title, meaning the blog title appears first. This can be changed using $seplocation to place the separator after the page title.

🚨 Important: Since WordPress 4.4, themes should use add_theme_support( 'title-tag' ) to allow WordPress to handle titles automatically.

Parameters

$sep
(string) (Optional) Separator between the title components.
Default: » (»)

$display
(bool) (Optional) Whether to echo (true) or return (false) the title.
Default: true

$seplocation
(string) (Optional) Position of the separator ('left' or 'right').
Default: '' (default behavior places separator before title)

Return

(string|void) Returns the title if $display is false, otherwise prints it directly.

Usage

Instead of wp_title(), use:

function mytheme_setup() {
    add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'mytheme_setup' );

This ensures WordPress dynamically manages the title.

Deprecated Approach (wp_title())

<title><?php wp_title('|', true, 'right'); ?></title>

Source code

/**
 * Displays or retrieves page title for all areas of blog.
 *
 * By default, the page title will display the separator before the page title,
 * so that the blog title will be before the page title. This is not good for
 * title display, since the blog title shows up on most tabs and not what is
 * important, which is the page that the user is looking at.
 *
 * There are also SEO benefits to having the blog title after or to the 'right'
 * of the page title. However, it is mostly common sense to have the blog title
 * to the right with most browsers supporting tabs. You can achieve this by
 * using the seplocation parameter and setting the value to 'right'. This change
 * was introduced around 2.5.0, in case backward compatibility of themes is
 * important.
 *
 * @since 1.0.0
 *
 * @global WP_Locale $wp_locale WordPress date and time locale object.
 *
 * @param string $sep         Optional. How to separate the various items within the page title.
 *                            Default '&raquo;'.
 * @param bool   $display     Optional. Whether to display or retrieve title. Default true.
 * @param string $seplocation Optional. Location of the separator (either 'left' or 'right').
 * @return string|void String when `$display` is false, nothing otherwise.
 */
function wp_title( $sep = '&raquo;', $display = true, $seplocation = '' ) {
	global $wp_locale;

	$m        = get_query_var( 'm' );
	$year     = get_query_var( 'year' );
	$monthnum = get_query_var( 'monthnum' );
	$day      = get_query_var( 'day' );
	$search   = get_query_var( 's' );
	$title    = '';

	$t_sep = '%WP_TITLE_SEP%'; // Temporary separator, for accurate flipping, if necessary.

	// If there is a post.
	if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
		$title = single_post_title( '', false );
	}

	// If there's a post type archive.
	if ( is_post_type_archive() ) {
		$post_type = get_query_var( 'post_type' );
		if ( is_array( $post_type ) ) {
			$post_type = reset( $post_type );
		}
		$post_type_object = get_post_type_object( $post_type );
		if ( ! $post_type_object->has_archive ) {
			$title = post_type_archive_title( '', false );
		}
	}

	// If there's a category or tag.
	if ( is_category() || is_tag() ) {
		$title = single_term_title( '', false );
	}

	// If there's a taxonomy.
	if ( is_tax() ) {
		$term = get_queried_object();
		if ( $term ) {
			$tax   = get_taxonomy( $term->taxonomy );
			$title = single_term_title( $tax->labels->name . $t_sep, false );
		}
	}

	// If there's an author.
	if ( is_author() && ! is_post_type_archive() ) {
		$author = get_queried_object();
		if ( $author ) {
			$title = $author->display_name;
		}
	}

	// Post type archives with has_archive should override terms.
	if ( is_post_type_archive() && $post_type_object->has_archive ) {
		$title = post_type_archive_title( '', false );
	}

	// If there's a month.
	if ( is_archive() && ! empty( $m ) ) {
		$my_year  = substr( $m, 0, 4 );
		$my_month = substr( $m, 4, 2 );
		$my_day   = (int) substr( $m, 6, 2 );
		$title    = $my_year .
			( $my_month ? $t_sep . $wp_locale->get_month( $my_month ) : '' ) .
			( $my_day ? $t_sep . $my_day : '' );
	}

	// If there's a year.
	if ( is_archive() && ! empty( $year ) ) {
		$title = $year;
		if ( ! empty( $monthnum ) ) {
			$title .= $t_sep . $wp_locale->get_month( $monthnum );
		}
		if ( ! empty( $day ) ) {
			$title .= $t_sep . zeroise( $day, 2 );
		}
	}

	// If it's a search.
	if ( is_search() ) {
		/* translators: 1: Separator, 2: Search query. */
		$title = sprintf( __( 'Search Results %1$s %2$s' ), $t_sep, strip_tags( $search ) );
	}

	// If it's a 404 page.
	if ( is_404() ) {
		$title = __( 'Page not found' );
	}

	$prefix = '';
	if ( ! empty( $title ) ) {
		$prefix = " $sep ";
	}

	/**
	 * Filters the parts of the page title.
	 *
	 * @since 4.0.0
	 *
	 * @param string[] $title_array Array of parts of the page title.
	 */
	$title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );

	// Determines position of the separator and direction of the breadcrumb.
	if ( 'right' === $seplocation ) { // Separator on right, so reverse the order.
		$title_array = array_reverse( $title_array );
		$title       = implode( " $sep ", $title_array ) . $prefix;
	} else {
		$title = $prefix . implode( " $sep ", $title_array );
	}

	/**
	 * Filters the text of the page title.
	 *
	 * @since 2.0.0
	 *
	 * @param string $title       Page title.
	 * @param string $sep         Title separator.
	 * @param string $seplocation Location of the separator (either 'left' or 'right').
	 */
	$title = apply_filters( 'wp_title', $title, $sep, $seplocation );

	// Send it out.
	if ( $display ) {
		echo $title;
	} else {
		return $title;
	}
}

Changelog

Deprecated in: 4.4
Introduced in: 1.0.0

Related

add_theme_support( 'title-tag' )
wp_get_document_title()

Copy link

Heads up @docs-reviewers - the "[Status] Review" label was applied to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer documentation (DevHub) Improvements or additions to developer documentation self-assigned [Status] Review Issue in review [Status] To do Issue marked as Todo
Projects
None yet
Development

No branches or pull requests

5 participants