I'm trying to write a little script within my footer.php (which I'll later transform into a plugin) that send two form fields to a custom table (wp_newsletter). I'm already sending the form and writing to the table correctly, but I don't know how I can send a success or fail message back to the user. My current code is as follows:

<form method="post">
  <input type="text" name="user_name">Name
  <input type="text" name="user_email">Email
  <input type="submit">
  <?php echo $message;  ?>

global $wpdb;
$table   = $wpdb->prefix . "newsletter";
$name    = sanitize_text_field( $_POST["user_name"] );
$email   = sanitize_email( $_POST["user_email"] );
$message = "";

if( isset($_POST["submit"]) ) {
  if ( is_email($email) && isset($name)) {
    if ( $wpdb->insert( $table, array("name"  => $name, "email" => $email)) != false ) {
      $message = "Your subscription was sent.";
  else {
    if ( !is_email($email) ) {
        $message = "Invalid email address.";
    } elseif ( !isset($name) ) {
        $message = "The field name is mandatory.";
    } else {
        $message = "Both name and email fields are mandatory.";
} else {
    $message = "Please, try again later.";

<?php wp_footer(); ?>


I (think) am testing it right, accordingly to the $wpdb docs which says that:

This function returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1).

  • Where did you want the message to appear? You have your echo $message before the $message is created. Commented Jul 20, 2016 at 19:22
  • Hm, I was not sure if I could used it before declaration... But I wanted to show it under the form.
    – vcamargo
    Commented Jul 20, 2016 at 20:03
  • 1
    use this !== FALSE and try
    – user93819
    Commented Jul 21, 2016 at 2:24

3 Answers 3


It returns either the number of rows inserted or false on error.

Maybe you can get id of inserted recode or false if the insert fails:

refer link: https://developer.wordpress.org/reference/classes/wpdb/insert/#return

So you can check like below:

$result_check = $wpdb->insert( $table, array("name"  => $name, "email" => $email));
   //successfully inserted.
  //something gone wrong

When I realized that PHP is an acronym for "PHP Hypertext Preprocessor" -- emphasis on "preprocessor" -- I finally understood that I can't mix PHP and HTML and expect any kind of interactivity with the user. When a web page is served, the PHP creates the HTML and then the HTML is displayed by the browser. If the user does something the PHP must respond to, it must trigger a new PHP/HTML page to provide some sort of response. (An alternate would be to use AJAX to send data back and forth without loading a new page. Wordpress works well with AJAX and there are tutorials a google search away.)

For a simple form like yours, I would use javascript for error checking. If the form is not filled in, prevent form submission with JS. If the form is complete, the form's action can be a .php file that does the database insert and displays the success/failure message in HTML.

  • Thanks for the tip, scott. How could I create such .php file that would display the success/failure message to the user?
    – vcamargo
    Commented Jul 21, 2016 at 0:52
  • Sorry for the late reply. In a web environment, I would have the form's action be a .php file. The data is sent via GET to the target file. The data is then parsed and inserted into the DB. Then the .php file outputs HTML into a new web document that indicates success or failure. Note this does not give the user much opportunity to correct errors to achieve a better result once the new web page is loaded.
    – scott
    Commented Jul 25, 2016 at 16:38
  • In Wordpress, however, I would consider trying AJAX. There are many good tutorials, and I would look in the Codex too, but here are two that worked for me: smashingmagazine.com/2011/10/how-to-use-ajax-in-wordpress and premium.wpmudev.org/blog/using-ajax-with-wordpress AJAX allows DB access without loading a new web page. So you would have an element that has no HTML content until the AJAX returned with success/failure: document.getElementById("results-div").innerHTML = content.result;
    – scott
    Commented Jul 25, 2016 at 16:42
  • The logic that handles the form is inside the same file of the form: the footer.php. I'm just trying to make it work this way, then I can separate it better when I'm done with the tests. Despite this, I don't want to send my form's data through a GET.
    – vcamargo
    Commented Jul 25, 2016 at 20:09
  • My mistake. You can use POST. It sounds like you want to do AJAX inside footer.php so it doesn't trigger a page reload. You will have to use javascript to send/receive the AJAX data, but you can create a new .php file to handle the AJAX call. (I put my .php files inside a plugin so all the dependencies are handled by the WP system.)
    – scott
    Commented Jul 25, 2016 at 20:13

I came here via google and what helped me was using properties of $wpdb object:


shows the last error if present


shows the last query which gave the above error

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.