contact form 7 - cannot run php
Asked Answered
C

3

6

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

To make it a multi-page contact form I have installed Contact Form 7 Multi-Step Forms as well. Everything works fine until yet. Even the mail gets sent.

The problem I am having is, that I want to run some PHP-code before the emails get sent.

I have inserted this code to try the ability of the plugin to run it.

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 of the plugin in an extra php-file.

Now I cannot figure out why the function does not work when inserted in the plugin?

Chatwin answered 6/5, 2014 at 7:49 Comment(6)
have you checked if the table aaaaaaaaaaa already exists?Pabulum
yes, that is what i want to achieve and thats how I know it is not working, because the table does not exist after running the scriptChatwin
try adding or die(mysql_error()); after mysql_query.Pabulum
the problem is i do not even get any responses from the server like this. i do not get anything back because I am runnnig this whole thing in wordpress while submitting a form.Chatwin
Is the parameter $cf7 mandatory? That could be the issue.Porringer
Please read Why shouldnt I use mysql_*Voluble
M
2

wordpress create tabel using this way not in php structure for connect:

More info hear for create, get 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 );
}
Magi answered 14/5, 2014 at 13:13 Comment(0)
O
3

Query database in wordpress using it's global wpdb object like this way

Refer this

function testfunc( $cf7 )
{
    global $wpdb;
    $wpdb->query("CREATE TABLE `aaaaaaaaaaa` ( test VARCHAR(30))");
}

add_action( 'wpcf7_before_send_mail', 'testfunc', 1);
Otorhinolaryngology answered 15/5, 2014 at 19:34 Comment(0)
M
2

wordpress create tabel using this way not in php structure for connect:

More info hear for create, get 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 );
}
Magi answered 14/5, 2014 at 13:13 Comment(0)
D
0

+1 for Rave Patel

I only suggest that you check if the table exists before trying to create it everytime.

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 );
    }
}
Dessiedessma answered 21/5, 2014 at 4:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.