How to attach files to an email sent by a WordPress booking plugin? - wordpress

I'm using a WordPress plugin for accepting online bookings (Appointment Hour Booking) and I need to attach a file to the emails sent after submitting the booking request (a PDF file with the general booking terms). I already applied a solution by editing the calls to the wp_mail() function in this way:
wp_mail(trim($payer_email), $subject, $message,
"From: ".$from."\r\n".
$content_type. "X-Mailer: PHP/" . phpversion(),
array(WP_CONTENT_DIR . '/uploads/agreement.pdf'));
The above works but everytime the plugin updates the file is overwritten and I've to reapply the code modification again. There is a better way to do that without being affected by the plugin updates or there is a way to prevent partially or completely a plugin update in WordPress?
Thank you in advance for any help.

Disabling the plugin update isn't a good idea, you may lost important compatibility or security updates. The way the call to the wp_mail() was modified also causes other attachment-related features stop working. The plugin you mention has a filter that can be used to modify the list of attached files, you can put the following code for example into your theme’s functions.php file:
add_filter( 'cpappb_email_attachments', 'my_attach_function', 10, 3 );
function my_attach_function( $attachments, $params, $form_id )
{
$attachments[] = WP_CONTENT_DIR . '/uploads/agreement.pdf';
return $attachments;
}
With the above code located out of the plugin files your file is added to the list of attachments without removing other attachments and locating the code out of the plugin files will prevent being overwritten by the plugin updates.

Your options are:
Fork the plugin and customize it to your needs.
Ask the team behind the plugin to implement a filter hook to allow customizing the headers passed to the wp_mail() function (so you can then attach files to e-mails).
Keep doing what you have been doing until now.
I like option two the best because:
It allows you to customize the behavior of the plugin from the outside, and,
Your changes will survive plugin updates.

Related

Wordpress Woocommerce plugin mail triggering

Mail not sending when status changed from processing to on-hold and processing to failed.
Kindly tell me how to achieve this. Thanks in advance
Even that kind of questions (directly asking the need without any research or any part of code) are not welcomed here, I want to help you about that by giving the basic idea.
In the file "wp-content/plugins/woocommerce/includes/class-wc-emails.php", search for the "public static function init_transactional_emails()" and check "$email_actions" array there.
$email_actions = apply_filters(
'woocommerce_email_actions', array(
'woocommerce_low_stock',
'woocommerce_no_stock',
'woocommerce_product_on_backorder',
'woocommerce_order_status_pending_to_processing',
'woocommerce_order_status_pending_to_completed',
'woocommerce_order_status_processing_to_cancelled',
'woocommerce_order_status_pending_to_failed',
'woocommerce_order_status_pending_to_on-hold',
'woocommerce_order_status_failed_to_processing',
'woocommerce_order_status_failed_to_completed',
'woocommerce_order_status_failed_to_on-hold',
'woocommerce_order_status_on-hold_to_processing',
'woocommerce_order_status_on-hold_to_cancelled',
'woocommerce_order_status_on-hold_to_failed',
'woocommerce_order_status_completed',
'woocommerce_order_fully_refunded',
'woocommerce_order_partially_refunded',
'woocommerce_new_customer_note',
'woocommerce_created_customer',
)
);
Since after every update of Woocommerce plugin any changes you made on those files will be gone, you need to add your email trigger for status changes you mentioned by either using a hook or overriding the files using your child theme.
About your request, for "from processing to on-hold" you need to add:
'woocommerce_order_status_processing_to_on-hold',
About overriding a file (or function) from includes folder of Woocommerce you may check this post: Override woocommerce files from includes folder
I hope this will help you to solve it. Have a good day.

Risks about edit the source code of a Wordpress plugin

