Gần đây, mình có cài lại Plugin Woocommerce cho 1 site vệ tinh để test case study lẫn sửa lỗi sản phẩm do thiếu Schema Markup luôn. Trong quá trình cài đặt, cần update WordPress lẫn PHP lên phiên bản mới nhất nên gặp 1 lỗi PHP là phát hiện giá trị của biến không phải là số. Tìm mãi mới ra cách giải quyết nên share luôn cho ai cần. Dưới đây là bài hướng dẫn cách sửa lỗi: “PHP Warning: A non-numeric value encountered”. Lỗi này xuất hiện ở theme Voice và Grindlove của WordPress sau khi update phiên bản PHP 4.9. Thường là do bạn sử dụng bộ đếm traffic bằng plugin Entry Views.
PHP Warning: A non-numeric value encountered error
Mục lục:
I. Nguyên nhân PHP Warning: A non-numeric value encountered error
Một vài biến trong PHP mới cần là Integer thay vì String như trước đây. Lỗi dạng này gặp khá nhiều đấy nhưng mà chúng sẽ được giải quyết ở bài viết khác cơ. Còn hiện tại, chỉ cần sửa lại biến về int là được.
1. Theme Voice
Lỗi này sẽ xuất hiện ở dòng 855 của file helpers.php: PHP Warning: A non-numeric value encountered in … /wp-content/themes/voice/include/helpers.php on line 855
1
2
3
4
5
6
7
8
|
case ‘views’:
global $wp_locale;
$thousands_sep = isset( $wp_locale->number_format[‘thousands_sep’] ) ? $wp_locale->number_format[‘thousands_sep’] : ‘,’;
if ( strlen( $thousands_sep ) > 1 ) {
$thousands_sep = trim( $thousands_sep );
}
/*line 855*/ $meta = function_exists( ‘ev_get_post_view_count’ ) ? number_format_i18n( absint( str_replace( $thousands_sep, ”, ev_get_post_view_count( get_the_ID() ) ) + vce_get_option( ‘views_forgery’ ) ) ) . ‘ ‘.__vce( ‘views’ ) : ”;
break;
|
2. Theme Grindlove
Lỗi này sẽ xuất hiện ở dòng 163 của file template-functions.php: PHP Warning: A non-numeric value encountered in … wp-contentthemesgridlovecoretemplate-functions.php on line 163
1
2
3
4
5
6
7
8
|
case ‘views’:
global $wp_locale;
$thousands_sep = isset( $wp_locale->number_format[‘thousands_sep’] ) ? $wp_locale->number_format[‘thousands_sep’] : ‘,’;
if ( strlen( $thousands_sep ) > 1 ) {
$thousands_sep = trim( $thousands_sep );
}
/*line 163*/ $meta = function_exists( ‘ev_get_post_view_count’ ) ? number_format_i18n( absint( str_replace( $thousands_sep, ”, ev_get_post_view_count( get_the_ID() ) ) + gridlove_get_option( ‘views_forgery’ ) ) ) . ‘ ‘.__gridlove( ‘views’ ) : ”;
break;
|
II. Sửa lỗi PHP Warning: A non-numeric value encountered error
Tuy chỉ đơn giản là chuyển biến về Interger những khá là tốn time suy nghĩ đấy. Bạn chỉ cần thêm “(int)” trước “str_replace” và “vce_get_option” hay “gridlove_get_option” là xong.
1. Theme Voice
1
2
3
4
5
6
7
8
|
case ‘views’:
global $wp_locale;
$thousands_sep = isset( $wp_locale->number_format[‘thousands_sep’] ) ? $wp_locale->number_format[‘thousands_sep’] : ‘,’;
if ( strlen( $thousands_sep ) > 1 ) {
$thousands_sep = trim( $thousands_sep );
}
/*line 855*/ $meta = function_exists( ‘ev_get_post_view_count’ ) ? number_format_i18n( absint( (int)str_replace( $thousands_sep, ”, ev_get_post_view_count( get_the_ID() ) ) + (int)vce_get_option( ‘views_forgery’ ) ) ) . ‘ ‘.__vce( ‘views’ ) : ”;
break;
|
2. Theme Grindlove
1
2
3
4
5
6
7
8
|
case ‘views’:
global $wp_locale;
$thousands_sep = isset( $wp_locale->number_format[‘thousands_sep’] ) ? $wp_locale->number_format[‘thousands_sep’] : ‘,’;
if ( strlen( $thousands_sep ) > 1 ) {
$thousands_sep = trim( $thousands_sep );
}
/*line 163*/ $meta = function_exists( ‘ev_get_post_view_count’ ) ? number_format_i18n( absint( (int)str_replace( $thousands_sep, ”, ev_get_post_view_count( get_the_ID() ) ) + (int)gridlove_get_option( ‘views_forgery’ ) ) ) . ‘ ‘.__gridlove( ‘views’ ) : ”;
break;
|
Vậy là bạn đã giar quyết xong rồi đấy. Đây là lỗi Evergreen gặp khi cài Woocommerce nên search trên mạng cách sửa “A non-numeric value encountered” luôn. Ai bị thì tham khảo nha!