I've got about 6 subdomains that have a "contact us" link and I'm sending all these links to a single form that uses "Contact Form 7". I add ?from=site-name to each of the links so that I can set a $referredFrom variable in the contact form.
The only two things I'm missing are (1) the ability to insert this referredFrom variable into the email that I get whenever someone submits the form and (2) The ability to redirect the user back to the site they came from (stored in $referredFrom)
Any ideas?
Here's a bit of code from includes/classes.php that I thought might be part of the email insert but its not doing much...
function mail() {
global $referrer;
$refferedfrom = $referrer; //HERE IS MY CUSTOM CODE
$fes = $this->form_scan_shortcode();
foreach ( $fes as $fe ) {
$name = $fe['name'];
$pipes = $fe['pipes'];
if ( empty( $name ) )
continue;
$value = $_POST[$name];
if ( WPCF7_USE_PIPE && is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
if ( is_array( $value) ) {
$new_value = array();
foreach ( $value as $v ) {
$new_value[] = $pipes->do_pipe( $v );
}
$value = $new_value;
} else {
$value = $pipes->do_pipe( $value );
}
}
$this->posted_data[$name] = $value;
$this->posted_data[$refferedfrom] = $referrer; //HERE IS MY CUSTOM CODE
}
I'm also thinking that I could insert the referredFrom code somewhere in this function as well...
function compose_and_send_mail( $mail_template ) {
$regex = '/\[\s*([a-zA-Z][0-9a-zA-Z:._-]*)\s*\]/';
$callback = array( &$this, 'mail_callback' );
$mail_subject = preg_replace_callback( $regex, $callback, $mail_template['subject'] );
$mail_sender = preg_replace_callback( $regex, $callback, $mail_template['sender'] );
$mail_body = preg_replace_callback( $regex, $callback, $mail_template['body'] );
$mail_recipient = preg_replace_callback( $regex, $callback, $mail_template['recipient'] );
$mail_headers = "From: $mail_sender\n";
if ( $mail_template['use_html'] )
$mail_headers .= "Content-Type: text/html\n";
$mail_additional_headers = preg_replace_callback( $regex, $callback,
$mail_template['additional_headers'] );
$mail_headers .= trim( $mail_additional_headers ) . "\n";
if ( $this->uploaded_files ) {
$for_this_mail = array();
foreach ( $this->uploaded_files as $name => $path ) {
if ( false === strpos( $mail_template['attachments'], "[${name}]" ) )
continue;
$for_this_mail[] = $path;
}
return #wp_mail( $mail_recipient, $mail_subject, $mail_body, $mail_headers,
$for_this_mail );
} else {
return #wp_mail( $mail_recipient, $mail_subject, $mail_body, $mail_headers );
}
}
I'd found a plugin that works fantastic for doing this, plus a little more:
http://wordpress.org/plugins/contact-form-7-leads-tracking/
Which will add all the information to the end of your email when it is sent
First of all, in order to get the from variable you'll have to insert
$referrer = $_GET['from'];
somewhere in the top script, at least before the last line you inserted.
Additionally, in the second script you have to add the value to $mail_body somehow, but since I don't know how that value is made up I can't help much with that.
Is the code for this form available online somewhere?
Insert in your functions.php or create a simple plugin...
1.
function custom_wpcf7_special_mail_tag( $output, $name ) {
if ( 'from' == $name ) {
$referredFrom = ( isset($_GET["from"]) && !empty($_GET["from"]) ) ? $_GET["from"] : '';
$output = $referredFrom;
}
return $output;
}
add_filter( 'wpcf7_special_mail_tags', 'custom_wpcf7_special_mail_tag', 10, 2 );
Use the [from] tag in your email template.
2.
function add_custom_js_cf7() {
$referredFrom = ( isset($_GET["from"]) && !empty($_GET["from"]) ) ? $_GET["from"] : '';
if ( $referredFrom ) {
?>
<script type="text/javascript">
var from = "<?php echo $referredFrom; ?>";
</script>
<?php }
}
add_action( 'wpcf7_enqueue_scripts', 'add_custom_js_cf7' );
And add this line to the "additional settings" in your form settings:
on_sent_ok: "location = from;"
http://contactform7.com/blog/2010/03/27/redirecting-to-another-url-after-submissions/
You can also use global $referredFrom; if you declared it somewhere.
Related
I am trying to access php variable from wordpress shortcode function and use into og filter.
I have tried with php superglobals, set transient also, but nothing give the needed result.
Can someone take a look and help, thanks!
That's my code:
add_shortcode('test', 'get_sheet_value_shortcode');
function get_sheet_value_shortcode() {
ob_start();
$API = get_option('google_sheet_api');
$google_spreadsheet_ID = get_option('google_sheet_id');
$LANG = get_language_shortcode();
$api_key = esc_attr( $API);
$get_cell = new WP_Http();
$cell_url = "https://sheets.googleapis.com/v4/spreadsheets/$google_spreadsheet_ID/values/$LANG!A1:Z1000/?majorDimension=ROWS&key=$api_key";
$cell_response = $get_cell -> get( $cell_url);
if ( empty( $cell_response) ) {
return;
}
$json_body = json_decode($cell_response['body'],true);
$values = $json_body['values'];
//$values = json_encode($values);
array_shift($values); // removes first column key
if (! empty($values) ) {
$count = count($values);
// this will be using for share f-nality, so we can get same quote
$quote_number = array_search($rand_values, $values);
$rand_values = $values[array_rand($values)];
$image = (! empty($rand_values[3]) ) ? $rand_values[3] : '';
$GLOBALS['ogimage'] = $image;
}
ob_get_clean();
}
add_filter( 'aioseo_facebook_tags2', 'aioseo_filter_facebook_title2', 99 );
function aioseo_filter_facebook_title2( $facebookMeta ) {
$image = $GLOBALS['ogimage'];
if ( is_page('test') ) {
$facebookMeta['og:image'] = $image;
}
return $facebookMeta;
}
can someone help me a bit with PhpMailer form? I'm not a php dev and i'm a bit lost, mostly because I have no idea how to debug it.
Tips how to debug such scripts are very welcome! (I don't know how to do it in localhost and I host it in a web shared host so I cannot ssh the server)
This is the script:
I have a multiple step form from Frontend which has also recaptcha, so the script include validation of different form steps and recaptcha validation.
<?php
require_once("/PHPMailer/PHPMailer.php");
use PHPMailer\PHPMailer\PHPMailer;
$t_mailer = new PHPMailer;
$t_mailer->SMTPAuth = true;
$t_mailer->Username = "myemail#gmail.com"; // gmail username
$t_mailer->Password = "****"; //gmail password
$t_mailer->SMTPSecure = 'tls';
$t_mailer->Port = 587;
$t_mailer->setFrom("myemail#gmail.com", "Name for the owner of the Account");
//$t_mailer->addAddress("myemail#gmail.com", "Name for who is being sent the email.");
$t_mailer->Subject = "Project request from ECA";
$t_mailer->Body = "This will be the message body that is sent.";
// $recipient = 'myemail#gmail.com'; // Enter the recipient's email address here.
// $subject = 'Project request from ECA'; // Enter the subject of the email here.
$success = 'Your message was sent successful. Thanks.';
$error = 'Sorry. We were unable to send your message.';
$invalid = 'Validation errors occurred. Please confirm the fields and submit it again.';
if ( ! empty( $_POST ) ) {
require_once('recaptcha.php');
if( isset( $_POST['email'] ) ) {
$from = filter_var( $_POST['email'], FILTER_VALIDATE_EMAIL );
} else {
$from = null;
}
if( isset( $_POST['step'] ) ) {
$step = $_POST['step'];
} else {
$step = 'send';
}
if ( ! empty( $_POST['reCAPTCHA'] ) ) {
if ( ! empty( $reCAPTCHA['success'] ) ) {
$errCaptcha = '';
} else {
$errCaptcha = true;
}
} else {
$errCaptcha = '';
}
$errFields = array();
foreach( $_POST as $key => $value ) {
if ( $key != 'section' && $key != 'reCAPTCHA' ) {
if ( $key == 'email' ) {
$validation = filter_var( $_POST[$key], FILTER_VALIDATE_EMAIL );
} else {
$validation = ! empty( $_POST[$key] );
}
if ( ! $validation ) {
$errFields[$key] = true;
}
}
}
if ( empty( $errCaptcha ) && count( $errFields ) === 0 && $step === 'send' ) {
$header = "From: " . $from . " <" . $from . ">" . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$body = '<table style="padding: 35px; background-color: #f5f5f5"; font-family: Roboto, sans-serif; font-size: 1rem; text-align: left; border-radius: 4px>';
$body .= '<tr><th style="font-size: 1.5rem; font-weight: 600; color: #1E50BC">'.$subject.'</th></tr>';
$body .= '<tr></td>';
foreach( $_POST as $key => $value ) {
if ( $key != 'section' && $key != 'reCAPTCHA' ) {
$body .= '<p><b>' . str_replace( '-', ' ', ucfirst( $key ) ) . '</b>: ' . $value . '</p>';
}
}
$body .= '</td></tr>';
$body .= '</table>';
$t_mailer->Body = $body;
$t_mailer->addAddress($from, 'who send the email');
// $mail = mail( $recipient, $subject, $body, $header );
// $mail
if ( $t_mailer->send() ) {
$response = array(
'status' => 'success',
'info' => $success
);
print_r( json_encode( $response ) );
} else {
$response = array(
'status' => 'fail',
'info' => $error
);
print_r( json_encode( $response ) );
}
} else {
$response = array(
'status' => 'invalid',
'info' => $invalid,
'captcha' => $errCaptcha,
'fields' => $errFields,
'errors' => count( $errFields )
);
print_r( json_encode( $response ) );
}
exit;
}
I'm sure PhpMailer works with my host and my server because I tried a very simple script that send an email from root and it worked. (I got the email in my gmail inbox)
But This script is in another folder, not root, even tho they all have same folder/files permission (not sure if is relevant, but better specify!)
Several things not quite right here.
When you're using PHPMailer without composer, you need to load all the classes it needs yourself, so add:
require_once '/PHPMailer/SMTP.php';
require_once '/PHPMailer/Exception.php';
You are setting some SMTP config properties, but not actually telling PHPMailer to use SMTP, so add this:
$t_mailer->isSMTP();
You're sending from a gmail address, which means you must send through gmail's servers (or you will fail SPF checks), but you have not specified a server to send through, so set this:
$t_mailer->Host = 'smtp.gmail.com';
Your content is in HTML, but you're not asking PHPMailer to send it as HTML:
$t_mailer->isHTML();
You don't need any of that stuff you're doing with $header; PHPMailer does all that for you.
Rather than going any further, I'd recommend basing your code on the gmail example provided with PHPMailer, and if you run into any other issues, refer to the PHPMailer troubleshooting guide and search on here.
I would like to check a custom form against spam using Akismet.
I searched the web and the only simple way I found is this (see code excerpt below): http://www.binarymoon.co.uk/2010/03/akismet-plugin-theme-stop-spam-dead/. Unfortunately $isSpam returns true!
Does anyone know how to do the magic? I appreciate your help.
function akismet_isSpam ($content) {
// innocent until proven guilty
$isSpam = FALSE;
$content = (array) $content;
if (function_exists('akismet_init')) {
$wpcom_api_key = get_option('wordpress_api_key');
if (!empty($wpcom_api_key)) {
global $akismet_api_host, $akismet_api_port;
// set remaining required values for akismet api
$content['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
$content['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
$content['referrer'] = $_SERVER['HTTP_REFERER'];
$content['blog'] = get_option('home');
if (empty($content['referrer'])) {
$content['referrer'] = get_permalink();
}
$queryString = '';
foreach ($content as $key => $data) {
if (!empty($data)) {
$queryString .= $key . '=' . urlencode(stripslashes($data)) . '&';
}
}
$response = akismet_http_post($queryString, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
smart_dump($response, true);
if ($response[1] == 'true') {
update_option('akismet_spam_count', get_option('akismet_spam_count') + 1);
$isSpam = TRUE;
}
}
}
return $isSpam;
}
First , you can assume akismet is installed and API key verified, that will allow you to use the akismet_http_post function directly to post the data to servers ..
// Like above mentioned, We assume that :
// Akismet is installed with the corresponding API key
if( function_exists( 'akismet_http_post' ) )
{
global $akismet_api_host, $akismet_api_port;
// data to be delivered to Akismet (This is what you need Modify this to your needs)
$data = array(
'comment_author' => 'Spammer Spammy',
'comment_author_email' => 'spammy#spamserver.com',
'comment_author_url' => 'spamming.com',
'comment_content' => 'my Spam',
'user_ip' => '99.99.99.99',
'user_agent' => '',
'referrer' => '',
'blog' => 'http://example.com',
'blog_lang' => 'en_US',
'blog_charset' => 'UTF-8',
'permalink' => 'http://example.com/my-link',
'is_test' => TRUE,
);
// Now we need to construct the query string
$query_string = http_build_query( $data );
// and then post it to Akismet
$response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
// ..and check the results
$result = ( is_array( $response ) && isset( $response[1] ) ) ? $response[1] : 'false';
// display the result ( 'true', 'false' or any error message you want )
printf( 'Spam check according to Akismet :%s', $result );
}
This was not thoroughly tested , but should work..
Note that the blog, user_ip and user_agent parameters are required for the akismet_http_post function.
Also, it would be nice to mention that Akismet has libraries and good API documents .
i using a wordpress plugin, i notice that returns a error on
$alias = (string)end(array_keys($settings));
above line .the error is
PHP Strict Standards: Only variables should be passed by reference in on wordpress function
i added that function below. anyone know how to solve that error please, becoz admin dashboard of the plugin not loading because of this error.
/*
* GET modules lists
*/
function load_modules ()
{
$folder_path = $this->cfg['paths']['plugin_dir_path'] . 'modules/';
$cfgFileName = 'config.php';
// static usage, modules menu order
$menu_order = array();
foreach(glob($folder_path . '*/' . $cfgFileName) as $module_config ){
$module_folder = str_replace($cfgFileName, '', $module_config);
// Turn on output buffering
ob_start();
if( is_file( $module_config ) ) {
require_once( $module_config );
}
$settings = ob_get_clean(); //copy current buffer contents into $message variable and delete current output buffer
if(trim($settings) != "") {
$settings = json_decode($settings, true);
$alias = (string) end(array_keys($settings));
// create the module folder URI
// fix for windows server
$module_folder = str_replace( DIRECTORY_SEPARATOR, '/', $module_folder );
$__tmpUrlSplit = explode("/", $module_folder);
$__tmpUrl = '';
$nrChunk = count($__tmpUrlSplit);
if($nrChunk > 0) {
foreach ($__tmpUrlSplit as $key => $value){
if( $key > ( $nrChunk - 4) && trim($value) != ""){
$__tmpUrl .= $value . "/";
}
}
}
// get the module status. Check if it's activate or not
$status = false;
// default activate all core modules
if(in_array( $alias, $this->cfg['core-modules'] )) {
$status = true;
}else{
// activate the modules from DB status
$db_alias = $this->alias . '_module_' . $alias;
if(get_option($db_alias) == 'true'){
$status = true;
}
}
// push to modules array
$this->cfg['modules'][$alias] = array_merge(array(
'folder_path' => $module_folder,
'folder_uri' => $this->cfg['paths']['plugin_dir_url'] . $__tmpUrl,
'db_alias' => $this->alias . '_' . $alias,
'status' => $status
), $settings );
// add to menu order arrayhttp://cc.aa-team.com/wp-plugins/smart-seo-v2/wp-admin/admin-ajax.php?action=pspLoadSection§ion=Social_Stats
if(!isset($this->cfg['menu_order'][(int)$settings[$alias]['menu']['order']])){
$this->cfg['menu_order'][(int)$settings[$alias]['menu']['order']] = $alias;
}else{
// add the menu to next free key
$this->cfg['menu_order'][] = $alias;
}
// add module to activate modules array
if($status == true){
$this->cfg['activate_modules'][$alias] = true;
}
// load the init of current loop module
if( $status == true && isset( $settings[$alias]['module_init'] ) ){
if( is_file($module_folder . $settings[$alias]['module_init']) ){
//if( is_admin() ) {
$current_module = array($alias => $this->cfg['modules'][$alias]);
require_once( $module_folder . $settings[$alias]['module_init'] );
//}
}
}
}
}
// order menu_order ascendent
ksort($this->cfg['menu_order']);
}
End rereceives value by reference, but result of function is not variable.
You could rewrite your code.
$array_keys = array_keys($settings);
$alias = (string)end($array_keys);
unset($array_keys);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
My contact form is othervise working but I keep getting the following
error:
Deprecated: Function ereg() is deprecated in/home/.....
I'm really lost here but I figure this is the part that needs some adjusting.
if ( empty($_REQUEST['name']) ) {
$pass = 1;
$alert .= $emptyname;
} elseif ( ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] ) ) {
$pass = 1;
$alert .= $alertname;
}
if ( empty($_REQUEST['email']) ) {
$pass = 1;
$alert .= $emptyemail;
} elseif ( !eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*#[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z] {2,3})$", $_REQUEST['email']) ) {
$pass = 1;
$alert .= $alertemail;
}
if ( empty($_REQUEST['message']) ) {
$pass = 1;
$alert .= $emptymessage;
} elseif ( preg_match( "[][{}()*+?\\^$|]", $_REQUEST['message'] ) ) {
$pass = 1;
$alert .= $alertmessage;
}
Finding a solution would be highly appreciated
You must use preg_match instead of ereg because the last one is deprecated.
Replacing it is not a big deal:
ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] )
will become:
preg_match( "/[][{}()*+?.\\^$|]/", $_REQUEST['name'] )
p.s. I had to modify more than one hundred files while I was porting my old project to PHP 5.3 to avoid manually modifying I've used following script to do it for me:
function replaceEregWithPregMatch($path) {
$content = file_get_contents($path);
$content = preg_replace('/ereg\(("|\')(.+)(\"|\'),/',
"preg_match('/$2/',",
$content);
file_put_contents($path, $content);
}
I hope it helps.
The function ereg() is deprecated and should not be used any more. The documentation tells you what to do (to use preg_match instead).
Like you said - no bigie, it works like a charm:
if ( empty($_REQUEST['name']) ) {
$pass = 1;
$alert .= $emptyname;
} elseif ( preg_match( "/[][{}()*+?.\\^$|]/", $_REQUEST['name'] ) ) {
$pass = 1;
$alert .= $alertname;
}
if ( empty($_REQUEST['email']) ) {
$pass = 1;
$alert .= $emptyemail;
} elseif ( !preg_match("#^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$#i", $_REQUEST['email']) ) {
$pass = 1;
$alert .= $alertemail;
}
if ( empty($_REQUEST['message']) ) {
$pass = 1;
$alert .= $emptymessage;
} elseif ( preg_match( "/[][{}()*+?\\^$|]/", $_REQUEST['message'] ) ) {
$pass = 1;
$alert .= $alertmessage;
}
Thank you guys