0

After deleting some categories (shouldn't have done this), I get the following error

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

Here's the file which caused the error. Here's an excerpt (line 45 is $output .= '<a href="' . $link . '">' . $term->name. '</a>, ';)

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

        // no option may inerrupt !
        if(!$term) {
          continue;
        }

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

How can I fix this problem? I recreated all the deleted categories (I backuped their names and slugs), but the error still remains.

Thank you for your help!

Here's the complete file

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);

        // no option may inerrupt !
        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', __('Link to taxonomy page', 'hydraforms')))
      ->setDefaultValue(false);
  }
}
3

get_term can return a WP_Error object in addition to a falsy value for term not found or an actual term row.

You fix this, by adding an additional check:

if (!$term) {
   continue;
}

Becomes:

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

You should also do this above the get_term_link call.

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

$link = get_term_link($term);

get_term usually returns a WP_Error when the taxonomy doesn't exist or isn't registered (you can look at the source for more info). So much sure that it is. If you are registering it, make sure that the above code (that's causing the error) is happening sometime after init, where the taxonomy is probably registered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.