Asked 7 years ago
10 Jun 2017
Views 1312
Phpworker

Phpworker posted

how catch error in wordpress , how WP_Error work ?

making wordpress plugin and finding class WP_Error at class reference wordpress.org


    return new WP_Error( 'broke', __( "I've fall", "my_textdomain" ) );


so we can add more and simply put all error message display at one place in single page
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

one can initialize with WP_Error() with construct without any argument

$error= new WP_Error();


and can add as much Error messages with same error code

if($phone_no==''){
		$error->add( 'broke', __( "Please enter valid Phone No.", "my_textdomain" ),"tech_error" );

	}



 if($email==''){
		$error->add( 'broke', __( "Please enter valid Email id", "my_textdomain" ),"tech_error" );

	}


display Error Message ::

if(isset($error->errors['broke'])){
	 if ( is_wp_error( $error ) ) {
		 $error_string = $error->get_error_message();
		 echo '<div id="message" class="error"><p style="	color:red">' . $error_string . '</p></div>';
	}
}
Post Answer