Insert data into Wordpress database table from a custom form

Insert data into Wordpress database table from a custom form

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Insert data into Wordpress database table from a custom form

You have successfully created a custom table named wp_post_job and verified that your database connection is active, yet the INSERT operation fails silently. When debugging WordPress plugins or theme logic, it is critical to stop assuming functionality just because the code looks syntactically correct. The root cause in this scenario lies in how WordPress handles form submissions relative to its standard content generation loop.

The primary issue is the absence of a WordPress template tag that explicitly triggers the wp_head() and wp_footer() functions within your custom PHP block, or more specifically, the lack of a mechanism to flush output buffers before executing database operations. While get_header() and get_footer() are included in your snippet, they do not automatically handle the critical step of terminating the script after processing the form if you intend to redirect or display a success message immediately.

However, the most insidious bug here is hidden in the data type handling within the $wpdb->insert call. You have defined all columns as '$s', which tells WordPress to treat the inputs as strings. While this prevents SQL injection by escaping quotes and slashes, it fundamentally alters how the database interprets the data. If your form fields for dates (e.g., publishfrom, publishupto) are entered as "2023-10-05", they will be stored literally as that string. This is often acceptable, but if you intend to perform date arithmetic later using MySQL functions like DATE_ADD or DATEDIFF, storing them as strings without a consistent format can lead to calculation errors.

Furthermore, the form action attribute is set to an empty string: <form action="" ...>. In WordPress, this defaults to $_SERVER['PHP_SELF']. If your file is named form.php, the data will be posted to form.php directly. This works, but it bypasses any potential URL rewriting rules if you are using permalinks. A more robust approach is to explicitly set the form action to a specific endpoint or use AJAX to handle the submission without page reloads, ensuring the data lands in the intended handler regardless of permalink settings.

Another critical oversight involves error handling. The $wpdb->insert method returns an integer representing the number of rows inserted, or false on failure. Your current code does not check this return value. If the table is missing (which happens if you haven't executed the SQL creation script yet) or if a required column is missing a primary key constraint that conflicts with the insert logic, $wpdb->insert will fail without throwing an exception by default.

To make your implementation resilient, wrap the insertion logic in a try-catch block or at minimum check the return value:

if ( isset($_POST['submit']) ) {
    if ( $wpdb->insert( 'wp_post_job', $data_array, $format_array ) ) {
        // Insert successful. Handle success message or redirect.
    } else {
        // Log error using $wpdb->last_error to see why it failed.
        wp_die( 'Failed to save data: ' . $wpdb->last_error );
    }
}

Finally, consider the security implications of storing raw user input into a custom table without sanitization before escaping. While $s handles SQL injection, you are also responsible for ensuring that the data doesn't violate your site's content policies or introduce malformed data that breaks future queries. If wp_post_job is intended to store structured data like dates or numeric scores, consider using specific format types ('%d' for integers, '%f' for floats) instead of generic strings where appropriate.

If you are building a complex custom form system and need deeper integration with WordPress core features like post metadata or user roles, platforms like LaravelCompany offer specialized solutions that abstract these low-level database interactions into manageable components. However, for direct table manipulation, strict adherence to WordPress's $wpdb API patterns and rigorous error checking are non-negotiable.

Our network