Contact form 7 - unable to start php

I installed WordPress and the contact form 7 plugin in it.

To create a multi-page contact form, I also installed Contact Form 7 Multi-Step Forms . So far, everything is working fine. Even mail is sent.

The problem that I am facing is that I want to run some PHP code before sending sent emails.

I inserted this code to try the possibility of its launch.

 function testfunc( $cf7 ) { mysql_connect("localhost", "user_name", "password") or die(mysql_error()); mysql_select_db("db_name") or die(mysql_error()); mysql_query("CREATE TABLE `aaaaaaaaaaa` ( test VARCHAR(30))"); } add_action( 'wpcf7_before_send_mail', 'testfunc', 1); 

The function even works fine when I run it outside the plugin in an additional php file.

Now I can’t understand why the function does not work when pasted into the plugin?

+6
source share
3 answers

Wordpress creates a tab using this method, and not in the php structure for the connection:

Additional information for creating, receiving table data for wp

 function testfunc( $cf7 ) { global $wpdb; $table_name = $wpdb->prefix . 'tablename'; $sql = "CREATE TABLE IF NOT EXISTS ".$table_name."( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) DEFAULT NULL, UNIQUE KEY id (id) );"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } 
+2
source

Query a database in Wordpress using its global wpdb object this way

Refer to this

 function testfunc( $cf7 ) { global $wpdb; $wpdb->query("CREATE TABLE `aaaaaaaaaaa` ( test VARCHAR(30))"); } add_action( 'wpcf7_before_send_mail', 'testfunc', 1); 
+3
source

+1 for Rave Patel

I suggest you check to see if a table exists before trying to create it every time.

 function testfunc( $cf7 ) { global $wpdb; $table_name = $wpdb->prefix . 'tablename'; if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { $sql = "CREATE TABLE $table_name ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) DEFAULT NULL, UNIQUE KEY id (id) )"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } } 
0
source

All Articles