1

I am using WordPress, I have created a custom form. I have added the form code in the function.php and added the shortcode on the page.

Then I also created process.php in the theme folder and added the below code. I am able to insert the data in the table. There is no issue here.

<?php

define('BLOCK_LOAD', true);
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );
$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
global $wpdb;        // <--- making $wpdb as global



if(isset($_POST['submit'])){
    
    $data = $_POST;

    $parentsdata= array(
            'name' => sanitize_text_field($data['parentsname']),
            'mobile' => sanitize_text_field($data['mobileno']),
            'email' => sanitize_text_field($data['email']),
            'agent_email' => sanitize_text_field($data['agent_email'])
        );
    
    
    $query=$wpdb->insert('wp_parent',$parentsdata,array('%s','%s','%s','%s'));
    $lastid = $wpdb->insert_id;
    
    $childdata= array(
            'p_id' => $lastid,
            'childname' => sanitize_text_field($data['childname']),
            'childage' => sanitize_text_field($data['childage']),
            'chldschoolname' => sanitize_text_field($data['nameofschool']),
            'childgrade' => sanitize_text_field($data['childgread']),
        );
    
 if ($query) {
     $query=$wpdb->insert('wp_child',$childdata,array('%s','%s','%s','%s','%s'));
    $response['error'] = "true";
     header('Location: /thank-you');
     exit();

    }else{
    $response['error'] = "false";
    } 
       
}

?>

What I am doing is, I have to send the email to the user as well as the admin. I know using SMTP code or wp_mail I can send it but there are more changes to email going to spam.

I have installed the WordPress plugin called WP Mail SMTP by WPForms. Now my issue is how to connect the custom code with this plugin or any other plugin?

user9437856
  • 2,360
  • 2
  • 33
  • 92
  • Your problem is that your emails are going to spam ? – Anatole Dec 30 '21 at 08:26
  • @Anatole, Yes, So my colleague suggested use an SMTP plugin. – user9437856 Dec 30 '21 at 08:29
  • 1
    Well if you are using a plugin for your email, why don't you use a plugin for your form too ? It will be easier I guess ! But if your emails are going to spam, most of the time it's because you need to add a DKIM signature etc... – Anatole Dec 30 '21 at 08:48
  • Because my senior want more customisation over the form – user9437856 Dec 30 '21 at 08:56
  • It sounds like the database portion of your code is working fine, so I don't see a reason to post it here. However, WordPress 3.4 removed `BLOCK_LOAD`, and instead you should look into `SHORTINIT`. You should also avoid creating a dedicated WPDB object, use the built-in one. Doing that, plugins will still load and you can use `wp_mail` normally, and that plugin will take care of the rest. (Except possibly your spam problem, because that isn't necessarily a WordPress problem, but an SMTP/email one.) – Chris Haas Dec 30 '21 at 15:15
  • 1
    @ChrisHaas, "You should also avoid creating a dedicated WPDB object," can explain me this? Any link where I can read this? – user9437856 Dec 31 '21 at 09:32
  • https://stackoverflow.com/a/25158840/231316 – Chris Haas Dec 31 '21 at 14:51

0 Answers0