I am making my first steps coding. I made some courses on Internet, and now I started to make a Wordpress theme to continue learning from the practice.
I find that there is a lot of Plugins that can help me to achieve the goals that I want, and I also found a plugin that makes almost everything that I want.
I started to modify the source code of that plugin so it could fits in my design scheme. Now I don't know if it is a good idea.
I didn't find a way to make a "child plugin" so at this moment I don't know if continue editing the source of this plugin, (that means that I would never update my plugin because I would lose all the modifications) or simply make everything by my own that would take me a lot more of time.
Do you have some suggestion?
You should take a look into world of hooks and filters.
This is nice place to learn from. Here is list of WP hooks where you can hook your code.
And Woocommerce hook example how to cusom validate field.
First part: woocommerce_checkout_process is place where to hook code and my_custom_checkout_field_process is name of your function.
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['my_field_name'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
Some plugins have template files, that you can put into child theme.
But sadly not all plugins are well customizable.

how to display "There are updates available for your Custom Plugin" in wordpress

I have developed a custom wordpress plugin, many users have started using it, but now I have updates available for the plugin and want to display a message to the users who have older versions of the plugin on there site.
How can I modify the code of my plugin so that once I make updates to it, it should trigger a message to the users on the plugin dashboard that there are updates to available to your plugin.
Here is a scenario:
Say a user has version 1.0 of my plugin and the place where I host the plugin has version 1.2, how can I notify the user on his plugins page that my plugin has an updated version??
Although user3042036 answer is great, and very comprehensive, I thought I would entend his / her answer with a open source solution.
This is what you are looking for: WordPress Plugin Update Notifier
First, good practice is to create a constant for your current plugin version, and create an activation and deactivation hook for your plugin. This allows you to check things like version numbers, and do some general initialization.
define ( 'MY_PLUGIN_VERSION', '2.0.0');
register_activation_hook(__FILE__, 'my_plugin_activation'));
register_deactivation_hook(__FILE__, 'my_plugin_deactivation'));
function my_plugin_activation() {
// Initialize some stuff for my_plugin
}
function my_plugin_deactivation() {
// Welp, I've been deactivated - are there some things I should clean up?
}
Here is an example of a typical update function:
function my_plugin_activation() {
$version = get_option( 'my_plugin_version' );
if( version_compare($version, '2.0.0', '<')) {
// Do some special things when we update to 2.0.0.
}
update_option( 'my_plugin_version', MY_PLUGIN_VERSION );
return MY_PLUGIN_VERSION;
}
There is no hook for when your plugin is updated. You, as a plugin
author, have to manually check the plugin version. First, you want to
create a simple function which will tell you if your plugin is up to
date:
function my_plugin_is_current_version(){
$version = get_option( 'my_plugin_version' );
return version_compare($version, MY_PLUGIN_VERSION, '=') ? true : false;
}
Then, test if your plugin is up to date, and call your update function (or in this case we call the same function as we would if the plugin was updated!):
if ( !my_plugin_is_current_version() ) my_plugin_activation();
Testing the update process from one version to the next is not all that complicated, though it is kinda cumbersome. Maybe someone has a better way, if so please tell me!
You can’t really see any errors when you activate a plugin, so the first step is to create a very simple hook to store plugin activation errors. In this case, we store these errors in error_activation.html in the plugin folder
add_action('activated_plugin', 'my_plugin_activation_error');
my_plugin_activation_error() {
file_put_contents( plugin_dir_path(__FILE__) . '/error_activation.html', ob_get_contents());
}

wordpress plugin hooks method

I am creating a plugin for wordpress.I need database interaction.So i need to run some queries to create table.I want to run those queries in a php function.I need to run this function when this plugin will active.What hooks should i use for this purpose?? Now i am using this:
add_action( 'admin_menu', 'bs_check_database_creation' );
This is working fine so far.But i need appropriate hooks to run this function once when this plugin will activate.
Another queries : i want add a link of this plugin in header/footer/sidebar for end user to go to the plugin end user page.How should i do this?
Currently i've manually added a link for this in wordpress template page.
Thanks in advance
It depends when you want the hook to run, But I think that init or admin_init will be right for you becasue they are the earliest ones running respectively on front and back end.
EDIT : (After comment) The INIT and admin_init are ment to use whenever a plugin needs to RUN, and not on first activation (or install) . writing "I need to run this function when this plugin will active " is a bit confusing :-) active means when it start to run , or when it is actually ACTIVATED ?
If you need to run a function upon ACTIVATION , then it is a bit different..
register_activation_hook(__FILE__, 'o99_brsa_on_activate');
function o99_brsa_on_activate() {
// do your stuff on activation
}
About the links, I am not sure what you mean by end user page ... Do you mean action links ?
And what footer do you mean ? the Admin or the Front ? ( After answering those issues I can try and reply - Even if it is a material for another question .)
As for links in the header / footer . If ou are planning to host this plugin in the wordpress repository please know that it is somewhat against the terms ( unless you request specific permission from the user )
Anyhow , this will do :
function o99_add_to_footer() {
echo '<p>This is inserted at the bottom</p>';
}
add_action('wp_footer', ' o99_add_to_footer');

