Common causes
- A WordPress function returned a WP_Error object on failure and the code echoed it
- Echoing a DateTime/DateTimeImmutable object instead of calling ->format()
- Concatenating a database row object or decoded JSON object into a string
- Passing an object to string functions like strlen(), str_replace() or htmlspecialchars()
- Using an object as an array key or in string interpolation
How to fix it
- Identify the object and its class. The message names the class. On the reported line, find which variable is being echoed or concatenated.
- Handle WP_Error results. Check is_wp_error($result) before using it, then show $result->get_error_message() if you need the text.
- Format dates explicitly. Use $date->format('Y-m-d H:i') rather than echo $date.
- Output the property you need. For data objects, echo the specific field: $user->name or $row->title.
- Add __toString() for your own classes. If an object has a natural string form, implement public function __toString(): string so it can be echoed safely.
- Debug-print objects properly. For inspection use print_r($obj, true), var_export() or json_encode($obj), never string concatenation.
PHP / WordPress
$result = wp_insert_post($data, true);
if (is_wp_error($result)) {
error_log($result->get_error_message());
} else {
echo 'Created post ' . (int) $result;
}
echo $date->format('Y-m-d'); How to stop it happening again
- Check for error objects from functions that can return them
- Type-hint string parameters so mistakes fail early with clear messages
- Implement __toString() on value objects meant for display