I am working on a project where I need to add a email template that uses variables for name, address and phone number.
In my database i have 2 tables
Users - with name, address, phone number and category
Email_templates - for different email templates like Christmas, New year etc
on front end i have a textarea to add templates in database.
My template contains tokens like {name}, {address}, {phone} that are replaced with respective user detail when I send emails using that template.
Now i am able to fetch all details for users and email templates but not able to replace the tokens with values with php.
i tried str_replace to replace {name} and other tokens with variables like $user->name.
You can use below code to replace tokens :
$template_body = file_get_contents('Email template file path');
$email_values= array(
'name'=>$user->name,
'address'=>$user->address,
'phone'=>$user->phone,
);
if(count($email_values)>0)
{
foreach($email_values as $key=>$value)
{
$template_body = str_replace('{'.$key.'}',$value,$template_body);
}
}
return $template_body;
Related
I want to send {contactfield=id} (one of the variable of the mautic) into custom headers for a mail that is to be used for a campaign. I am not sure of the right way to send it. I am keeping this variable into custom headers under Channels > Emails and selecting a particular email's advanced settings and into custom headers. This sets the id value of the first contact in the contact list of the segment selected for the campaign into all the remaining contacts. I want to get the dynamic value of each contact's id respectively. How can I get this appropriate result? Thanks in advance.
Last time I checked, not all of the fields in the Email builder can contain tokens. I remember that the Body and Subject allow it, but there were some other fields that did not allow it.
You seem to be getting the correct token, so if it's not returning in the header based on your testing, that field may not get parsed for tokenization on processing.
As pointed out by Jordan Ryan, not all tokens are available under email, however the email object can be tapped by creating a custom plugin.
https://developer.mautic.org/#extending-emails
check out this section, it may help.
The second example on code block shows that you can place your custom placeholder and then replace it programmatically.
/**
* Search and replace tokens with content
*
* #param EmailSendEvent $event
*/
public function onEmailGenerate(EmailSendEvent $event)
{
// Get content
$content = $event->getContent();
// Search and replace tokens
$content = str_replace('{hello}', 'world!', $content);
// Set updated content
$event->setContent($content);
}
I created some custom field (title) in my Moodle install: I need to include it in the customization of the Moodle Language String (newusernewpasswordtext).
The default language string is:
Hi {$a->firstname}, A new account has been created for you at '{$a->sitename}' and you have been...
I need to update it to allow updating with the custom field, 'title' and a default field of last name.
I added the following string:
Hi {$a->profile_field_title} {$a->firstname} {$a->lastname}, A new account has been created for you at '{$a->sitename}' and you have been...
Output:
Hi {$a->profile_field_title} John Doe {$a->lastname}, A new account has been created for you at 'www.mysite.com' and you have been...
How can I insert a custom field in the string?
You won't be able to do that without editing Moodle core code. The newusernewpassword email is generated and sent in the lib/moodlelib.php file with the setnew_password_and_mail function. You'll need to change the object that's passed into that string to include your profile field before you can edit the language pack:
$yourfieldid = $DB->get_field('user_info_field', 'id', array('name'=>'profile_field_title'));
$a = new stdClass();
$a->firstname = fullname($user, true);
...
$a->your_profile_field = $DB->get_field('user_info_data', 'data',
array('userid'=>$user->id, 'fieldid'=>$yourfieldid));
Then you would change your string to:
Hi {$a->your_profile_field} {$a->firstname}, A new account has been
created for you at '{$a->sitename}' and you have been...
Also note: the reason {$a->lastname} shows up in your output as well is because the object that's passed into the function that makes the email doesn't have a 'lastname'-- the last name is included in the 'firstname' through Moodle's fullname function.
By going to Home Site administration> Users> Permissions> User policies, you can change the fullname function of Moodle. Change both Set both 'Full name format' and 'Alternative full name format' from the default language to the desired format (firstname, lastname, etc.)
I'm a newbie to web designing. I'm using contact form 7 to create a registration form for our conference.
All I wanted to do is, I need to give a unique id for all of them after they have registered for the conference and the further forms should be identified using this unique id.
So far, I have installed contact form 7 and contact form dtx
for this purpose and I have tried Koen de Bakker solution of generating a random number.
But it's slightly different from what I want, since it changes the random number for each refresh.
What I'd like is:
An unique number like "17ICLAA001,..." should be generated for each form submission.
Send the unique number to the applicant after successive submission of the form.(I hope this can be easily done once the shortcode is done).
Editing the form using the unique id.
Any help will be much appreciated. Thank you.
I have found a way to do this. Its just the number of rows+1 in the table.
When you add a record to your table the unique number also gets increased by 1 in the following code. Add the following function in function.php in your theme and use the short code "row_count" to call the function. Use it with dynamichidden text from dtx.
function row_count_shortcode() {
global $wpdb;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->username_wp1.SaveContactForm7_1" )+1;
return "17ICLAA".sprintf('%03d',$user_count);
}
add_shortcode( 'row_count', 'row_count_shortcode' );
Usually when you are creating contact forms using contact form7 it automatically creates a table in your database something like
username_wp1.SaveContactForm7_1
Instead of this replace your database table name.
So in your contact form, type
[dynamichidden uniqueid "row_count"]
and use [uniqueid] in your email body to serve your purpose.
It works fine. I have checked in my site.
Correct way to generate a unique and progressive number is to set a field in wp_option like this:
add_option('unique_number', '1');
When filter is called, you must simply increment this unique number:
function genTicketString() {
$currentUniqueNumber = get_option('unique_number');
$newCurrentUniqueNumber = $currentUniqueNumber + 1;
update_option('unique_number' $newCurrentUniqueNumber );
return $newCurrentUniqueNumber;
}
add_shortcode('quoteticket', 'genTicketString');
I can add and remove fields in the user profile section with:
function add_remove_contactmethods($contactmethods ) {
// Remove AIM
unset($contactmethods['aim']);
//add Phone
$contactmethods['phone'] = 'Phone';
return $contactmethods;
}
add_filter( 'user_contactmethods', 'add_remove_contactmethods' );
When I view this screen in the backend, the "Phone" field comes last, after some other fields like "Email" and "Website". I guess this is because my added field was added after the default Wordpress fields. How do I sort this, for instance alphabetically, so that my "Phone" field comes in alphabetical order instead of after the default fields? How do I sort the output of $contactmethods without messing it up?
try using ksort
function add_remove_contactmethods($contactmethods ) {
// Remove AIM
unset($contactmethods['aim']);
//add Phone
$contactmethods['phone'] = 'Phone';
ksort($contactmethods);
return $contactmethods;
}
add_filter( 'user_contactmethods', 'add_remove_contactmethods' );
re
UPDATE: So I guess the answer to my original question, is to explain why and how "Website" and "Email" are stored, and how the output is controlled in the backend when you view a profile. Maybe it's an ordered action? I guess "Website" and "Email" are just user meta, but how is the output order controlled. I accept that I might have to write a custom script to sort the output, I just don't know where to begin.
Your right about that, all the new contact fields were added into user_meta table. user_email and user_url are in the users table. The problem you are going to have doing this, is that a filter does not exist to modify the information. You can check the main filters here:
http://codex.wordpress.org/Plugin_API/Filter_Reference
and also you can look at core itself. All the admin templates are in wp-admin so you can look at the variable you need to modify in user-edit.php ($profileuser). Im in no way recommending this, but you could modify the template there, it will be overwritten on the next update of course so thats a drawback to it.
There may be a hook somewhere in admin in the load template process, if you could find one, you could relocate the template location to a theme file and recreate it with the changes you want. But all this seems like a lot of work to include just 2 fields to reorder?
Another approach is to use a higher priority the other when adding the fields. For example, Yoast adding 3 contact methods, and if you want your 'phone' appear before those, set the filter to:
add_filter('user_contactmethods', 'my_contactmethods', -5, 1);
Email and Website cant be re-ordered unless deep PHP coding or javascript re-order or advanced CSS.
if you know the key(s) (check the name fields in the source code), you can add other plugin fields by yourself, and choose exact appearance. A field is only added once if they added twice (!) This is how we use:
function entex_author_contactmethods($contactmethods){
$contactmethods['mail'] = __('Public email', 'entex-theme');
$contactmethods['phone'] = __('Phone', 'entex-theme');
$contactmethods['googleplus'] = __('Google+', 'wordpress-seo');
$contactmethods['youtube'] = __('YouTube URL', 'wordpress-seo');
$contactmethods['facebook'] = __('Facebook profile URL', 'wordpress-seo');
$contactmethods['instagram'] = __('Instagram URL', 'wordpress-seo');
$contactmethods['twitter'] = __('Twitter username (without #)', 'wordpress-seo');
$contactmethods['linkedin'] = __('LinkedIn URL', 'wordpress-seo');
$contactmethods['myspace'] = __('MySpace URL', 'wordpress-seo');
$contactmethods['pinterest'] = __('Pinterest URL', 'wordpress-seo');
return $contactmethods;
}
add_filter('user_contactmethods', 'entex_author_contactmethods', -5, 1);
Happy contact-ing!
Hi guys I'm building a contacts application using the Zend Framework 1. I have a contacts table and a contact_data table.
Contact
|NAME|DESCRIPTION|...
CONTACT DATA
|TYPE|LOCATION|DETAILS|CONTACT_ID
ADDRESS
STREET|ZIP|CITY|STATE|COUNTRY|CONTACT_ID
The Contact Data holds all contact details such as Phone, Email, Fax etc and the address table is self explanatory. The trick is that I need to set it up so that I can add unlimited contact data and addresses. I've accomplished this earlier on by pretty much working on customised views however thats putting a lot of code logic within the view which I don't want. So I'm redoing it using Zend_Form but am stuck with regards to setting up the add/edit/remove multiple contact details from the same form part.
I have the javascript worked out and know how to get it done using views - but I need to get this done using Zend Forms here. I've looked at the idea of subforms however in my case I need to do the following:
My form is stuctured as follows:
Text and INputs for all contact details listed out
A special contact Data region with links to Add a Phone, Add a Fax, Add an address. Clicking on these links would open up and add a set of inputs to the table eg add an address link adds a street, city, country and state set of input to the table.
I've been hacking at this for an hour and am pretty lost here. Any ideas on how can i handle this?
I think the best way to solve this problem is creating new Elements. One for contact data and one for address. Each of those elements should have custom viewHelper to render your complex interface. Each of view helpers can assign javascript file to handle dynamic adding / removing.
class Form_Element_Contact extends Zend_Form_Element_Xhtml {
public $helper = 'contact';
}
class Zend_View_Helper_Contact extends Zend_View_Helper_FormElement {
public function contact($name, $value = null, $attribs = null, $options = null) {
$this->view->headScript()->appendFile("/js/dynamicInterfaceForContacts.js");
$info = $this->_getInfo($name, $value, $attribs, $options);
$html = $this->generateHtml(); // elements should be part of an array
return $html;
}
}
If you need more details let me know.