WordPress.org

Ready to get started?Download WordPress

Forums

Fatal error: Object of class stdClass could not be converted to string (2 posts)

  1. ChiefAlchemist
    Member
    Posted 3 years ago #

    Help me out, please. I (think I) fixed my plug-in's bug but I don't quite understand why. I was getting:

    "fatal error: Object of class stdClass could not be converted to string" from this snippet:

    if (preg_match('%POSTID%', $str_GaFormat))  {
       $str_GaFormat = str_ireplace('%POSTID%', $obj_PostID, $str_GaFormat);
    }

    Obviously (now), the $obj_PostID wasn't a integer as it needed to be. So I did this:

    if (preg_match('%POSTID%', $str_GaFormat)) {
      $row_Post = get_post($obj_PostID); // get the post
      $int_PostID = $row_Post->ID;
      $str_GaFormat = str_ireplace('%POSTID%', (string)$int_PostID, $str_GaFormat);
    }

    What's not clear to me is why I was getting an error if the conditional wasn't being met in the first place. In other words, in my test case(s) (preg_match('%POSTID%', $str_GaFormat)) was always false.

    When a plug-in runs is there some sort of pre-processing? I know what you're thinking, "What a noob...". And you're right! :) My programming background goes back to the last 70s but PHP within WP is new to me. I'm learning, and often the hard way. I'd greatly appreciate anyone who might spend a moment educating me a bit on this issue.

    Also, is there an easy (?) way to recognize what this problem is/was? Or is this just something I needed to learn?

    Thanks in advance
    Mark

  2. Are you sure that $str_Gaformat is a string, and not an array or object? Try running a var_dump() before that line, and seeing exactly what it's casted as.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.