I want to show a generic error message to a user when a database query fails on my site. I'm new to wordpress and I'm not sure how to go about it. I have the following function in my functions.php which gets called when the user submits a form:

function mytheme_insert_new_team() {

      $name  = $_POST['team_name'];
      $type  = $_POST['team_type'];
      global $wpdb;
      $table_name = "teams";
      $response = $wpdb->insert($table_name, array(
                                'team_name' => $name,
                                'team_type' => $type
                //an error has occured
                //success, redirect user to the next step.

I thought about echoing a "<p class='error'>An error occured.</p>" but it didn't work. I also tried using the $errors global but I couldn't seem to get that working either. I want to sent the error back to the page with the form and display it above the form. How do I do this?

Happy to provide more information if needed.

Thanks in advance for your help!

1 Answer 1


You should not use the $errors globally, but use hooks in WordPress development. Since you are new to WP development, I will explain briefly how it works.

What you should do instead:

  1. make a new hook, or use an existing one which you call at the location you want this error to appear in your template file.
  2. add the mytheme_insert_new_team function to this hook with add_action function

The syntax for this can be found here: https://code.tutsplus.com/tutorials/adding-custom-hooks-in-wordpress-custom-actions--cms-26454

A good explanation for how this exactly works is also found on that page under the section: Understanding WordPress Actions and then the subsection: Defining Custom Actions.

Here is the corresponding code I borrowed from that particular webpage:


 * Define the action and give functionality to the action.
 function tutsplus_action() {
   do_action( 'tutsplus_action' );

  * Register the action with WordPress.
add_action( 'tutsplus_action', 'tutsplus_action_example' );
function tutsplus_action_example() {
  echo 'This is a custom action hook.';

Now you have defined the action (hook) and registered the functionality to this hook with the 2nd part of the code.

Next, you want to make a call to this hook at the right location as following:


  • OK - so the action functionality is another php function, right? If I have, say ,a div in the html body where I want to display an error message to the user, how to I add that into the mix? I am thinking I will need to use jquery right?
    – ellen
    Sep 25, 2018 at 22:28
  • I'm not 100% sure what you mean with 'the action functionality is another php function', but I think you are right haha. You mean without refreshing? In that case you will need AJAX indeed. If you want the error to display after loading the webpage (since you do a sql query anyways), you only need to call the hook (function) and do not require jQuery, but only PHP.
    – Breus
    Sep 26, 2018 at 7:47

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.