how to display "There are updates available for your Custom Plugin" in wordpress - 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());
}

Related

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

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.

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 do you protect certain php files from overwriting by updates in Wordpress?

Let's say I have made some changes on a php file from a certain plug-in. How do I prevent their lost when someone else decide to update this plug-in?
You should try to alter the plugin's behavior with a plugin of your own. But that depends on the nature of the changes you've done.
You can disable its update capabilities with the following filter:
add_filter( 'site_transient_update_plugins', 'filter_plugin_updates' );
function filter_plugin_updates( $value ) {
unset( $value->response['akismet/akismet.php'] );
return $value;
}
There are a couple of other methods to deal with this in the Answer from where I copied this code:
If I rename a plugin (in its main php file) do I still get update notifications?

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');

WordPress Plugin: Need to fire off a function immediately after the plugin is activated

I need my plugin to run a function immediately after the plugin has been installed. The reason I need to run the function after and not during the installation is because none of the hooks work until "after" the plugin is activated and I need to do some additional install synching with a thirdparty server and I need those hooks.
So far I've found nothing that does what I want. The crons functions, from what I can tell and from what the codex says, only fire after someone visits the site. This is a "no no". The plugin cannot wait some "random" period of time. It might even be a serious security risk.
Thirlan, I have the same problem. I haven't been able to come up with a great solution, so what I'm doing is on plugin activate I'm setting a update_option and then once the settings page is visited I'm checking for the get_option to check for my one-time setting and if it's there, I fire off the function and delete_option. Now this won't exactly work for you, but... you might be able to figure out how to apply this filter:
http://adambrown.info/p/wp_hooks/hook/install_plugin_complete_actions?version=3.0&file=wp-admin/includes/class-wp-upgrader.php
or you might be able to sort of use my method. Try this:
register_activation_hook(__FILE__, 'initialize_my_function');
function initialize_my_function() {
add_option('run_my_initialization',"1");
}
add_action('admin_init', 'launch_activation_script');
function launch_activation_script() {
if (get_option('run_my_initialization') == "1") {
//Do Your Init Stuff Here
delete_option('run_my_initialization');
}
}
Can you use register_activation_hook?

Resources