WordPress除錯 – error_log()

在 trace 別人寫的plugin 時,發現別人有把 exception 寫入到 log 裡,wordpress 的 error_log() 會寫入 debug.log 到 /wp-content/ 目錄裡。



Example wp-config.php for Debugging

The following code, inserted in your wp-config.php file, will log all errors, notices, and warnings to a file called debug.log in the wp-content directory. It will also hide the errors so they do not interrupt page generation.

 // Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

// Disable display of errors and warnings 
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );

NOTE: You must insert this BEFORE /* That's all, stop editing! Happy blogging. */ in the wp-config.php file

接下來終於看到 Error message:

[21-Apr-2017 10:45:17 UTC] WordPress 資料庫錯誤:Column ‘user_id’ cannot be null 由指令 INSERT INTO `wp_apsl_users_social_profile_details` (`user_id`, `provider_name`, `identifier`, `unique_verifier`, `email`, `first_name`, `last_name`, `profile_url`, `photo_url`, `display_name`, `description`, `gender`) VALUES (NULL, ‘facebook’, ‘1387607167994’, ‘df8c6ac9b4084ab507ea54f47ed829f26ba1’, ‘[email protected]’, ‘xx’, ‘x’, ‘https://www.facebook.com/app_scoped_user_id/1387607167994/’, ‘https://fb-s-a-a.akamaihd.net/h-ak-xap1/v/t1.0-1/c0.13.50.50/p50x50/n.jpg’, ‘xx x’, ”, ‘female’) 引發,錯誤來自 require(‘wp-load.php’), require_once(‘wp-config.php’), require_once(‘wp-settings.php’), do_action(‘init’), WP_Hook->do_action, WP_Hook->apply_filters, APSL_Lite_Class->login_check, include(‘/plugins/accesspress-social-login-lite/inc/frontend/login_check.php’), APSL_Lite_Login_Check_Class->__construct, APSL_Lite_Login_Check_Class->onFacebookLogin, APSL_Lite_Login_Check_Class::UpdateUserMeta


原來是 username 卡關,多增加下面的檢查即可:

先用這一個 validate_username( $username ) 來檢查 username. 

function validate_username( $username ) {
	$sanitized = sanitize_user( $username, true );
	$valid = ( $sanitized == $username && ! empty( $sanitized ) );

	 * Filters whether the provided username is valid or not.
	 * @since 2.0.1
	 * @param bool   $valid    Whether given username is valid.
	 * @param string $username Username to check.
	return apply_filters( 'validate_username', $valid, $username );