contactform7_Wordpress:ContactForm7-将提交的数据保存到自己的数据库中

contactform7

I recently worked on a Wordpress site that utilized the popular ContactForm7 plug-in that was paired with Flamingo to save data to the WordPress Admin panel.

我最近在一个Wordpress网站上工作,该网站使用了流行的ContactForm7插件,该插件与Flamingo配对将数据保存到WordPress Admin面板。

If you are happy with submitted data going directly to Wordpress where you can export manually, you're good at this point if you just install Flamingo. However, my task was to integrate submitted data to a custom CRM database.  

如果您对直接提交到Wordpress并可以手动导出的提交数据感到满意,那么只要安装Flamingo,就可以了。 但是,我的任务是将提交的数据集成到自定义CRM数据库中。

I did some searching and found there are others looking to do the same. Unfortunately, Google points to some solutions at StackOverflow which all seemed to be downvoted or closed as off-topic. 

我进行了一些搜索,发现还有其他人希望这样做。 不幸的是,Google指出了StackOverflow上的一些解决方案,这些解决方案似乎都被否决了,或者被关闭了。

Need to do redirections in functions.php and Contact Form 7 get_posted_data(); is not returning posted values are a couple of examples. Hopefully, others will find this article useful and can create their own questions based on the information here.

需要在functions.php联系表7中 进行重定向 get_posted_data(); 不返回已发布的值是两个示例。 希望其他人会发现本文有用,并可以根据此处的信息提出自己的问题。

My Solution

我的解决方案

I am using a child theme that contains both functions.php and style.css in the theme-child folder. Some of the big all in one themes generate their own child theme and as an extra precaution for files being updated, I create a new folder inside my WordPress folder called CustomFunctions. Inside that folder I have two files for custom.config.php and custom.functions.php

我正在使用在theme-child文件夹中同时包含function.php和style.css的子主题。 一些大的多合一主题会生成自己的子主题,并且为了防止文件被更新,我在WordPress文件夹中创建了一个名为CustomFunctions的新文件夹。 在该文件夹中,我有两个文件用于custom.config.php和custom.functions.php

My ContactForm7 has the following code 我的ContactForm7具有以下代码
<label> Your Name (required)
[text* your-name] </label>
<label> Your Email (required)
[email* your-email] </label>

<label> Your Message
[textarea your-message] </label>

[recaptcha size:compact]

[submit "Send"] 

If you were to view the source of the rendered fields, it would look something like

如果您要查看渲染字段的源,它看起来像

<input type="text" name="your-name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false"> 

You can see that  [text* your-name] equates to the field name =”your-name". The field names I will capture will be “your-name”, “your-email” and “your-message”. In addition, ContactForm7 is capturing the IP, the URL from where the form is submitted and the user agent (browser information) which I am also going to capture.

您会看到[text * your-name]等于字段名称=“您的名称”。我将捕获的字段名称将是“您的名称”,“您的电子邮件”和“您的消息”。另外,ContactForm7正在捕获IP,提交表单的URL和我还将要捕获的用户代理(浏览器信息)。

First thing I have is my own custom config file that will contain my database credentials and anything else I might need to set up.

我首先要拥有的是我自己的自定义配置文件,其中将包含数据库凭据以及可能需要设置的其他内容。

custom.config.php

custom.config.php

<?php

date_default_timezone_set('America/Chicago');

// **************** database *************

$db_host = "localhost";

$db_name = "the_db_name";

$db_user = "db_user_name";

$db_word = "db_password";

Next is the function that will save data to our database as well as anything else you may want to do. For simplicity, I am saving to the database only.

接下来是将数据以及您可能要执行的其他任何操作保存到我们的数据库的功能。 为简单起见,我仅保存到数据库。

custom.functions.php

custom.functions.php

<?php

include_once 'custom.config.php';

if (!$dbCustom = new PDO("sqlsrv:Server=localhost;Database=$db_name", $db_user , $db_word))

{

die("0,There is a problem, please call the administrator");

}

function addLead($name,$email,$comment,$remoteIP,$url,$userAgent){

global $dbCustom;

$stmt = $dbCustom->prepare('INSERT INTO dbo.tWebLeads (name,email,Comment,RemoteIP,

URL, UserAgent)

VALUES(:your-name, :your-email,:your-message,:remoteip,:url,:userAgent)');

$stmt->bindValue(':your-name', $name, PDO::PARAM_STR);

$stmt->bindValue(':your-email',  $email,  PDO::PARAM_STR);

$stmt->bindValue(':your-message',  $comment,  PDO::PARAM_STR);

$stmt->bindValue(':remoteip',  $remoteIP,  PDO::PARAM_STR);

$stmt->bindValue(':url',  $url,  PDO::PARAM_STR);

$stmt->bindValue(':userAgent',  $userAgent,  PDO::PARAM_STR);

$stmt->execute();

//print_r($dbCustom->errorInfo());  //TESTING ONLY

$stmt = NULL;

Last, in the child theme functions.php I am capturing the data and sending to my addLead function.

最后,在子主题functions.php中,我正在捕获数据并将其发送到我的addLead函数。

Child theme functions.php

子主题functions.php

<?php

include_once $_SERVER["DOCUMENT_ROOT"].'/customfunctions/custom.functions.php';

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

function my_theme_enqueue_styles() {

wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );

}

add_action('wpcf7_before_send_mail', 'save_form' );

function save_form( $wpcf7 ) {

global $dbCustom;

$submission = WPCF7_Submission::get_instance();

if ( $submission ) {

// If you want to see all the values of $submission, you can add the two lines below. Just make sure you remove for production. To view the result, you will have to use your browser console network tab as this is an ajax request.

// print_r($submission);

//die();        

$submited = array();

$submited['posted_data'] = $submission->get_posted_data();

}

$name = $submited['posted_data']['your-name'];

$email = $submited['posted_data']['your-email'];

$comment = $submited['posted_data']['your-message'];

$remoteIP = $submission->get_meta('remote_ip');

$url = $submission->get_meta('url');  // Helpful when you have the same form on multiple pages

$userAgent = $submission->get_meta('user_agent');

// call addLead function from the custom.functions.php

addLead($name,$email,$comment,$remoteIP,$url,$userAgent);

// send our own custom message to ourself

wp_mail( 'you@example.com', 'new contact', 'there is an new contact in your database');

};

Please let me know your comments below.

请在下面让我知道您的评论。

翻译自: https://www.experts-exchange.com/articles/31767/Wordpress-ContactForm7-Save-Submitted-Data-To-Your-Own-Database.html

contactform7

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值