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 How is do_action( 'ttm11...table', $create_args); triggered? What's the code around it? – ManzoorWani Commented Oct 3, 2018 at 12:58
  • I am using siteorigin page builder and there is a function called as get_template_variables, it triggers by default. – Alaksandar Jesus Gene Commented Oct 3, 2018 at 14:08
  • The code is incomplete. Please provide the code around the do_action call. – ManzoorWani Commented Oct 3, 2018 at 14:12
  • And how is the actions.php file included? Are you sure it's included only once? – Krzysiek Dróżdż Commented Dec 27, 2018 at 23:40
  • The issue was with Yoast plugin. After disabling Yoast plugin, the code works fine. Since it is out of my scope, i did not go much into it. Thanks to all the supporters – Alaksandar Jesus Gene Commented Jan 2, 2019 at 6:17
Add a comment  | 

1 Answer 1

Reset to default 0

We 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