Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#42034 closed defect (bug) (invalid)

PHP 7.1 PHP Warning: A non-numeric value encountered wp-includes/formatting.php on line 3362

Reported by: laserjobs's profile laserjobs Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.8.2
Component: Formatting Keywords:
Focuses: Cc:

Description

After updating from PHP 7.0 to 7.1 this error started filling my logs

Got error 'PHP message: PHP Warning: A non-numeric value encountered in ...html/wp-includes/formatting.php on line 3362

<?php
function wp_trim_words( $text, $num_words = 55, $more = null ) {
        if ( null === $more ) {
                $more = __( '&hellip;' );
        }

Change History (5)

#1 @SergeyBiryukov
9 years ago

  • Component changed from General to Formatting

#2 @johnbillion
9 years ago

  • Keywords reporter-feedback added

Thanks for the report, @laserjobs.

The code in question is this line within wp_trim_words():

$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );

If the $num_words variable is not numeric, it would trigger this error.

I've checked every use of wp_trim_words() in WordPress core and it only uses numeric values for this parameter. Could you try switching to one of the default themes such as TwentySeventeen and see if the issue goes away? If not, could you try deactivating your plugins one by one to see if you can pinpoint the issue?

#3 @laserjobs
9 years ago

I know you have probably heard this before but it is a live site I really can't test with. Is there another way to check without having to change themes out or disable plugins? Can I search files for "wp_trim_words()" because I found that the wp-content/themes/smart-mag/lib/posts.php file is common to all those warning and is the only reference I found to that function in themes and plugins?

Also I did find the same warning doing a Google search for ""wp-includes/formatting.php on line 3362" on a some wp sites that have debugging enabled.

<?php
        /**
         * Custom excerpt function - utilize existing wordpress functions to add real support for <!--more-->
         * to excerpts which is missing in the_excerpt().
         * 
         * Maintain plugin functionality by utilizing wordpress core functions and filters. 
         * 
         * @param  string|null  $text
         * @param  integer  $length
         * @param  array   $options   add_more: add more if needed, more_text = more link anchor, force_more: always add more link
         * @return string
         */
        public function excerpt($text = null, $length = 55, $options = array())
        {
                global $more;

                // add defaults
                $options = array_merge(array('add_more' => null, 'force_more' => null), $options);
                
                // add support for <!--more--> cut-off on custom home-pages and the like
                $old_more = $more;
                $more = false;
                
                // override options
                $more_text = $this->more_text;
                extract($options);

                // set global more_text - used by excerpt_read_more()
                $this->more_text = $more_text;
                
                if (!$text) {
                        
                        // have a manual excerpt?
                        if (has_excerpt()) {
                                return apply_filters('the_excerpt', get_the_excerpt()) . ($force_more ? $this->excerpt_read_more() : '');
                        }
                        
                        // don't add "more" link
                        $text = get_the_content('');
                }
                
                $text = strip_shortcodes(apply_filters('bunyad_excerpt_pre_strip_shortcodes', $text));
                $text = str_replace(']]>', ']]&gt;', $text);
                
                // get plaintext excerpt trimmed to right length
                $excerpt = wp_trim_words($text, $length, apply_filters('bunyad_excerpt_hellip', '&hellip;') . ($add_more !== false ? $this->excerpt_read_more() : '')); 


                /*
                 * Force "More" link?
                 * 
                 * wp_trim_words() will only add the read more link if it's needed - if the length actually EXCEEDS. In some cases,
                 * for styling, read more has to be always present. 
                 * 
                 */
                if ($force_more) {
                        
                        $read_more = $this->excerpt_read_more();

                        if (substr($excerpt, -strlen($read_more)) !== $read_more) {
                                $excerpt .= $this->excerpt_read_more();
                        }
                }
                
                // fix extra spaces
                $excerpt = trim(str_replace('&nbsp;', ' ', $excerpt)); 
                
                // apply filters after to prevent added html functionality from being stripped
                // REMOVED: the_content filters often clutter the HTML - use the_excerpt filter instead 
                // $excerpt = apply_filters('the_content', $excerpt);
                
                $excerpt = apply_filters('the_excerpt', apply_filters('get_the_excerpt', $excerpt));
                
                // revert
                $more = $old_more;
                
                return $excerpt;
        }
Last edited 9 years ago by laserjobs (previous) (diff)

#4 @laserjobs
9 years ago

Theme developer got back to me with a fix. Not a Wordpress Issue. Sorry for any inconvenience.

#5 @johnbillion
9 years ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

No problem, thanks for letting us know!

Note: See TracTickets for help on using tickets.