PageRenderTime 54ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-includes/general-template.php

https://github.com/markjaquith/WordPress
PHP | 4920 lines | 2340 code | 456 blank | 2124 comment | 391 complexity | 5e0b9f128daa38abbd91cd422109d0b2 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * General template tags that can go anywhere in a template.
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. */
  8. /**
  9. * Load header template.
  10. *
  11. * Includes the header template for a theme or if a name is specified then a
  12. * specialised header will be included.
  13. *
  14. * For the parameter, if the file is called "header-special.php" then specify
  15. * "special".
  16. *
  17. * @since 1.5.0
  18. * @since 5.5.0 A return value was added.
  19. * @since 5.5.0 The `$args` parameter was added.
  20. *
  21. * @param string $name The name of the specialised header.
  22. * @param array $args Optional. Additional arguments passed to the header template.
  23. * Default empty array.
  24. * @return void|false Void on success, false if the template does not exist.
  25. */
  26. function get_header( $name = null, $args = array() ) {
  27. /**
  28. * Fires before the header template file is loaded.
  29. *
  30. * @since 2.1.0
  31. * @since 2.8.0 The `$name` parameter was added.
  32. * @since 5.5.0 The `$args` parameter was added.
  33. *
  34. * @param string|null $name Name of the specific header file to use. Null for the default header.
  35. * @param array $args Additional arguments passed to the header template.
  36. */
  37. do_action( 'get_header', $name, $args );
  38. $templates = array();
  39. $name = (string) $name;
  40. if ( '' !== $name ) {
  41. $templates[] = "header-{$name}.php";
  42. }
  43. $templates[] = 'header.php';
  44. if ( ! locate_template( $templates, true, true, $args ) ) {
  45. return false;
  46. }
  47. }
  48. /**
  49. * Load footer template.
  50. *
  51. * Includes the footer template for a theme or if a name is specified then a
  52. * specialised footer will be included.
  53. *
  54. * For the parameter, if the file is called "footer-special.php" then specify
  55. * "special".
  56. *
  57. * @since 1.5.0
  58. * @since 5.5.0 A return value was added.
  59. * @since 5.5.0 The `$args` parameter was added.
  60. *
  61. * @param string $name The name of the specialised footer.
  62. * @param array $args Optional. Additional arguments passed to the footer template.
  63. * Default empty array.
  64. * @return void|false Void on success, false if the template does not exist.
  65. */
  66. function get_footer( $name = null, $args = array() ) {
  67. /**
  68. * Fires before the footer template file is loaded.
  69. *
  70. * @since 2.1.0
  71. * @since 2.8.0 The `$name` parameter was added.
  72. * @since 5.5.0 The `$args` parameter was added.
  73. *
  74. * @param string|null $name Name of the specific footer file to use. Null for the default footer.
  75. * @param array $args Additional arguments passed to the footer template.
  76. */
  77. do_action( 'get_footer', $name, $args );
  78. $templates = array();
  79. $name = (string) $name;
  80. if ( '' !== $name ) {
  81. $templates[] = "footer-{$name}.php";
  82. }
  83. $templates[] = 'footer.php';
  84. if ( ! locate_template( $templates, true, true, $args ) ) {
  85. return false;
  86. }
  87. }
  88. /**
  89. * Load sidebar template.
  90. *
  91. * Includes the sidebar template for a theme or if a name is specified then a
  92. * specialised sidebar will be included.
  93. *
  94. * For the parameter, if the file is called "sidebar-special.php" then specify
  95. * "special".
  96. *
  97. * @since 1.5.0
  98. * @since 5.5.0 A return value was added.
  99. * @since 5.5.0 The `$args` parameter was added.
  100. *
  101. * @param string $name The name of the specialised sidebar.
  102. * @param array $args Optional. Additional arguments passed to the sidebar template.
  103. * Default empty array.
  104. * @return void|false Void on success, false if the template does not exist.
  105. */
  106. function get_sidebar( $name = null, $args = array() ) {
  107. /**
  108. * Fires before the sidebar template file is loaded.
  109. *
  110. * @since 2.2.0
  111. * @since 2.8.0 The `$name` parameter was added.
  112. * @since 5.5.0 The `$args` parameter was added.
  113. *
  114. * @param string|null $name Name of the specific sidebar file to use. Null for the default sidebar.
  115. * @param array $args Additional arguments passed to the sidebar template.
  116. */
  117. do_action( 'get_sidebar', $name, $args );
  118. $templates = array();
  119. $name = (string) $name;
  120. if ( '' !== $name ) {
  121. $templates[] = "sidebar-{$name}.php";
  122. }
  123. $templates[] = 'sidebar.php';
  124. if ( ! locate_template( $templates, true, true, $args ) ) {
  125. return false;
  126. }
  127. }
  128. /**
  129. * Loads a template part into a template.
  130. *
  131. * Provides a simple mechanism for child themes to overload reusable sections of code
  132. * in the theme.
  133. *
  134. * Includes the named template part for a theme or if a name is specified then a
  135. * specialised part will be included. If the theme contains no {slug}.php file
  136. * then no template will be included.
  137. *
  138. * The template is included using require, not require_once, so you may include the
  139. * same template part multiple times.
  140. *
  141. * For the $name parameter, if the file is called "{slug}-special.php" then specify
  142. * "special".
  143. *
  144. * @since 3.0.0
  145. * @since 5.5.0 A return value was added.
  146. * @since 5.5.0 The `$args` parameter was added.
  147. *
  148. * @param string $slug The slug name for the generic template.
  149. * @param string $name The name of the specialised template.
  150. * @param array $args Optional. Additional arguments passed to the template.
  151. * Default empty array.
  152. * @return void|false Void on success, false if the template does not exist.
  153. */
  154. function get_template_part( $slug, $name = null, $args = array() ) {
  155. /**
  156. * Fires before the specified template part file is loaded.
  157. *
  158. * The dynamic portion of the hook name, `$slug`, refers to the slug name
  159. * for the generic template part.
  160. *
  161. * @since 3.0.0
  162. * @since 5.5.0 The `$args` parameter was added.
  163. *
  164. * @param string $slug The slug name for the generic template.
  165. * @param string|null $name The name of the specialized template.
  166. * @param array $args Additional arguments passed to the template.
  167. */
  168. do_action( "get_template_part_{$slug}", $slug, $name, $args );
  169. $templates = array();
  170. $name = (string) $name;
  171. if ( '' !== $name ) {
  172. $templates[] = "{$slug}-{$name}.php";
  173. }
  174. $templates[] = "{$slug}.php";
  175. /**
  176. * Fires before an attempt is made to locate and load a template part.
  177. *
  178. * @since 5.2.0
  179. * @since 5.5.0 The `$args` parameter was added.
  180. *
  181. * @param string $slug The slug name for the generic template.
  182. * @param string $name The name of the specialized template.
  183. * @param string[] $templates Array of template files to search for, in order.
  184. * @param array $args Additional arguments passed to the template.
  185. */
  186. do_action( 'get_template_part', $slug, $name, $templates, $args );
  187. if ( ! locate_template( $templates, true, false, $args ) ) {
  188. return false;
  189. }
  190. }
  191. /**
  192. * Display search form.
  193. *
  194. * Will first attempt to locate the searchform.php file in either the child or
  195. * the parent, then load it. If it doesn't exist, then the default search form
  196. * will be displayed. The default search form is HTML, which will be displayed.
  197. * There is a filter applied to the search form HTML in order to edit or replace
  198. * it. The filter is {@see 'get_search_form'}.
  199. *
  200. * This function is primarily used by themes which want to hardcode the search
  201. * form into the sidebar and also by the search widget in WordPress.
  202. *
  203. * There is also an action that is called whenever the function is run called,
  204. * {@see 'pre_get_search_form'}. This can be useful for outputting JavaScript that the
  205. * search relies on or various formatting that applies to the beginning of the
  206. * search. To give a few examples of what it can be used for.
  207. *
  208. * @since 2.7.0
  209. * @since 5.2.0 The `$args` array parameter was added in place of an `$echo` boolean flag.
  210. *
  211. * @param array $args {
  212. * Optional. Array of display arguments.
  213. *
  214. * @type bool $echo Whether to echo or return the form. Default true.
  215. * @type string $aria_label ARIA label for the search form. Useful to distinguish
  216. * multiple search forms on the same page and improve
  217. * accessibility. Default empty.
  218. * }
  219. * @return void|string Void if 'echo' argument is true, search form HTML if 'echo' is false.
  220. */
  221. function get_search_form( $args = array() ) {
  222. /**
  223. * Fires before the search form is retrieved, at the start of get_search_form().
  224. *
  225. * @since 2.7.0 as 'get_search_form' action.
  226. * @since 3.6.0
  227. * @since 5.5.0 The `$args` parameter was added.
  228. *
  229. * @link https://core.trac.wordpress.org/ticket/19321
  230. *
  231. * @param array $args The array of arguments for building the search form.
  232. * See get_search_form() for information on accepted arguments.
  233. */
  234. do_action( 'pre_get_search_form', $args );
  235. $echo = true;
  236. if ( ! is_array( $args ) ) {
  237. /*
  238. * Back compat: to ensure previous uses of get_search_form() continue to
  239. * function as expected, we handle a value for the boolean $echo param removed
  240. * in 5.2.0. Then we deal with the $args array and cast its defaults.
  241. */
  242. $echo = (bool) $args;
  243. // Set an empty array and allow default arguments to take over.
  244. $args = array();
  245. }
  246. // Defaults are to echo and to output no custom label on the form.
  247. $defaults = array(
  248. 'echo' => $echo,
  249. 'aria_label' => '',
  250. );
  251. $args = wp_parse_args( $args, $defaults );
  252. /**
  253. * Filters the array of arguments used when generating the search form.
  254. *
  255. * @since 5.2.0
  256. *
  257. * @param array $args The array of arguments for building the search form.
  258. * See get_search_form() for information on accepted arguments.
  259. */
  260. $args = apply_filters( 'search_form_args', $args );
  261. // Ensure that the filtered arguments contain all required default values.
  262. $args = array_merge( $defaults, $args );
  263. $format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';
  264. /**
  265. * Filters the HTML format of the search form.
  266. *
  267. * @since 3.6.0
  268. * @since 5.5.0 The `$args` parameter was added.
  269. *
  270. * @param string $format The type of markup to use in the search form.
  271. * Accepts 'html5', 'xhtml'.
  272. * @param array $args The array of arguments for building the search form.
  273. * See get_search_form() for information on accepted arguments.
  274. */
  275. $format = apply_filters( 'search_form_format', $format, $args );
  276. $search_form_template = locate_template( 'searchform.php' );
  277. if ( '' !== $search_form_template ) {
  278. ob_start();
  279. require $search_form_template;
  280. $form = ob_get_clean();
  281. } else {
  282. // Build a string containing an aria-label to use for the search form.
  283. if ( $args['aria_label'] ) {
  284. $aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ';
  285. } else {
  286. /*
  287. * If there's no custom aria-label, we can set a default here. At the
  288. * moment it's empty as there's uncertainty about what the default should be.
  289. */
  290. $aria_label = '';
  291. }
  292. if ( 'html5' === $format ) {
  293. $form = '<form role="search" ' . $aria_label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
  294. <label>
  295. <span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
  296. <input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
  297. </label>
  298. <input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
  299. </form>';
  300. } else {
  301. $form = '<form role="search" ' . $aria_label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
  302. <div>
  303. <label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
  304. <input type="text" value="' . get_search_query() . '" name="s" id="s" />
  305. <input type="submit" id="searchsubmit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
  306. </div>
  307. </form>';
  308. }
  309. }
  310. /**
  311. * Filters the HTML output of the search form.
  312. *
  313. * @since 2.7.0
  314. * @since 5.5.0 The `$args` parameter was added.
  315. *
  316. * @param string $form The search form HTML output.
  317. * @param array $args The array of arguments for building the search form.
  318. * See get_search_form() for information on accepted arguments.
  319. */
  320. $result = apply_filters( 'get_search_form', $form, $args );
  321. if ( null === $result ) {
  322. $result = $form;
  323. }
  324. if ( $args['echo'] ) {
  325. echo $result;
  326. } else {
  327. return $result;
  328. }
  329. }
  330. /**
  331. * Display the Log In/Out link.
  332. *
  333. * Displays a link, which allows users to navigate to the Log In page to log in
  334. * or log out depending on whether they are currently logged in.
  335. *
  336. * @since 1.5.0
  337. *
  338. * @param string $redirect Optional path to redirect to on login/logout.
  339. * @param bool $echo Default to echo and not return the link.
  340. * @return void|string Void if `$echo` argument is true, log in/out link if `$echo` is false.
  341. */
  342. function wp_loginout( $redirect = '', $echo = true ) {
  343. if ( ! is_user_logged_in() ) {
  344. $link = '<a href="' . esc_url( wp_login_url( $redirect ) ) . '">' . __( 'Log in' ) . '</a>';
  345. } else {
  346. $link = '<a href="' . esc_url( wp_logout_url( $redirect ) ) . '">' . __( 'Log out' ) . '</a>';
  347. }
  348. if ( $echo ) {
  349. /**
  350. * Filters the HTML output for the Log In/Log Out link.
  351. *
  352. * @since 1.5.0
  353. *
  354. * @param string $link The HTML link content.
  355. */
  356. echo apply_filters( 'loginout', $link );
  357. } else {
  358. /** This filter is documented in wp-includes/general-template.php */
  359. return apply_filters( 'loginout', $link );
  360. }
  361. }
  362. /**
  363. * Retrieves the logout URL.
  364. *
  365. * Returns the URL that allows the user to log out of the site.
  366. *
  367. * @since 2.7.0
  368. *
  369. * @param string $redirect Path to redirect to on logout.
  370. * @return string The logout URL. Note: HTML-encoded via esc_html() in wp_nonce_url().
  371. */
  372. function wp_logout_url( $redirect = '' ) {
  373. $args = array();
  374. if ( ! empty( $redirect ) ) {
  375. $args['redirect_to'] = urlencode( $redirect );
  376. }
  377. $logout_url = add_query_arg( $args, site_url( 'wp-login.php?action=logout', 'login' ) );
  378. $logout_url = wp_nonce_url( $logout_url, 'log-out' );
  379. /**
  380. * Filters the logout URL.
  381. *
  382. * @since 2.8.0
  383. *
  384. * @param string $logout_url The HTML-encoded logout URL.
  385. * @param string $redirect Path to redirect to on logout.
  386. */
  387. return apply_filters( 'logout_url', $logout_url, $redirect );
  388. }
  389. /**
  390. * Retrieves the login URL.
  391. *
  392. * @since 2.7.0
  393. *
  394. * @param string $redirect Path to redirect to on log in.
  395. * @param bool $force_reauth Whether to force reauthorization, even if a cookie is present.
  396. * Default false.
  397. * @return string The login URL. Not HTML-encoded.
  398. */
  399. function wp_login_url( $redirect = '', $force_reauth = false ) {
  400. $login_url = site_url( 'wp-login.php', 'login' );
  401. if ( ! empty( $redirect ) ) {
  402. $login_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $login_url );
  403. }
  404. if ( $force_reauth ) {
  405. $login_url = add_query_arg( 'reauth', '1', $login_url );
  406. }
  407. /**
  408. * Filters the login URL.
  409. *
  410. * @since 2.8.0
  411. * @since 4.2.0 The `$force_reauth` parameter was added.
  412. *
  413. * @param string $login_url The login URL. Not HTML-encoded.
  414. * @param string $redirect The path to redirect to on login, if supplied.
  415. * @param bool $force_reauth Whether to force reauthorization, even if a cookie is present.
  416. */
  417. return apply_filters( 'login_url', $login_url, $redirect, $force_reauth );
  418. }
  419. /**
  420. * Returns the URL that allows the user to register on the site.
  421. *
  422. * @since 3.6.0
  423. *
  424. * @return string User registration URL.
  425. */
  426. function wp_registration_url() {
  427. /**
  428. * Filters the user registration URL.
  429. *
  430. * @since 3.6.0
  431. *
  432. * @param string $register The user registration URL.
  433. */
  434. return apply_filters( 'register_url', site_url( 'wp-login.php?action=register', 'login' ) );
  435. }
  436. /**
  437. * Provides a simple login form for use anywhere within WordPress.
  438. *
  439. * The login form HTML is echoed by default. Pass a false value for `$echo` to return it instead.
  440. *
  441. * @since 3.0.0
  442. *
  443. * @param array $args {
  444. * Optional. Array of options to control the form output. Default empty array.
  445. *
  446. * @type bool $echo Whether to display the login form or return the form HTML code.
  447. * Default true (echo).
  448. * @type string $redirect URL to redirect to. Must be absolute, as in "https://example.com/mypage/".
  449. * Default is to redirect back to the request URI.
  450. * @type string $form_id ID attribute value for the form. Default 'loginform'.
  451. * @type string $label_username Label for the username or email address field. Default 'Username or Email Address'.
  452. * @type string $label_password Label for the password field. Default 'Password'.
  453. * @type string $label_remember Label for the remember field. Default 'Remember Me'.
  454. * @type string $label_log_in Label for the submit button. Default 'Log In'.
  455. * @type string $id_username ID attribute value for the username field. Default 'user_login'.
  456. * @type string $id_password ID attribute value for the password field. Default 'user_pass'.
  457. * @type string $id_remember ID attribute value for the remember field. Default 'rememberme'.
  458. * @type string $id_submit ID attribute value for the submit button. Default 'wp-submit'.
  459. * @type bool $remember Whether to display the "rememberme" checkbox in the form.
  460. * @type string $value_username Default value for the username field. Default empty.
  461. * @type bool $value_remember Whether the "Remember Me" checkbox should be checked by default.
  462. * Default false (unchecked).
  463. *
  464. * }
  465. * @return void|string Void if 'echo' argument is true, login form HTML if 'echo' is false.
  466. */
  467. function wp_login_form( $args = array() ) {
  468. $defaults = array(
  469. 'echo' => true,
  470. // Default 'redirect' value takes the user back to the request URI.
  471. 'redirect' => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
  472. 'form_id' => 'loginform',
  473. 'label_username' => __( 'Username or Email Address' ),
  474. 'label_password' => __( 'Password' ),
  475. 'label_remember' => __( 'Remember Me' ),
  476. 'label_log_in' => __( 'Log In' ),
  477. 'id_username' => 'user_login',
  478. 'id_password' => 'user_pass',
  479. 'id_remember' => 'rememberme',
  480. 'id_submit' => 'wp-submit',
  481. 'remember' => true,
  482. 'value_username' => '',
  483. // Set 'value_remember' to true to default the "Remember me" checkbox to checked.
  484. 'value_remember' => false,
  485. );
  486. /**
  487. * Filters the default login form output arguments.
  488. *
  489. * @since 3.0.0
  490. *
  491. * @see wp_login_form()
  492. *
  493. * @param array $defaults An array of default login form arguments.
  494. */
  495. $args = wp_parse_args( $args, apply_filters( 'login_form_defaults', $defaults ) );
  496. /**
  497. * Filters content to display at the top of the login form.
  498. *
  499. * The filter evaluates just following the opening form tag element.
  500. *
  501. * @since 3.0.0
  502. *
  503. * @param string $content Content to display. Default empty.
  504. * @param array $args Array of login form arguments.
  505. */
  506. $login_form_top = apply_filters( 'login_form_top', '', $args );
  507. /**
  508. * Filters content to display in the middle of the login form.
  509. *
  510. * The filter evaluates just following the location where the 'login-password'
  511. * field is displayed.
  512. *
  513. * @since 3.0.0
  514. *
  515. * @param string $content Content to display. Default empty.
  516. * @param array $args Array of login form arguments.
  517. */
  518. $login_form_middle = apply_filters( 'login_form_middle', '', $args );
  519. /**
  520. * Filters content to display at the bottom of the login form.
  521. *
  522. * The filter evaluates just preceding the closing form tag element.
  523. *
  524. * @since 3.0.0
  525. *
  526. * @param string $content Content to display. Default empty.
  527. * @param array $args Array of login form arguments.
  528. */
  529. $login_form_bottom = apply_filters( 'login_form_bottom', '', $args );
  530. $form =
  531. sprintf(
  532. '<form name="%1$s" id="%1$s" action="%2$s" method="post">',
  533. esc_attr( $args['form_id'] ),
  534. esc_url( site_url( 'wp-login.php', 'login_post' ) )
  535. ) .
  536. $login_form_top .
  537. sprintf(
  538. '<p class="login-username">
  539. <label for="%1$s">%2$s</label>
  540. <input type="text" name="log" id="%1$s" class="input" value="%3$s" size="20" />
  541. </p>',
  542. esc_attr( $args['id_username'] ),
  543. esc_html( $args['label_username'] ),
  544. esc_attr( $args['value_username'] )
  545. ) .
  546. sprintf(
  547. '<p class="login-password">
  548. <label for="%1$s">%2$s</label>
  549. <input type="password" name="pwd" id="%1$s" class="input" value="" size="20" />
  550. </p>',
  551. esc_attr( $args['id_password'] ),
  552. esc_html( $args['label_password'] )
  553. ) .
  554. $login_form_middle .
  555. ( $args['remember'] ?
  556. sprintf(
  557. '<p class="login-remember"><label><input name="rememberme" type="checkbox" id="%1$s" value="forever"%2$s /> %3$s</label></p>',
  558. esc_attr( $args['id_remember'] ),
  559. ( $args['value_remember'] ? ' checked="checked"' : '' ),
  560. esc_html( $args['label_remember'] )
  561. ) : ''
  562. ) .
  563. sprintf(
  564. '<p class="login-submit">
  565. <input type="submit" name="wp-submit" id="%1$s" class="button button-primary" value="%2$s" />
  566. <input type="hidden" name="redirect_to" value="%3$s" />
  567. </p>',
  568. esc_attr( $args['id_submit'] ),
  569. esc_attr( $args['label_log_in'] ),
  570. esc_url( $args['redirect'] )
  571. ) .
  572. $login_form_bottom .
  573. '</form>';
  574. if ( $args['echo'] ) {
  575. echo $form;
  576. } else {
  577. return $form;
  578. }
  579. }
  580. /**
  581. * Returns the URL that allows the user to retrieve the lost password
  582. *
  583. * @since 2.8.0
  584. *
  585. * @param string $redirect Path to redirect to on login.
  586. * @return string Lost password URL.
  587. */
  588. function wp_lostpassword_url( $redirect = '' ) {
  589. $args = array(
  590. 'action' => 'lostpassword',
  591. );
  592. if ( ! empty( $redirect ) ) {
  593. $args['redirect_to'] = urlencode( $redirect );
  594. }
  595. if ( is_multisite() ) {
  596. $blog_details = get_blog_details();
  597. $wp_login_path = $blog_details->path . 'wp-login.php';
  598. } else {
  599. $wp_login_path = 'wp-login.php';
  600. }
  601. $lostpassword_url = add_query_arg( $args, network_site_url( $wp_login_path, 'login' ) );
  602. /**
  603. * Filters the Lost Password URL.
  604. *
  605. * @since 2.8.0
  606. *
  607. * @param string $lostpassword_url The lost password page URL.
  608. * @param string $redirect The path to redirect to on login.
  609. */
  610. return apply_filters( 'lostpassword_url', $lostpassword_url, $redirect );
  611. }
  612. /**
  613. * Display the Registration or Admin link.
  614. *
  615. * Display a link which allows the user to navigate to the registration page if
  616. * not logged in and registration is enabled or to the dashboard if logged in.
  617. *
  618. * @since 1.5.0
  619. *
  620. * @param string $before Text to output before the link. Default `<li>`.
  621. * @param string $after Text to output after the link. Default `</li>`.
  622. * @param bool $echo Default to echo and not return the link.
  623. * @return void|string Void if `$echo` argument is true, registration or admin link
  624. * if `$echo` is false.
  625. */
  626. function wp_register( $before = '<li>', $after = '</li>', $echo = true ) {
  627. if ( ! is_user_logged_in() ) {
  628. if ( get_option( 'users_can_register' ) ) {
  629. $link = $before . '<a href="' . esc_url( wp_registration_url() ) . '">' . __( 'Register' ) . '</a>' . $after;
  630. } else {
  631. $link = '';
  632. }
  633. } elseif ( current_user_can( 'read' ) ) {
  634. $link = $before . '<a href="' . admin_url() . '">' . __( 'Site Admin' ) . '</a>' . $after;
  635. } else {
  636. $link = '';
  637. }
  638. /**
  639. * Filters the HTML link to the Registration or Admin page.
  640. *
  641. * Users are sent to the admin page if logged-in, or the registration page
  642. * if enabled and logged-out.
  643. *
  644. * @since 1.5.0
  645. *
  646. * @param string $link The HTML code for the link to the Registration or Admin page.
  647. */
  648. $link = apply_filters( 'register', $link );
  649. if ( $echo ) {
  650. echo $link;
  651. } else {
  652. return $link;
  653. }
  654. }
  655. /**
  656. * Theme container function for the 'wp_meta' action.
  657. *
  658. * The {@see 'wp_meta'} action can have several purposes, depending on how you use it,
  659. * but one purpose might have been to allow for theme switching.
  660. *
  661. * @since 1.5.0
  662. *
  663. * @link https://core.trac.wordpress.org/ticket/1458 Explanation of 'wp_meta' action.
  664. */
  665. function wp_meta() {
  666. /**
  667. * Fires before displaying echoed content in the sidebar.
  668. *
  669. * @since 1.5.0
  670. */
  671. do_action( 'wp_meta' );
  672. }
  673. /**
  674. * Displays information about the current site.
  675. *
  676. * @since 0.71
  677. *
  678. * @see get_bloginfo() For possible `$show` values
  679. *
  680. * @param string $show Optional. Site information to display. Default empty.
  681. */
  682. function bloginfo( $show = '' ) {
  683. echo get_bloginfo( $show, 'display' );
  684. }
  685. /**
  686. * Retrieves information about the current site.
  687. *
  688. * Possible values for `$show` include:
  689. *
  690. * - 'name' - Site title (set in Settings > General)
  691. * - 'description' - Site tagline (set in Settings > General)
  692. * - 'wpurl' - The WordPress address (URL) (set in Settings > General)
  693. * - 'url' - The Site address (URL) (set in Settings > General)
  694. * - 'admin_email' - Admin email (set in Settings > General)
  695. * - 'charset' - The "Encoding for pages and feeds" (set in Settings > Reading)
  696. * - 'version' - The current WordPress version
  697. * - 'html_type' - The content-type (default: "text/html"). Themes and plugins
  698. * can override the default value using the {@see 'pre_option_html_type'} filter
  699. * - 'text_direction' - The text direction determined by the site's language. is_rtl()
  700. * should be used instead
  701. * - 'language' - Language code for the current site
  702. * - 'stylesheet_url' - URL to the stylesheet for the active theme. An active child theme
  703. * will take precedence over this value
  704. * - 'stylesheet_directory' - Directory path for the active theme. An active child theme
  705. * will take precedence over this value
  706. * - 'template_url' / 'template_directory' - URL of the active theme's directory. An active
  707. * child theme will NOT take precedence over this value
  708. * - 'pingback_url' - The pingback XML-RPC file URL (xmlrpc.php)
  709. * - 'atom_url' - The Atom feed URL (/feed/atom)
  710. * - 'rdf_url' - The RDF/RSS 1.0 feed URL (/feed/rdf)
  711. * - 'rss_url' - The RSS 0.92 feed URL (/feed/rss)
  712. * - 'rss2_url' - The RSS 2.0 feed URL (/feed)
  713. * - 'comments_atom_url' - The comments Atom feed URL (/comments/feed)
  714. * - 'comments_rss2_url' - The comments RSS 2.0 feed URL (/comments/feed)
  715. *
  716. * Some `$show` values are deprecated and will be removed in future versions.
  717. * These options will trigger the _deprecated_argument() function.
  718. *
  719. * Deprecated arguments include:
  720. *
  721. * - 'siteurl' - Use 'url' instead
  722. * - 'home' - Use 'url' instead
  723. *
  724. * @since 0.71
  725. *
  726. * @global string $wp_version The WordPress version string.
  727. *
  728. * @param string $show Optional. Site info to retrieve. Default empty (site name).
  729. * @param string $filter Optional. How to filter what is retrieved. Default 'raw'.
  730. * @return string Mostly string values, might be empty.
  731. */
  732. function get_bloginfo( $show = '', $filter = 'raw' ) {
  733. switch ( $show ) {
  734. case 'home': // Deprecated.
  735. case 'siteurl': // Deprecated.
  736. _deprecated_argument(
  737. __FUNCTION__,
  738. '2.2.0',
  739. sprintf(
  740. /* translators: 1: 'siteurl'/'home' argument, 2: bloginfo() function name, 3: 'url' argument. */
  741. __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s option instead.' ),
  742. '<code>' . $show . '</code>',
  743. '<code>bloginfo()</code>',
  744. '<code>url</code>'
  745. )
  746. );
  747. // Intentional fall-through to be handled by the 'url' case.
  748. case 'url':
  749. $output = home_url();
  750. break;
  751. case 'wpurl':
  752. $output = site_url();
  753. break;
  754. case 'description':
  755. $output = get_option( 'blogdescription' );
  756. break;
  757. case 'rdf_url':
  758. $output = get_feed_link( 'rdf' );
  759. break;
  760. case 'rss_url':
  761. $output = get_feed_link( 'rss' );
  762. break;
  763. case 'rss2_url':
  764. $output = get_feed_link( 'rss2' );
  765. break;
  766. case 'atom_url':
  767. $output = get_feed_link( 'atom' );
  768. break;
  769. case 'comments_atom_url':
  770. $output = get_feed_link( 'comments_atom' );
  771. break;
  772. case 'comments_rss2_url':
  773. $output = get_feed_link( 'comments_rss2' );
  774. break;
  775. case 'pingback_url':
  776. $output = site_url( 'xmlrpc.php' );
  777. break;
  778. case 'stylesheet_url':
  779. $output = get_stylesheet_uri();
  780. break;
  781. case 'stylesheet_directory':
  782. $output = get_stylesheet_directory_uri();
  783. break;
  784. case 'template_directory':
  785. case 'template_url':
  786. $output = get_template_directory_uri();
  787. break;
  788. case 'admin_email':
  789. $output = get_option( 'admin_email' );
  790. break;
  791. case 'charset':
  792. $output = get_option( 'blog_charset' );
  793. if ( '' === $output ) {
  794. $output = 'UTF-8';
  795. }
  796. break;
  797. case 'html_type':
  798. $output = get_option( 'html_type' );
  799. break;
  800. case 'version':
  801. global $wp_version;
  802. $output = $wp_version;
  803. break;
  804. case 'language':
  805. /*
  806. * translators: Translate this to the correct language tag for your locale,
  807. * see https://www.w3.org/International/articles/language-tags/ for reference.
  808. * Do not translate into your own language.
  809. */
  810. $output = __( 'html_lang_attribute' );
  811. if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) {
  812. $output = determine_locale();
  813. $output = str_replace( '_', '-', $output );
  814. }
  815. break;
  816. case 'text_direction':
  817. _deprecated_argument(
  818. __FUNCTION__,
  819. '2.2.0',
  820. sprintf(
  821. /* translators: 1: 'text_direction' argument, 2: bloginfo() function name, 3: is_rtl() function name. */
  822. __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s function instead.' ),
  823. '<code>' . $show . '</code>',
  824. '<code>bloginfo()</code>',
  825. '<code>is_rtl()</code>'
  826. )
  827. );
  828. if ( function_exists( 'is_rtl' ) ) {
  829. $output = is_rtl() ? 'rtl' : 'ltr';
  830. } else {
  831. $output = 'ltr';
  832. }
  833. break;
  834. case 'name':
  835. default:
  836. $output = get_option( 'blogname' );
  837. break;
  838. }
  839. $url = true;
  840. if ( strpos( $show, 'url' ) === false &&
  841. strpos( $show, 'directory' ) === false &&
  842. strpos( $show, 'home' ) === false ) {
  843. $url = false;
  844. }
  845. if ( 'display' === $filter ) {
  846. if ( $url ) {
  847. /**
  848. * Filters the URL returned by get_bloginfo().
  849. *
  850. * @since 2.0.5
  851. *
  852. * @param string $output The URL returned by bloginfo().
  853. * @param string $show Type of information requested.
  854. */
  855. $output = apply_filters( 'bloginfo_url', $output, $show );
  856. } else {
  857. /**
  858. * Filters the site information returned by get_bloginfo().
  859. *
  860. * @since 0.71
  861. *
  862. * @param mixed $output The requested non-URL site information.
  863. * @param string $show Type of information requested.
  864. */
  865. $output = apply_filters( 'bloginfo', $output, $show );
  866. }
  867. }
  868. return $output;
  869. }
  870. /**
  871. * Returns the Site Icon URL.
  872. *
  873. * @since 4.3.0
  874. *
  875. * @param int $size Optional. Size of the site icon. Default 512 (pixels).
  876. * @param string $url Optional. Fallback url if no site icon is found. Default empty.
  877. * @param int $blog_id Optional. ID of the blog to get the site icon for. Default current blog.
  878. * @return string Site Icon URL.
  879. */
  880. function get_site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
  881. $switched_blog = false;
  882. if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
  883. switch_to_blog( $blog_id );
  884. $switched_blog = true;
  885. }
  886. $site_icon_id = get_option( 'site_icon' );
  887. if ( $site_icon_id ) {
  888. if ( $size >= 512 ) {
  889. $size_data = 'full';
  890. } else {
  891. $size_data = array( $size, $size );
  892. }
  893. $url = wp_get_attachment_image_url( $site_icon_id, $size_data );
  894. }
  895. if ( $switched_blog ) {
  896. restore_current_blog();
  897. }
  898. /**
  899. * Filters the site icon URL.
  900. *
  901. * @since 4.4.0
  902. *
  903. * @param string $url Site icon URL.
  904. * @param int $size Size of the site icon.
  905. * @param int $blog_id ID of the blog to get the site icon for.
  906. */
  907. return apply_filters( 'get_site_icon_url', $url, $size, $blog_id );
  908. }
  909. /**
  910. * Displays the Site Icon URL.
  911. *
  912. * @since 4.3.0
  913. *
  914. * @param int $size Optional. Size of the site icon. Default 512 (pixels).
  915. * @param string $url Optional. Fallback url if no site icon is found. Default empty.
  916. * @param int $blog_id Optional. ID of the blog to get the site icon for. Default current blog.
  917. */
  918. function site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
  919. echo esc_url( get_site_icon_url( $size, $url, $blog_id ) );
  920. }
  921. /**
  922. * Whether the site has a Site Icon.
  923. *
  924. * @since 4.3.0
  925. *
  926. * @param int $blog_id Optional. ID of the blog in question. Default current blog.
  927. * @return bool Whether the site has a site icon or not.
  928. */
  929. function has_site_icon( $blog_id = 0 ) {
  930. return (bool) get_site_icon_url( 512, '', $blog_id );
  931. }
  932. /**
  933. * Determines whether the site has a custom logo.
  934. *
  935. * @since 4.5.0
  936. *
  937. * @param int $blog_id Optional. ID of the blog in question. Default is the ID of the current blog.
  938. * @return bool Whether the site has a custom logo or not.
  939. */
  940. function has_custom_logo( $blog_id = 0 ) {
  941. $switched_blog = false;
  942. if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
  943. switch_to_blog( $blog_id );
  944. $switched_blog = true;
  945. }
  946. $custom_logo_id = get_theme_mod( 'custom_logo' );
  947. if ( $switched_blog ) {
  948. restore_current_blog();
  949. }
  950. return (bool) $custom_logo_id;
  951. }
  952. /**
  953. * Returns a custom logo, linked to home unless the theme supports removing the link on the home page.
  954. *
  955. * @since 4.5.0
  956. * @since 5.5.0 Added option to remove the link on the home page with `unlink-homepage-logo` theme support
  957. * for the `custom-logo` theme feature.
  958. * @since 5.5.1 Disabled lazy-loading by default.
  959. *
  960. * @param int $blog_id Optional. ID of the blog in question. Default is the ID of the current blog.
  961. * @return string Custom logo markup.
  962. */
  963. function get_custom_logo( $blog_id = 0 ) {
  964. $html = '';
  965. $switched_blog = false;
  966. if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
  967. switch_to_blog( $blog_id );
  968. $switched_blog = true;
  969. }
  970. $custom_logo_id = get_theme_mod( 'custom_logo' );
  971. // We have a logo. Logo is go.
  972. if ( $custom_logo_id ) {
  973. $custom_logo_attr = array(
  974. 'class' => 'custom-logo',
  975. 'loading' => false,
  976. );
  977. $unlink_homepage_logo = (bool) get_theme_support( 'custom-logo', 'unlink-homepage-logo' );
  978. if ( $unlink_homepage_logo && is_front_page() && ! is_paged() ) {
  979. /*
  980. * If on the home page, set the logo alt attribute to an empty string,
  981. * as the image is decorative and doesn't need its purpose to be described.
  982. */
  983. $custom_logo_attr['alt'] = '';
  984. } else {
  985. /*
  986. * If the logo alt attribute is empty, get the site title and explicitly pass it
  987. * to the attributes used by wp_get_attachment_image().
  988. */
  989. $image_alt = get_post_meta( $custom_logo_id, '_wp_attachment_image_alt', true );
  990. if ( empty( $image_alt ) ) {
  991. $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
  992. }
  993. }
  994. /**
  995. * Filters the list of custom logo image attributes.
  996. *
  997. * @since 5.5.0
  998. *
  999. * @param array $custom_logo_attr Custom logo image attributes.
  1000. * @param int $custom_logo_id Custom logo attachment ID.
  1001. * @param int $blog_id ID of the blog to get the custom logo for.
  1002. */
  1003. $custom_logo_attr = apply_filters( 'get_custom_logo_image_attributes', $custom_logo_attr, $custom_logo_id, $blog_id );
  1004. /*
  1005. * If the alt attribute is not empty, there's no need to explicitly pass it
  1006. * because wp_get_attachment_image() already adds the alt attribute.
  1007. */
  1008. $image = wp_get_attachment_image( $custom_logo_id, 'full', false, $custom_logo_attr );
  1009. if ( $unlink_homepage_logo && is_front_page() && ! is_paged() ) {
  1010. // If on the home page, don't link the logo to home.
  1011. $html = sprintf(
  1012. '<span class="custom-logo-link">%1$s</span>',
  1013. $image
  1014. );
  1015. } else {
  1016. $aria_current = is_front_page() && ! is_paged() ? ' aria-current="page"' : '';
  1017. $html = sprintf(
  1018. '<a href="%1$s" class="custom-logo-link" rel="home"%2$s>%3$s</a>',
  1019. esc_url( home_url( '/' ) ),
  1020. $aria_current,
  1021. $image
  1022. );
  1023. }
  1024. } elseif ( is_customize_preview() ) {
  1025. // If no logo is set but we're in the Customizer, leave a placeholder (needed for the live preview).
  1026. $html = sprintf(
  1027. '<a href="%1$s" class="custom-logo-link" style="display:none;"><img class="custom-logo" alt="" /></a>',
  1028. esc_url( home_url( '/' ) )
  1029. );
  1030. }
  1031. if ( $switched_blog ) {
  1032. restore_current_blog();
  1033. }
  1034. /**
  1035. * Filters the custom logo output.
  1036. *
  1037. * @since 4.5.0
  1038. * @since 4.6.0 Added the `$blog_id` parameter.
  1039. *
  1040. * @param string $html Custom logo HTML output.
  1041. * @param int $blog_id ID of the blog to get the custom logo for.
  1042. */
  1043. return apply_filters( 'get_custom_logo', $html, $blog_id );
  1044. }
  1045. /**
  1046. * Displays a custom logo, linked to home unless the theme supports removing the link on the home page.
  1047. *
  1048. * @since 4.5.0
  1049. *
  1050. * @param int $blog_id Optional. ID of the blog in question. Default is the ID of the current blog.
  1051. */
  1052. function the_custom_logo( $blog_id = 0 ) {
  1053. echo get_custom_logo( $blog_id );
  1054. }
  1055. /**
  1056. * Returns document title for the current page.
  1057. *
  1058. * @since 4.4.0
  1059. *
  1060. * @global int $page Page number of a single post.
  1061. * @global int $paged Page number of a list of posts.
  1062. *
  1063. * @return string Tag with the document title.
  1064. */
  1065. function wp_get_document_title() {
  1066. /**
  1067. * Filters the document title before it is generated.
  1068. *
  1069. * Passing a non-empty value will short-circuit wp_get_document_title(),
  1070. * returning that value instead.
  1071. *
  1072. * @since 4.4.0
  1073. *
  1074. * @param string $title The document title. Default empty string.
  1075. */
  1076. $title = apply_filters( 'pre_get_document_title', '' );
  1077. if ( ! empty( $title ) ) {
  1078. return $title;
  1079. }
  1080. global $page, $paged;
  1081. $title = array(
  1082. 'title' => '',
  1083. );
  1084. // If it's a 404 page, use a "Page not found" title.
  1085. if ( is_404() ) {
  1086. $title['title'] = __( 'Page not found' );
  1087. // If it's a search, use a dynamic search results title.
  1088. } elseif ( is_search() ) {
  1089. /* translators: %s: Search query. */
  1090. $title['title'] = sprintf( __( 'Search Results for &#8220;%s&#8221;' ), get_search_query() );
  1091. // If on the front page, use the site title.
  1092. } elseif ( is_front_page() ) {
  1093. $title['title'] = get_bloginfo( 'name', 'display' );
  1094. // If on a post type archive, use the post type archive title.
  1095. } elseif ( is_post_type_archive() ) {
  1096. $title['title'] = post_type_archive_title( '', false );
  1097. // If on a taxonomy archive, use the term title.
  1098. } elseif ( is_tax() ) {
  1099. $title['title'] = single_term_title( '', false );
  1100. /*
  1101. * If we're on the blog page that is not the homepage
  1102. * or a single post of any post type, use the post title.
  1103. */
  1104. } elseif ( is_home() || is_singular() ) {
  1105. $title['title'] = single_post_title( '', false );
  1106. // If on a category or tag archive, use the term title.
  1107. } elseif ( is_category() || is_tag() ) {
  1108. $title['title'] = single_term_title( '', false );
  1109. // If on an author archive, use the author's display name.
  1110. } elseif ( is_author() && get_queried_object() ) {
  1111. $author = get_queried_object();
  1112. $title['title'] = $author->display_name;
  1113. // If it's a date archive, use the date as the title.
  1114. } elseif ( is_year() ) {
  1115. $title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );
  1116. } elseif ( is_month() ) {
  1117. $title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
  1118. } elseif ( is_day() ) {
  1119. $title['title'] = get_the_date();
  1120. }
  1121. // Add a page number if necessary.
  1122. if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
  1123. /* translators: %s: Page number. */
  1124. $title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
  1125. }
  1126. // Append the description or site title to give context.
  1127. if ( is_front_page() ) {
  1128. $title['tagline'] = get_bloginfo( 'description', 'display' );
  1129. } else {
  1130. $title['site'] = get_bloginfo( 'name', 'display' );
  1131. }
  1132. /**
  1133. * Filters the separator for the document title.
  1134. *
  1135. * @since 4.4.0
  1136. *
  1137. * @param string $sep Document title separator. Default '-'.
  1138. */
  1139. $sep = apply_filters( 'document_title_separator', '-' );
  1140. /**
  1141. * Filters the parts of the document title.
  1142. *
  1143. * @since 4.4.0
  1144. *
  1145. * @param array $title {
  1146. * The document title parts.
  1147. *
  1148. * @type string $title Title of the viewed page.
  1149. * @type string $page Optional. Page number if paginated.
  1150. * @type string $tagline Optional. Site description when on home page.
  1151. * @type string $site Optional. Site title when not on home page.
  1152. * }
  1153. */
  1154. $title = apply_filters( 'document_title_parts', $title );
  1155. $title = implode( " $sep ", array_filter( $title ) );
  1156. /**
  1157. * Filters the document title.
  1158. *
  1159. * @since 5.8.0
  1160. *
  1161. * @param string $title Document title.
  1162. */
  1163. $title = apply_filters( 'document_title', $title );
  1164. return $title;
  1165. }
  1166. /**
  1167. * Displays title tag with content.
  1168. *
  1169. * @ignore
  1170. * @since 4.1.0
  1171. * @since 4.4.0 Improved title output replaced `wp_title()`.
  1172. * @access private
  1173. */
  1174. function _wp_render_title_tag() {
  1175. if ( ! current_theme_supports( 'title-tag' ) ) {
  1176. return;
  1177. }
  1178. echo '<title>' . wp_get_document_title() . '</title>' . "\n";
  1179. }
  1180. /**
  1181. * Display or retrieve page title for all areas of blog.
  1182. *
  1183. * By default, the page title will display the separator before the page title,
  1184. * so that the blog title will be before the page title. This is not good for
  1185. * title display, since the blog title shows up on most tabs and not what is
  1186. * important, which is the page that the user is looking at.
  1187. *
  1188. * There are also SEO benefits to having the blog title after or to the 'right'
  1189. * of the page title. However, it is mostly common sense to have the blog title
  1190. * to the right with most browsers supporting tabs. You can achieve this by
  1191. * using the seplocation parameter and setting the value to 'right'. This change
  1192. * was introduced around 2.5.0, in case backward compatibility of themes is
  1193. * important.
  1194. *
  1195. * @since 1.0.0
  1196. *
  1197. * @global WP_Locale $wp_locale WordPress date and time locale object.
  1198. *
  1199. * @param string $sep Optional. How to separate the various items within the page title.
  1200. * Default '&raquo;'.
  1201. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1202. * @param string $seplocation Optional. Location of the separator ('left' or 'right').
  1203. * @return string|void String when `$display` is true, nothing otherwise.
  1204. */
  1205. function wp_title( $sep = '&raquo;', $display = true, $seplocation = '' ) {
  1206. global $wp_locale;
  1207. $m = get_query_var( 'm' );
  1208. $year = get_query_var( 'year' );
  1209. $monthnum = get_query_var( 'monthnum' );
  1210. $day = get_query_var( 'day' );
  1211. $search = get_query_var( 's' );
  1212. $title = '';
  1213. $t_sep = '%WP_TITLE_SEP%'; // Temporary separator, for accurate flipping, if necessary.
  1214. // If there is a post.
  1215. if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
  1216. $title = single_post_title( '', false );
  1217. }
  1218. // If there's a post type archive.
  1219. if ( is_post_type_archive() ) {
  1220. $post_type = get_query_var( 'post_type' );
  1221. if ( is_array( $post_type ) ) {
  1222. $post_type = reset( $post_type );
  1223. }
  1224. $post_type_object = get_post_type_object( $post_type );
  1225. if ( ! $post_type_object->has_archive ) {
  1226. $title = post_type_archive_title( '', false );
  1227. }
  1228. }
  1229. // If there's a category or tag.
  1230. if ( is_category() || is_tag() ) {
  1231. $title = single_term_title( '', false );
  1232. }
  1233. // If there's a taxonomy.
  1234. if ( is_tax() ) {
  1235. $term = get_queried_object();
  1236. if ( $term ) {
  1237. $tax = get_taxonomy( $term->taxonomy );
  1238. $title = single_term_title( $tax->labels->name . $t_sep, false );
  1239. }
  1240. }
  1241. // If there's an author.
  1242. if ( is_author() && ! is_post_type_archive() ) {
  1243. $author = get_queried_object();
  1244. if ( $author ) {
  1245. $title = $author->display_name;
  1246. }
  1247. }
  1248. // Post type archives with has_archive should override terms.
  1249. if ( is_post_type_archive() && $post_type_object->has_archive ) {
  1250. $title = post_type_archive_title( '', false );
  1251. }
  1252. // If there's a month.
  1253. if ( is_archive() && ! empty( $m ) ) {
  1254. $my_year = substr( $m, 0, 4 );
  1255. $my_month = substr( $m, 4, 2 );
  1256. $my_day = (int) substr( $m, 6, 2 );
  1257. $title = $my_year .
  1258. ( $my_month ? $t_sep . $wp_locale->get_month( $my_month ) : '' ) .
  1259. ( $my_day ? $t_sep . $my_day : '' );
  1260. }
  1261. // If there's a year.
  1262. if ( is_archive() && ! empty( $year ) ) {
  1263. $title = $year;
  1264. if ( ! empty( $monthnum ) ) {
  1265. $title .= $t_sep . $wp_locale->get_month( $monthnum );
  1266. }
  1267. if ( ! empty( $day ) ) {
  1268. $title .= $t_sep . zeroise( $day, 2 );
  1269. }
  1270. }
  1271. // If it's a search.
  1272. if ( is_search() ) {
  1273. /* translators: 1: Separator, 2: Search query. */
  1274. $title = sprintf( __( 'Search Results %1$s %2$s' ), $t_sep, strip_tags( $search ) );
  1275. }
  1276. // If it's a 404 page.
  1277. if ( is_404() ) {
  1278. $title = __( 'Page not found' );
  1279. }
  1280. $prefix = '';
  1281. if ( ! empty( $title ) ) {
  1282. $prefix = " $sep ";
  1283. }
  1284. /**
  1285. * Filters the parts of the page title.
  1286. *
  1287. * @since 4.0.0
  1288. *
  1289. * @param string[] $title_array Array of parts of the page title.
  1290. */
  1291. $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );
  1292. // Determines position of the separator and direction of the breadcrumb.
  1293. if ( 'right' === $seplocation ) { // Separator on right, so reverse the order.
  1294. $title_array = array_reverse( $title_array );
  1295. $title = implode( " $sep ", $title_array ) . $prefix;
  1296. } else {
  1297. $title = $prefix . implode( " $sep ", $title_array );
  1298. }
  1299. /**
  1300. * Filters the text of the page title.
  1301. *
  1302. * @since 2.0.0
  1303. *
  1304. * @param string $title Page title.
  1305. * @param string $sep Title separator.
  1306. * @param string $seplocation Location of the separator ('left' or 'right').
  1307. */
  1308. $title = apply_filters( 'wp_title', $title, $sep, $seplocation );
  1309. // Send it out.
  1310. if ( $display ) {
  1311. echo $title;
  1312. } else {
  1313. return $title;
  1314. }
  1315. }
  1316. /**
  1317. * Display or retrieve page title for post.
  1318. *
  1319. * This is optimized for single.php template file for displaying the post title.
  1320. *
  1321. * It does not support placing the separator after the title, but by leaving the
  1322. * prefix parameter empty, you can set the title separator manually. The prefix
  1323. * does not automatically place a space between the prefix, so if there should
  1324. * be a space, the parameter value will need to have it at the end.
  1325. *
  1326. * @since 0.71
  1327. *
  1328. * @param string $prefix Optional. What to display before the title.
  1329. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1330. * @return string|void Title when retrieving.
  1331. */
  1332. function single_post_title( $prefix = '', $display = true ) {
  1333. $_post = get_queried_object();
  1334. if ( ! isset( $_post->post_title ) ) {
  1335. return;
  1336. }
  1337. /**
  1338. * Filters the page title for a single post.
  1339. *
  1340. * @since 0.71
  1341. *
  1342. * @param string $_post_title The single post page title.
  1343. * @param WP_Post $_post The current post.
  1344. */
  1345. $title = apply_filters( 'single_post_title', $_post->post_title, $_post );
  1346. if ( $display ) {
  1347. echo $prefix . $title;
  1348. } else {
  1349. return $prefix . $title;
  1350. }
  1351. }
  1352. /**
  1353. * Display or retrieve title for a post type archive.
  1354. *
  1355. * This is optimized for archive.php and archive-{$post_type}.php template files
  1356. * for displaying the title of the post type.
  1357. *
  1358. * @since 3.1.0
  1359. *
  1360. * @param string $prefix Optional. What to display before the title.
  1361. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1362. * @return string|void Title when retrieving, null when displaying or failure.
  1363. */
  1364. function post_type_archive_title( $prefix = '', $display = true ) {
  1365. if ( ! is_post_type_archive() ) {
  1366. return;
  1367. }
  1368. $post_type = get_query_var( 'post_type' );
  1369. if ( is_array( $post_type ) ) {
  1370. $post_type = reset( $post_type );
  1371. }
  1372. $post_type_obj = get_post_type_object( $post_type );
  1373. /**
  1374. * Filters the post type archive title.
  1375. *
  1376. * @since 3.1.0
  1377. *
  1378. * @param string $post_type_name Post type 'name' label.
  1379. * @param string $post_type Post type.
  1380. */
  1381. $title = apply_filters( 'post_type_archive_title', $post_type_obj->labels->name, $post_type );
  1382. if ( $display ) {
  1383. echo $prefix . $title;
  1384. } else {
  1385. return $prefix . $title;
  1386. }
  1387. }
  1388. /**
  1389. * Display or retrieve page title for category archive.
  1390. *
  1391. * Useful for category template files for displaying the category page title.
  1392. * The prefix does not automatically place a space between the prefix, so if
  1393. * there should be a space, the parameter value will need to have it at the end.
  1394. *
  1395. * @since 0.71
  1396. *
  1397. * @param string $prefix Optional. What to display before the title.
  1398. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1399. * @return string|void Title when retrieving.
  1400. */
  1401. function single_cat_title( $prefix = '', $display = true ) {
  1402. return single_term_title( $prefix, $display );
  1403. }
  1404. /**
  1405. * Display or retrieve page title for tag post archive.
  1406. *
  1407. * Useful for tag template files for displaying the tag page title. The prefix
  1408. * does not automatically place a space between the prefix, so if there should
  1409. * be a space, the parameter value will need to have it at the end.
  1410. *
  1411. * @since 2.3.0
  1412. *
  1413. * @param string $prefix Optional. What to display before the title.
  1414. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1415. * @return string|void Title when retrieving.
  1416. */
  1417. function single_tag_title( $prefix = '', $display = true ) {
  1418. return single_term_title( $prefix, $display );
  1419. }
  1420. /**
  1421. * Display or retrieve page title for taxonomy term archive.
  1422. *
  1423. * Useful for taxonomy term template files for displaying the taxonomy term page title.
  1424. * The prefix does not automatically place a space between the prefix, so if there should
  1425. * be a space, the parameter value will need to have it at the end.
  1426. *
  1427. * @since 3.1.0
  1428. *
  1429. * @param string $prefix Optional. What to display before the title.
  1430. * @param bool $display Optional. Whether to display or retrieve title. Default true.
  1431. * @return string|void Title when retrieving.
  1432. */
  1433. function single_term_title( $prefix = '', $display = true ) {
  1434. $term = get_queried_object();
  1435. if ( ! $term ) {
  1436. return;
  1437. }
  1438. if ( is_category() ) {
  1439. /**
  1440. * Filters the category archive page title.
  1441. *
  1442. * @since 2.0.10
  1443. *
  1444. * @param string $term_name Category name for archive being displayed.
  1445. */
  1446. $term_name = apply_filters( 'single_cat_title', $term->name );
  1447. } elseif ( is_tag() ) {
  1448. /**
  1449. * Filters t…

Large files files are truncated, but you can click here to view the full file