admin管理员组文章数量:1202348
mypage.php
$create_args = array(
"user_id" => get_current_user_id(),
"description" => "Your order #".$order_id." has been successful"
);
do_action('ttm11_create_in_notification_table', $create_args);
actions.php
if(!function_exists('ttm11_create_in_notification_table')){
function ttm11_create_in_notification_table($args){
$args['created_at'] = date('Y-m-d H:i:s');
$args['created_by'] = get_current_user_id();
global $wpdb;
$table = $wpdb->prefix.'ttm11_notification';
$insert = $wpdb->insert($table, $args);
}
}
add_action('ttm11_create_in_notification_table', 'ttm11_create_in_notification_table',10,2);
I do not have any duplication of the function.
The same function inserts only one row, if I make an ajax call.
The same is code inserts only once in my localhost but when I move the codes to the server, it inserts thrice.
I have also confirmed by putting var_dump("I am called")
inside the function and is executed only once.
Please let me know the possibilities of this happening.
mypage.php
$create_args = array(
"user_id" => get_current_user_id(),
"description" => "Your order #".$order_id." has been successful"
);
do_action('ttm11_create_in_notification_table', $create_args);
actions.php
if(!function_exists('ttm11_create_in_notification_table')){
function ttm11_create_in_notification_table($args){
$args['created_at'] = date('Y-m-d H:i:s');
$args['created_by'] = get_current_user_id();
global $wpdb;
$table = $wpdb->prefix.'ttm11_notification';
$insert = $wpdb->insert($table, $args);
}
}
add_action('ttm11_create_in_notification_table', 'ttm11_create_in_notification_table',10,2);
I do not have any duplication of the function.
The same function inserts only one row, if I make an ajax call.
The same is code inserts only once in my localhost but when I move the codes to the server, it inserts thrice.
I have also confirmed by putting var_dump("I am called")
inside the function and is executed only once.
Please let me know the possibilities of this happening.
Share Improve this question edited Jul 16, 2019 at 17:31 Castiblanco 2,1947 gold badges20 silver badges26 bronze badges asked Oct 3, 2018 at 8:21 Alaksandar Jesus GeneAlaksandar Jesus Gene 1359 bronze badges 5 |1 Answer
Reset to default 0We had a similar problem here. The reason was that when loading a page in the frontend, WordPress actually was doing two requests:
- One to load the page
- A second one in the background after the page was loaded using
admin-ajax.php
The solution was to make sure, out function was only executed during the "normal" page-load and not the AJAX request:
function my_callback() {
if (defined('DOING_AJAX') && DOING_AJAX) return;
// do normal stuff here
}
本文标签: actionswpdbgtinsert creates duplicate rows
版权声明:本文标题:actions - wpdb->insert creates duplicate rows 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738637788a2104138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
do_action( 'ttm11...table', $create_args);
triggered? What's the code around it? – ManzoorWani Commented Oct 3, 2018 at 12:58do_action
call. – ManzoorWani Commented Oct 3, 2018 at 14:12