在Wordpress中提交表单后,在后台运行功能

问题描述

我具有使用Ajax提交表单的功能。提交后,我得到了答复。

function myform() {
   //run script here

   // get the results and show in the console that the form submission was successful 
   echo json_encode($results);

   // Now call another function to process a script in the background after response
   my_background_function();


}

现在的问题是,如果提交表单,则仅在完成功能my_background_function()后才会显示响应。

成功提交表单后如何运行“ my_background_function()”?

一个功能中的整个代码是在mailchimp中创建组,并在databade中创建自定义数据表

// Import Subscriber batch
function maybe_insert_new_interest_groups() {
    //upload_file_callback();

    // Get Mailchimp Details
    $mcApi = $GLOBALS['mcApi'];
    $mcList = $GLOBALS['mcList'];
    $mcDC = $GLOBALS['mcDC'];
    $mcFolder = $GLOBALS['mcFolder'];
    
    // Get current user
    $current_user = $GLOBALS['current_user'];


    // get entered form data
    parse_str( $_POST['form_data'],$form_data );
    $postarr = array();
    $postarr = array_merge( $postarr,$form_data );

    $InterestCategoryID = get_option(LUBUVNA_PREFIX.'mc_interest_category');
    $groups = $form_data[LUBUVNA_PREFIX.'mc_interest_groups'];

    if ( !empty($groups) && !empty($InterestCategoryID) ){

        $addGroups = 'https://'. $mcDC . '.api.mailchimp.com/3.0/lists/' . $mcList . '/interest-categories' . '/' . $InterestCategoryID . '/interests';

        $lubuvnaIDstest = explode(",",$groups);

        $dataToUpdate = '';
        foreach($lubuvnaIDstest as $row) {
            
            $dataToUpdate = array (
                'name' => $row
            ); 


            $dataToUpdate = json_encode($dataToUpdate);
            $curl = curl_init();
            curl_setopt_array($curl,array(    
                CURLOPT_URL => $addGroups,CURLOPT_RETURNTRANSFER => true,CURLOPT_TIMEOUT => 30,CURLOPT_CUSTomrEQUEST => "POST",CURLOPT_POSTFIELDS => $dataToUpdate,CURLOPT_HTTPHEADER => array(
                    "authorization: apikey $mcApi"
                ),));
    
            $addedGroups = curl_exec($curl);
            $errorGroups = curl_error($curl);
            curl_close($curl);

        }

        $json = json_encode($addedGroups);
        $array = json_decode($addedGroups);


        $optionGroups = update_option( LUBUVNA_PREFIX.'mc_interest_groups',$groups );


    }


    
    global $wpdb;

    // needed for creating database table
    $charset_collate = $wpdb->get_charset_collate();
    // Table name
    $table_name = $wpdb->prefix . "lubuvna_subscribers";



    $sqlDrop = "DROP TABLE IF EXISTS $table_name";
    $wpdb->query($sqlDrop);



    // Get group names values - replace space with '_' and lowercase
    $listsValuesDel = get_option(LUBUVNA_PREFIX.'mc_interest_groups');
    $journalName = preg_replace('/\s+/','_',$listsValuesDel);
    $final_group_name = strtolower($journalName);
    $lubuvnaListsDel = explode(',',$final_group_name);
    $destinationsDel = '';
    
    // foreach value filter and output
    foreach($lubuvnaListsDel as $row){

        $destinationsDel .= 'gr_'."$row".' varchar(255),';
    }

    // create the table if it doesn't exist
    if($wpdb->get_var("show tables like '$table_name'") != $table_name) {

        $sql = "CREATE TABLE `{$wpdb->base_prefix}lubuvna_subscribers` (
        ID bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,first_name varchar(255),last_name varchar(255),email varchar(255),phone varchar(255),birthday varchar(255),gender varchar(255),customer_type varchar(255),id_company_number varchar(255),street_address varchar(255),address_line_2 varchar(255),city varchar(255),state_area varchar(255),zip varchar(255),customer_from varchar(255),$destinationsDel
        last_visit varchar(255),send_sms varchar(255),send_email varchar(255),join_date varchar(255),post_author bigint(20),post_date DATETIME DEFAULT CURRENT_TIMESTAMP,post_modified DATETIME DEFAULT CURRENT_TIMESTAMP,post_status varchar(20) DEFAULT 'publish',PRIMARY KEY  (ID)
        ) $charset_collate;";

        require_once( ABSPATH . 'wp-admin/includes/upgrade.PHP' );
        dbDelta( $sql );
        $success = empty( $wpdb->last_error );

        $groupNames = array();
        // foreach group response in array
        foreach($lubuvnaListsDel as $row){
            $groupNames[] = $row;
        }

        // Response data
        $callback_database['status'] = '200';
        $callback_database['title'] = __("Table created","translate-newsletter");
        $callback_database['detail'] = __("Datatable and all groups were created successfully","translate-newsletter");
        $callback_database['groups'] = $groupNames;

    } else {

        $callback_database['status'] = '400';
        $callback_database['title'] = __("Table wans't created","translate-newsletter");

        if($wpdb->get_var("show tables like '$table_name'") == $table_name) {
        
            $callback_database['errors'][]['message'] = __("Datatable exists,thats why it wasn't created","translate-newsletter");
        
        } else {
        
            $callback_database['errors'][]['message'] = __("the Datatable wasn't created,contact page administrator","translate-newsletter");
        
        }

    }
    

    $callback_database_results = json_encode($callback_database,true);

    $response1 = json_decode($addedGroups,true);
    $response2 = json_decode($callback_database_results,true);
    $mergedResponses = array_merge( $response1,$response2 );

    $responseResults = json_encode($mergedResponses);


    echo $responseResults;
    exit(maybe_insert_new_merged_fields());
   
}
add_action( 'wp_ajax_new_interest_groups','maybe_insert_new_interest_groups' );



// the function that should run after the first function
function maybe_insert_new_merged_fields() {
   // some stuff here
}
//add_action( 'wp_ajax_new_merged_fields','maybe_insert_new_merged_fields' );

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)