最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

PHP Catchable fatal error: Object of class WP_Error could not be converted to string

matteradmin8PV0评论

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:

<?php

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

if(!get_option('rttheme_breadcrumb_menus')){
    return false;
}

//Markup 
$before='<div class="breadcrumb">'.wpml_t(THEMESLUG, 'Breadcrumb Menu Text', get_option(THEMESLUG.'_breadcrumb_text')).' ';
$after='</div>';
$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
        );

        woocommerce_breadcrumb($defaults); 
        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
    $get_start_page=get_page($start_page);
    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>";
    }
}

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

    //Find start page and define taxonomy names
    if($taxonomy=="product_categories"){
        $start_page=get_option('rttheme_product_list');
    }elseif($taxonomy=="portfolio_categories"){
        $start_page=get_option('rttheme_portf_page');
    }   

    //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>";
}



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

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

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

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

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

    }else{
    //Categories

    //start page
    $start_page=get_option('rttheme_blog_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;
        if($category_id){
            echo get_category_parents($category_id, TRUE, $delimiter, FALSE );
        }
        echo $post->post_title;
    }

//Category
}elseif (is_category()){
    //start page
    $start_page=get_option('rttheme_blog_page');

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

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

//Taxonomy
}elseif (is_tax()){
    term_links();
} else {
    echo  $delimiter."";
    wp_title('');
}

echo $after;

wp_reset_query(); 
}
?>

Could someone help me?

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:

<?php

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

if(!get_option('rttheme_breadcrumb_menus')){
    return false;
}

//Markup 
$before='<div class="breadcrumb">'.wpml_t(THEMESLUG, 'Breadcrumb Menu Text', get_option(THEMESLUG.'_breadcrumb_text')).' ';
$after='</div>';
$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
        );

        woocommerce_breadcrumb($defaults); 
        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
    $get_start_page=get_page($start_page);
    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>";
    }
}

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

    //Find start page and define taxonomy names
    if($taxonomy=="product_categories"){
        $start_page=get_option('rttheme_product_list');
    }elseif($taxonomy=="portfolio_categories"){
        $start_page=get_option('rttheme_portf_page');
    }   

    //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>";
}



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

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

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

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

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

    }else{
    //Categories

    //start page
    $start_page=get_option('rttheme_blog_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;
        if($category_id){
            echo get_category_parents($category_id, TRUE, $delimiter, FALSE );
        }
        echo $post->post_title;
    }

//Category
}elseif (is_category()){
    //start page
    $start_page=get_option('rttheme_blog_page');

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

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

//Taxonomy
}elseif (is_tax()){
    term_links();
} else {
    echo  $delimiter."";
    wp_title('');
}

echo $after;

wp_reset_query(); 
}
?>

Could someone help me?

Share Improve this question edited Jun 12, 2017 at 11:19 Johansson 15.4k11 gold badges44 silver badges80 bronze badges asked Jun 12, 2017 at 11:02 SergiSergi 631 gold badge1 silver badge3 bronze badges 1
  • 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
Add a comment  | 

1 Answer 1

Reset to default 16

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.

Better

Prefix them:

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

Best

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

Post a comment

comment list (0)

  1. No comments so far