[wp-trac] [WordPress Trac] #61352: PHP deprecation warning in /wp-includes/general-template.php

WordPress Trac noreply at wordpress.org
Tue Jun 4 09:03:36 UTC 2024


#61352: PHP deprecation warning in /wp-includes/general-template.php
--------------------------+------------------------------
 Reporter:  nexbridge     |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  General       |     Version:  6.5.3
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by Tussendoor):

 We are experiencing the same problem. It occurs on a dynamic post which we
 are creating on runtime.

 Ways to reproduce:

 1. Add a rewrite tag to be able to catch a custom query
 {{{#!php
 <?php
 add_rewrite_tag('%rewrite_tag_example%', '([^&]+)');
 }}}

 2. Add a rewrite rule for the rewrite tag
 {{{#!php
 <?php
 add_rewrite_rule('basepage/([^/]*)/?',
 'index.php?rewrite_tag_example=$matches[1]', 'top');
 }}}

 3. Register a template with template_include
 {{{#!php
 <?php
 add_filter('template_include', 'registerFrontendTemplate', 9999);
 }}}

 4. Add the registerFrontendTemplate function with a custom query to
 dynamically populate a template
 {{{#!php
 <?php
 /**
  * Register the template file
  *
  * @param string $template
  * @return string
  */
 function registerFrontendTemplate(string $template)
 {
     if (empty(get_query_var('rewrite_tag_example'))) {
         return $template;
     }

     // Add custom query here. To get data from a custom database table for
     // example. Use the custom query variable to get postdata dynamically
     // based on the request

     return locate_template('template/single.php');
 }
 }}}

 5. Refresh the permalinks by navigating to the permalinks settings in the
 WordPress admin.

 6. Go to the generated frontend page: https:/example.com/basepage/test.
 With that URL the "rewrite_tag_example" query variable is filled with
 "test".

 7. On this page the <title> HTML-element will show the deprecation
 warning. This is because in wp-includes/general-template.php the wp_title
 function call the single_post_title function via:
 {{{#!php
 <?php
 // If there is a post.
 if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && !
 is_front_page() ) ) {
     $title = single_post_title( '', false );
 }
 }}}

 And the single_post_title function returns null because of:
 {{{#!php
 <?php
 if ( ! isset( $_post->post_title ) ) {
    return;
 }
 }}}


 **Possible solution**
 We could make the single_post_title function return a string value. But
 this does not solve the problem for other possible occurances of this
 error that are possibly triggered by the wp_title function. So that
 requires changing the functions below to always return a string instead of
 string|void:
 - single_post_title
 - post_type_archive_title
 - single_term_title
 - single_term_title
 - post_type_archive_title

 As the default value of $title is already an empty string (and not null)
 in the wp_title function we could try this for each occurance of those
 functions in wp_title:
 {{{#!php
 <?php
 // If there is a post.
 if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && !
 is_front_page() ) ) {
     $title ??= single_post_title( '', false );
 }
 }}}

 The Null Coalescing Assignment operator (??=) does require PHP 7.4 tho.

 Hope this helps! :)

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61352#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list