In WordPress I created an options page that has fields my clients can enter a Site Title, Slogan, and Instagram Access Code. I was able to overwrite the default values for these fields using update_option (see code below). Unfortunately I just realized that this is a one time overwrite and if a client goes in and updates their Site Title or Slogan these changes do not update the database.
Here is my code:
$all_options = get_option( 'global-information', array() );
$siteTitle = isset( $all_options['company-name'] ) ? $all_options['company-name'] : false;
$siteSlogan = isset( $all_options['slogan'] ) ? $all_options['slogan'] : false;
$agentmlsid = isset( $all_options['agent-mls-id'] ) ? $all_options['agent-mls-id'] : false;
$instagram = isset( $all_options['instagram-access-code'] ) ? $all_options['instagram-access-code'] : false;
update_option( 'blogname', $siteTitle );
update_option( 'blogdescription', $siteSlogan );
update_option( 'cx_insta-access-token', $instagram );
Is there a way to have these values update with every change?
Related
I have a very strange issue and I can't find any fix for it.
I have the following code:
function convertToComponent($idPryComponent) {
$pryComponentData = $this->{$this->modelClass}->findByIdPryComponent ( $idPryComponent );
$this->loadModel ('ComponentSource' );
if ($this->ComponentSource->save ( $pryComponentData [$this->modelClass] )) {
$idComponent = $this->ComponentSource->id;
$pryComponentData [$this->modelClass] ['ID_COMPONENT'] = $idComponent;
$save=array();
$save['PryComponent']['ID_PRY_COMPONENT']=$idPryComponent;
$save['PryComponent']['ID_COMPONENT']=$idComponent;
$return= $this->PryComponent->save ($save);
return $idComponent;
}
return false;
}
And after the save actiond, the sentence "return $idComponent;", inserts another empty record on the database, and I don't know why!!!
Could you help?
I am using contact form 7 for creating my form and I have a custom db hosted in the same server that should contain the related data.
I want to store the data from the contact from7 into my custom db and not a wordpress db.
I am doing the below in functions.php now,
add_action('wpcf7_before_send_mail', 'save_form');
function save_form($wpcf7) {
/* For connecting to database */
$dbuser = "user";
$dbpass = "pass";
$dbhost = "localhost";
$dbname = "cistom_db";
// Connect to server and select database.
$db = mysqli_connect($dbhost, $dbuser, $dbpass) or die("cannot connect");
mysqli_select_db($db, $dbname) or die("cannot select DB");
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$submited = array();
$submited['title'] = $wpcf7->title();
} else {
echo 'error';
}
$insert_query = "insert into candidate(title)values('" . $submited['title'] . "')";
$result = mysqli_query($db, $insert_query);
if (!$result) {
die('Invalid query: $insert_query : ' . mysqli_error($db));
}
}
However, nothing seems to be working here. Can anyone please help?
Follow the steps to store contact form 7 data in custom table:
1) Create Custom table
CREATE TABLE candidate(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50)
);
2) Create contact form 7 fields
[text* title]
[submit "Send"]
3) Add Below code to function.php
function contactform7_before_send_mail( $form_to_DB ) {
//set your db details
$mydb = new wpdb('root','','cistom_db','localhost');
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB )
$formData = $form_to_DB->get_posted_data();
$title = $formData['title'];
$mydb->insert( 'candidate', array( 'title' =>$title ), array( '%s' ) );
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );
Hope this works for you.
I took over the web development for a company and they've got a feedback form on a couple pages (colorado, prescott, flagstaff and wyoming) that submits form info to an email as a spam filter using PHPmailer. However, they're not receiving emails the way they hoped. I can get emails through all addAddress emails except for the one using $recipeint. Searched and tried everything I could think of including explode and foreach other codes. Can't get this to work. A little help would be great.
$colorado = array( "ColoradoSprings", "Denver", "Boulder" );
$colorado = array_map( 'strtolower', $colorado );
$wyoming = array( "Cheyenne", "Buffalo" );
$wyoming = array_map( 'strtolower', $wyoming );
if ( in_array( $_POST[ 'location' ], $colorado ) || in_array( $_POST[ 'location' ], $wyoming ) )
$recipient = "person1#mail.com, person2#mail.com";
switch ( $_POST[ 'location' ] ) {
case "prescott":
$recipient = "person3#mail.com, person4#mail.com";
break;
case "flagstaff":
$recipient = "person3#mail.com, person4#mail.com";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.mail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "senderemail#mail.com";
$mail->Password = "password";
$mail->setFrom( 'allmail#mail.com', 'Everyone' );
$mail->addReplyTo( $_POST[ "Email" ], $_POST[ "Name" ] );
$mail->addAddress( 'allmail#mail.com', 'Everyone' );
$mail->addAddress( 'random#mail.com', 'John Doe' );
$mail->addAddress( $recipient, 'Others' );
This is strange code, or incomplete. As it stands it will only attempt to send email if location == 'flagstaff', so you probably need to pay some attention to your conditions.
addAddress only accepts a single address; if you want to send more, call it multiple times in a loop. For example:
$recipients = ["person1#mail.com", "person2#mail.com"];
foreach ($recipients as $recipient) {
$mail->addAddress($recipient);
}
This function adds custom post 'event' data into a Salesforce db. I've tested the function outside of Wordpress and it works flawlessly. When I test it inside Wordpress by adding a new event, no error is generated and a the data is not inserted into the SF db. I've also tested this by printing out the $_POST and saw that the data is being collected. How can I get this display some errors so that I can trouble shoot this?
function add_campaign_to_SF( $post_id) {
global $SF_USERNAME;
global $SF_PASSWORD;
if ('event' == $_POST['post-type']) {
try {
$mySforceConnection = new SforceEnterpriseClient();
$mySoapClient = $mySforceConnection->createConnection(CD_PLUGIN_PATH . 'Toolkit/soapclient/enterprise.wsdl.xml');
$mySFlogin = $mySforceConnection->login($SF_USERNAME, $SF_PASSWORD);
$sObject = new stdclass();
$sObject->Name = get_the_title( $post_id );
$sObject->StartDate = date("Y-m-d", strtotime($_POST["events_startdate"]));
$sObject->EndDate = date("Y-m-d", strtotime($_POST["events_enddate"]));
$sObject->IsActive = '1';
$createResponse = $mySforceConnection->create(array($sObject), 'Campaign');
$ids = array();
foreach ($createResponse as $createResult) {
error_log($createResult);
array_push($ids, $createResult->id);
}
} catch (Exception $e) {
error_log($mySforceConnection->getLastRequest());
error_log($e->faultstring);
die;
}
}
}
add_action( 'save_post', 'add_campaign_to_SF');
I would use get_post_type() to check for "event" posts. Use error_log() to write to the PHP error log for additional debugging - check the status of your Salesforce login, etc.
Keep in mind that save_post will run every time a post is saved - created or updated - so you might want to do some additional checking (like setting a meta value) before creating a new Campaign in Salesforce, otherwise you will end up with duplicates.
function add_campaign_to_SF( $post_id ) {
$debug = true;
if ($debug) error_log("Running save_post function add_campaign_to_SF( $post_id )");
if ( 'event' == get_post_type( $post_id ) ){
if ($debug) error_log("The post type is 'event'");
if ( false === get_post_meta( $post_id, 'sfdc_id', true ) ){
if ($debug) error_log("There is no meta value for 'sfdc_id'");
// add to Salesforce, get back the ID of the new Campaign object
if ($debug) error_log("The new object ID is $sfdc_id");
update_post_meta( $post_id, 'sfdc_id', $sfdc_id );
}
}
}
add_action( 'save_post', 'add_campaign_to_SF' );
I'm having some issues trying to save an article that has 4 pictures. The thing is that i need to use the article id in order to name the pictures like article_id."-"$i
Since I have only 4 pictures per article this $i should be from 1 to 4 or from 0 to three.
Now the problem is that in order to achieve this i need to create and save Article model so i can have an id to use, but then after performing all the scripting to make the thumbs and form the names, when I go Article->saveAssociated() i have two times the article record created!! i tried to set the id to "-1" before saving but nothing...
Any suggestion will be very much appreciated !!!
Code:
public function add() {
if ($this->request->is ( 'ajax' )) {
$this->layout = 'ajax';
} else {
$this->layout = 'default';
}
if ($this->request->is ( 'post' )) {
$this->Article->create ();
$this->request->data ['Article'] ['time_stamp'] = date ( 'Y-m-d H:i:s', time () );
if ($this->Article->save($this->request->data) ) {
for ($i=0; $i<4; $i++){
$img_path = "./images/";
$extension[$i] = end(explode('.', $this->request->data['Image'][$i]['image']['name']));
$this->request->data['Image'][$i]['image'] = array('name'=>$this->Article->id."-".$i, 'tmp_name' => $this->request->data['Image'][$i]['image']['tmp_name']);
// $this->request->data['Image'][$i]['name'] = $this->Article->id."-".$i;
$this->request->data['Image'][$i]['ext']= $extension[$i];
$target_path[$i] = $img_path . basename($this->request->data['Image'][$i]['image']['name'].".".$extension[$i]);
if(!move_uploaded_file($this->request->data['Image'][$i]['image']['tmp_name'], $target_path[$i])) {
die(__ ( 'Fatal error, we are all going to die.' ));
}else{
$this->Resize->img($target_path[$i]);
$this->Resize->setNewImage($img_path.basename($this->request->data['Image'][$i]['image']['name']."t.".$extension[$i]));
$this->Resize->setProportionalFlag('H');
$this->Resize->setProportional(1);
$this->Resize->setNewSize(90, 90);
$this->Resize->make();
}
}
$this->Article->id;
pr($this->Article->id);
$this->Article->saveAssociated($this->request->data, array('deep' => true));
//$this->redirect ( array ('action' => 'view', $this->Article->id ) );
pr($this->Article->id);
exit;
$this->Session->setFlash ( __ ( 'Article "' . $this->request->data ["Article"] ["name"] . '" has been saved' ) );
} else {
$this->Session->setFlash ( __ ( 'The article could not be saved. Please, try again.' ) );
}
}
$items = $this->Article->Item->find ( 'list' );
$payments = $this->Article->Payment->find ( 'list' );
$shippings = $this->Article->Shipping->find ( 'list' );
$this->set ( compact ( 'items', 'payments', 'shippings' ) );
}
Instead of
$this->Article->saveAssociated();
which would save the Article AGAIN, just save the images separately using something like this:
foreach($this->request->data['Image'] as &$image) {
$image['name'] = 'whatever_you_want' . $this->Article->id;
$image['article_id'] = $this->Article->id;
}
$this->Article->Image->save($this->request->data['Image']);
Another option (not necessarily better - just another option) would just be to append the newly-created Article's id to the existing Article array, then saveAssociated(). If the Article has an id in it's data, it will update instead of create. I would suggest the first answer above, but - just brainstorming other options in case this helps for someone's scenario:
// 1) save the Article and get it's id
// 2) append the `id` into the Article array
// 3) do your image-name manipulation using the id
// 4) saveAssociated(), which updates the Article and creates the Images