Hook into the WordPress Theme Customizer save action

I am facing the following issue :
I used to keep all the styling in a theme options page. When the user clicked the save button, i had a backend script that generated a css file with the changes so that they will not be output inline in each page. This has a lot of benefits, amongst them caching.
I have switched to the Theme Customizer, and everything is fine except i can't find a way to hook into the the "save" button. I would like to trigger a function that updates the content of the css file when that button is clicked in the backend.
Is this even possible ?
Thanks !
Since WordPress 3.6.0 you can now call customize_save_after.
<?php
function emailAdmin(){
mail('your#email', 'Woza!', 'You won\'t believe this but someone has updated the theme customizations!!');
}
add_action( 'customize_save_after', 'emailAdmin' );
?>
More info: http://developer.wordpress.org/reference/hooks/customize_save_after/
I'm facing the same situation. The customize_save works BEFORE the options are saved, so that's out. I've emailed Otto (ottodestruct.com) about it.
The solution I have right now is as follows:
add_action('customize_save', 'regenCSS', 100);
function regenCSS( $wp_customize ) {
checkCSSRegen(); // Checks if I need to regen and does so
set_theme_mod('regen-css', time()+3); // Waits 3 seconds until everything is saved
}
function checkCSSRegen() {
if (get_theme_mod('regen-css') != "" && get_theme_mod('regen-css') < time()) {
makecss();
remove_theme_mod('regen-css');
}
}
I also add an extra checkCSSRegen(); to my customize_controls_init function.
Again, this is a little bit of a hack. Unfortunately, it's the best I can find to do at the time.
Another option would be to use a ajax response that just pings a php file. That feels even more of a hack than this.
Another quick hack would be to do a javascript action that when the save button is clicked, it sets a timer to delay a call to a PHP file that runs the compile. That is VERY hacky to me.
The only fallback of the above, is unless the customizer is reloaded or another value saved, you may not get all the values you want.
Anyone else have a better idea?
** Update **
Just added the following request to the Wordpress team. Hopefully we'll get it squeezed in there.
http://wordpress.org/ideas/topic/do-customize_save-action-hook-after-the-settings-are-saved?replies=3#post-24853
* Update 2 *
Looks like it will be in the 3.6 release as customize_save_after. Guess a few tweets and example code can make stuff happen even with the Wordpress team. ;)
As describe by #Dovy already you can hook customize_save_after to do this now:
do_action('customize_save_after', 'savesettings', 99);
When savesettings save settings to a file it will be bad practice to do this with native php file functions (like file_put_contents()) as described here: http://ottopress.com/2011/tutorial-using-the-wp_filesystem/ by #otto.
Solution for file saving will be to use wp_filesystem. To use wp_filesystem you will need the file credentials (ftp) of the user.
customize_save_after will be called in a AJAX request and the result won't be visible. Cause of the AJAX handle you can't ask the user for the file credentials which requires a form submit.
Solution can be found by saving the file credentials to wp-config.php and add them ( temporary ) to the database. Doing this savesettings can read the credentials from the database and use them to save the file by using credentials. (this solution is described in more detail here: https://wordpress.stackexchange.com/a/126631/31759)
Not tested, but there is the action hook customize_save in /wp-includes/class-wp-customize-manager.php.
It's inside the save() function:
/**
* Switch the theme and trigger the save action of each setting.
*
* #since 3.4.0
*/
There are some other interesting action hooks (do_action) in this file that may be worth check.

Resources