【问题标题】:Calling paramenter, get UTF8 null return error in formatting.php调用参数,在formatting.php中得到UTF 8 null返回错误
【发布时间】:2015-05-03 22:44:52
【问题描述】:

我收到一个相关的 Catchable 致命错误:WP_User 类的对象无法在第 766 行的 /wp-includes/formatting.php 中转换为字符串。我在https://wordpress.org/support/topic/catchable-fatal-error-object-of-class-wp_error-could-not-be-converted-to-string-11 找到了一个相关的线程

我正在使用的 php,我认为应该可以工作的是:

add_filter('gform_field_value_pm_first', 'populate_pm_first');
function populate_pm_first($value){
    return $user_info = get_userdata(get_current_user_id());
      $first_name = $user_info->first_name;
      echo "$first_name";

}

我得到的错误与formatting.php有关:

/**

 * Checks for invalid UTF8 in a string.
 *
 * @since 2.8.0
 *
 * @param string $string The text which is to be checked.
 * @param boolean $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
 * @return string The checked text.
 */
function wp_check_invalid_utf8( $string, $strip = false ) {
    $string = (string) $string;

        if ( 0 === strlen( $string ) ) {
        return '';
    }

【问题讨论】:

  • 不是在过滤器中返回整个用户对象,而是返回名称,如return $user_info->user_firstname; 这将解决问题。

标签: php utf-8 wordpress


【解决方案1】:

我认为语法是:

$first_name = $user_info->user_firstname;

可能值得一试。希望这会有所帮助:)

【讨论】:

    【解决方案2】:

    您需要将用户名作为字符串返回,而不是整个用户 object

     add_filter('gform_field_value_pm_first', 'populate_pm_first');
     function populate_pm_first($value)
     {
          $user_info = get_userdata(get_current_user_id());
          $first_name = $user_info->first_name;
          return $first_name; // return user name string
          //echo "$first_name";
     }
    

    【讨论】:

      猜你喜欢
      • 2012-11-14
      • 2017-03-20
      • 2019-05-27
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 2019-06-27
      相关资源
      最近更新 更多