Error: Objeto de clase WP_Error no se puede convertir a string en WordPress

18 abr 2014, 21:50:15
Vistas: 16.2K
Votos: 1

Después de eliminar algunas categorías (no debería haberlo hecho), obtengo el siguiente error:

Catchable fatal error: Object of class WP_Error could not be converted to string in [FILENAME] on line 45

Aquí está el archivo que causó el error. Este es un extracto (la línea 45 es $output .= '<a href="' . $link . '">' . $term->name. '</a>, ';):

   ...
  foreach($item['value'] as $value) {
        $term = get_term($value, $fieldSettings['taxonomy']);
        $link = get_term_link($term);

        // ¡ninguna opción debe interrumpir el flujo!
        if(!$term) {
          continue;
        }

        if(isset($formatterSettings['link_bool']) && $formatterSettings['link_bool']) {
          $output .= '<a href="' . $link  . '">' . $term->name. '</a>, ';
        } else {
          $output .= $term->name . ', ';
        }
      }
    }
 ...

¿Cómo puedo solucionar este problema? Recreé todas las categorías eliminadas (había respaldado sus nombres y slugs), pero el error persiste.

¡Gracias por tu ayuda!

Aquí está el archivo completo:

namespace Hydra\Formatters;

use Hydra\Builder;

class TaxonomyFormatter extends BasicFormatter {

  public function __construct() {
    $this->name = 'taxonomy';
  }

  public function render(\HydraFieldViewRecord $viewView, $post) {

    $items = $this->getValues($viewView);
    if(!$items) {
      return $items;
    }

    $fieldSettings = $viewView->field->attributes;
    $formatterSettings = $viewView->settings;

    $meta = $viewView->field->meta;
    $output = '';

    foreach ($items as $item) {
      if(is_string($item['value'])) {
        if($item['value'] == 0) {
          continue;
        }
        $item['value'] = array($item['value']);
      }

      foreach($item['value'] as $value) {
        $term = get_term($value, $fieldSettings['taxonomy']);
        $link = get_term_link($term);

        // ¡ninguna opción debe interrumpir el flujo!
        if(!$term) {
          continue;
        }

        if(isset($formatterSettings['link_bool']) && $formatterSettings['link_bool']) {
          $output .= '<a href="' . $link  . '">' . $term->name. '</a>, ';
        } else {
          $output .= $term->name . ', ';
        }
      }
    }

    $output = trim($output, ', ');
    $terms = $output;

    $output = '';
    $output .= '<div ' . $this->printAttributes($viewView) . '>';

    if ($meta->prefix) {
      $output .= "<div class=\"field-prefix\" >" . $meta->prefix . "</div>";
    }

    $output .= "<div class=field-value>" . $terms . "</div>";
    if ($meta->suffix) {
      $output .= "<div class=\"field-suffix\">" . $meta->suffix . "</div>";
    }
    $output .= "</div>";

    return $output;
  }

  public function getSettingsForm($parentElement) {
    parent::getSettingsForm($parentElement);
    $parentElement->addField('checkbox', array('link_bool', __('Enlazar a página de taxonomía', 'hydraforms')))
      ->setDefaultValue(false);
  }
}
0
Todas las respuestas a la pregunta 1
0

get_term puede devolver un objeto WP_Error además de un valor falso cuando no se encuentra el término o una fila de término real.

Puedes solucionar esto añadiendo una verificación adicional:

if (!$term) {
   continue;
}

Se convierte en:

if (!$term || is_wp_error($term)) {
    continue;
}

También deberías hacer esto antes de la llamada a get_term_link.

$term = get_term($value, $fieldSettings['taxonomy']);
if (!$term || is_wp_error($term)) {
    continue;
}

$link = get_term_link($term);

get_term normalmente devuelve un WP_Error cuando la taxonomía no existe o no está registrada (puedes ver el código fuente para más información). Así que asegúrate de que lo esté. Si estás registrándola, asegúrate de que el código anterior (que está causando el error) se ejecute después de init, donde probablemente se registra la taxonomía.

18 abr 2014 21:54:23