The dbdelta_insert_queries WordPress PHP filter allows you to modify the SQL queries for inserting or updating data in the database. Queries filterable via this hook contain “INSERT INTO” or “UPDATE”.
Usage
add_filter('dbdelta_insert_queries', 'your_custom_function'); function your_custom_function($iqueries) { // your custom code here return $iqueries; }
Parameters
$iqueries
(string[]): An array of dbDelta insert or update SQL queries.
More information
See WordPress Developer Resources: dbdelta_insert_queries
Examples
Modify a specific table’s INSERT query
add_filter('dbdelta_insert_queries', 'modify_insert_query_for_my_table'); function modify_insert_query_for_my_table($iqueries) { foreach ($iqueries as $key => $query) { if (strpos($query, 'INSERT INTO my_table') !== false) { // Modify the query here $iqueries[$key] = $modified_query; } } return $iqueries; }
Change all INSERT queries to REPLACE queries
add_filter('dbdelta_insert_queries', 'change_insert_to_replace_queries'); function change_insert_to_replace_queries($iqueries) { foreach ($iqueries as $key => $query) { if (strpos($query, 'INSERT INTO') !== false) { $iqueries[$key] = str_replace('INSERT INTO', 'REPLACE INTO', $query); } } return $iqueries; }
Remove a specific UPDATE query
add_filter('dbdelta_insert_queries', 'remove_update_query_for_my_table'); function remove_update_query_for_my_table($iqueries) { foreach ($iqueries as $key => $query) { if (strpos($query, 'UPDATE my_table') !== false) { unset($iqueries[$key]); } } return $iqueries; }
Add a custom INSERT query
add_filter('dbdelta_insert_queries', 'add_custom_insert_query'); function add_custom_insert_query($iqueries) { $custom_query = "INSERT INTO custom_table (column1, column2) VALUES ('value1', 'value2')"; array_push($iqueries, $custom_query); return $iqueries; }
Log all INSERT and UPDATE queries
add_filter('dbdelta_insert_queries', 'log_insert_and_update_queries'); function log_insert_and_update_queries($iqueries) { foreach ($iqueries as $query) { error_log('dbdelta_insert_queries: ' . $query); } return $iqueries; }