I need help with this:

PHP Catchable fatal error: Object of class WP_Error could not be converted to string in /hosting/www/ortopediairati.es/public/wp-content/themes/irati/rt-framework/functions/rt_breadcrumb.php on line 58

I´ve updated to Wordpress 4.8 and the web is not working well.

The full file is here:


/* RT-Breadcrumb Function */
function rt_breadcrumb(){
global $taxonomy,$term_slug,$post,$delimiter;

    return false;

$before='<div class="breadcrumb">'.wpml_t(THEMESLUG, 'Breadcrumb Menu Text', get_option(THEMESLUG.'_breadcrumb_text')).' ';
$delimiter=' | ';

/*  WooCommerce Breadcrumb */ 
if ( function_exists( 'woocommerce_breadcrumb' ) ) {
    if( is_woocommerce() ){
        $defaults = array(
            'delimiter'  => $delimiter,
            'wrap_before'  => $before,
            'wrap_after' => $after,
            'before'   => '',
            'after'   => '',
            'home'    => null

        return false;

echo $before;

//Home Page
echo "<a href=\"". BLOGURL ."\" title=\"". get_bloginfo('name')."\">".__( 'Home', 'rt_theme' )."</a>";

// page parents function
function page_parents($parent_page_id,$child_pages){
    global $delimiter;

    $parent_page = get_page($parent_page_id);
    $page_parents = $delimiter."<a href=\"".get_permalink($parent_page->ID)."\" title=\"". get_the_title($parent_page->ID) ."\" >". get_the_title($parent_page->ID) ."</a>" .$child_pages;

    if ($parent_page->post_parent) page_parents($parent_page->post_parent,$page_parents);

    else echo $page_parents;


// term parents function
function term_parents($term_id,$child_terms){
    global $taxonomy,$delimiter;

    $parent_term = get_term_by('ID',$term_id, $taxonomy);
    $term_parents = $delimiter."<a href=\"".get_term_link($parent_term->slug,$taxonomy)."\" title=\"". $parent_term->name ."\" >". $parent_term->name ."</a>" .$child_terms;

    if ($parent_term->parent) term_parents($parent_term -> parent,$term_parents);

    else echo $term_parents;


//get start page
function get_start_page($start_page){
    global $delimiter;

    //start page parents
    if ($get_start_page -> post_parent){
        page_parents( $get_start_page -> post_parent,''); 

    //start page
    if ($start_page && !get_query_var('lang')) {
        echo  $delimiter."<a href=\"".get_permalink($start_page)."\" title=\"". get_the_title($start_page) ."\" >". get_the_title($start_page) ."</a>";

function term_links(){
    global $taxonomy,$post_type,$term_slug,$delimiter;

    //Find start page and define taxonomy names

    //get start page
    if ($start_page) get_start_page($start_page);

    $term=get_term_by('slug',$term_slug, $taxonomy);

    //parent terms
    if (is_object($term) && $term -> parent){
        echo term_parents($term -> parent,'');   

    //current term
    if(is_object($term) && $term->slug) echo  $delimiter."<a href=\"".get_term_link($term->slug,$taxonomy)."\" title=\"". $term->name ."\" >". $term->name ."</a>";

if ( is_page() ){
    //parent pages
    if ($post -> post_parent){
        page_parents( $post -> post_parent,''); 

    //current page
    echo  $delimiter ."". $post->post_title;

elseif (is_single() && !is_attachment()){ 
    // Get post type
    $post_type = get_post_type();

    if($post_type == 'products' || $post_type == 'portfolio'){

        //current page
        echo  $delimiter."<a href=\"".get_permalink()."\" title=\"". get_the_title() ."\" >". get_the_title() ."</a>";


    //start page

    //get start page
    if ($start_page) get_start_page($start_page);

        $category_id = get_the_category();
        $category_id = $category_id[0]->cat_ID;//only one category can be show in the list  - the first one
        echo $delimiter;
            echo get_category_parents($category_id, TRUE, $delimiter, FALSE );
        echo $post->post_title;

}elseif (is_category()){
    //start page

    //get start page
    if ($start_page) get_start_page($start_page);

        echo $delimiter."".get_category_parents(get_query_var('cat'), TRUE, $delimiter, FALSE);

}elseif (is_tax()){
} else {
    echo  $delimiter."";

echo $after;


Could someone help me?

  • What is on line 58 of the function being mentioned in the error? Somewhere along the lines, something is returning an WP_Error and it is being converted to a string.
    – Welcher
    Commented Jun 12, 2017 at 11:17

1 Answer 1


Line 58 as the error indicates is this line:

$parent_term = get_term_by('ID',$term_id, $taxonomy);
$term_parents = $delimiter."<a href=\"".get_term_link($parent_term->slug,$taxonomy)."\" title=\"". $parent_term->name ."\" >". $parent_term->name ."</a>" .$child_terms;

And our error is:

Object of class WP_Error could not be converted to string

Which means that one of those things being appended is not a "string", but actually a WP_Error object.

I would cast suspicion on get_term_link, which is probably what's returning the error object, which suggests either that the:

  • term doesn't exist
  • the taxonomy isn't registered or valid
  • the values being passed are unexpectedly blank
  • $parent_term isn't a term, but an error object

Which leads us to the lesson here:

Sometimes functions return error objects and you have to check for that, don't just assume it succeeded

How to Check for Errors

Lets take an example that will always fail:

$value = get_term_link( "not a real term","fake taxonomy" );
if ( is_wp_error( $value ) ) {
    // something went wrong

is_wp_error will be true if $value is an error object. Some functions may return false or null, so !empty( $value ) is also a useful check

Error objects can contain error codes and messages, and you can use the get_error_message() method to display them:

$value = get_term_link( "not a real term","fake taxonomy" );
if ( is_wp_error( $value ) ) {
    // something went wrong
    echo $value->get_error_message();

You could also set $value manually to a default value instead

A final note on globals

The code makes use of global variables, but these variables have super generic names such as $taxonomy. Other plugins may also use these, and they can clash. The same is true of function names.


Prefix them:

// term parents function
function sergi_term_parents( $term_id, $child_terms ) {
    global $sergi_taxonomy, $sergi_delimiter;


Use dependency injection and eliminate the globals entirely:

// term parents function
function sergi_term_parents( $term_id, $child_terms, $taxonomy, $delimeter ){

Now your term_parents function will never clash, and works for any taxonomy or delimeter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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