Update hook in wordpress not fired - wordpress

I want to perform a database operation on plugin update. My current version of plugin doesn't have this hook. I added the following code in the updated version. I am using the following code, but this doesn't work.
add_action( 'upgrader_process_complete', 'init_plugin_update', 10, 2 );
function init_plugin_update( $upgrader_object, $options ) {
error_log('test123');
}
I have seen WooCommerce using the same hook. So is there something i am unaware of ? Won't the new plugin file get loaded before this action ?

The upgrader_process_complete hook, as it's stated in the Codex:
Fires when the upgrader process is complete.
But it's also stated in the Notes:
Use with caution: When you use the upgrader_process_complete action hook in your plugin and your plugin is the one which under upgrade, then this action will run the old version of your plugin.
Effectively it means, if your current version of the plugin is 1.0 and it doesn't have this hook, and you're updating the plugin to version 2.0 which has that hook - it won't get fired.
Further Problems with this and other hooks
To perform a database upgrade (in case your version 2.0 has a different table structure) you obviously can't use this hook.
There is another scenario - when your plugin is updated via FTP and not the admin interface (for example you push some file changes through GIT CI/CD pipelines) - none of the hooks will get fired.
The solution
Manually manage the database version of your plugin.
Save the current plugin version in the database with
add_option.
On init hook (or even better, on
admin_init hook, because it gets triggered only during an admin
request - the Core uses this technique) perform a check if the
current version of the plugin is equal as the saved version in the
database. Use get_option and compare that with the version hardcoded in the plugin itself
If there's a mismatch, that means that the database runs
off of the old codebase and you need to update it

Related

init vs wp_loaded: Difference with example?

I understand init is fired when most of the WordPress is loaded but headers are not sent and wp_loaded is fired when most of WordPress is loaded and headers are sent.
Can someone please explain with an example that what will and will not work with these hooks with a use case scenario that explains and clarifies their difference?
As per my understanding, init hook implies that WP core is loaded but 3rd party content (e.g. themes, plugins, etc.) is only starting to load. Hook wp_loaded means that core and 3rd party content is loaded.
Can someone please explain with an example that what will and will not work with these hooks with a use case scenario that explains and clarifies their difference?
I guess if you want to override 3rd party plugin, you need to use wp_loaded unless the plugin itself creates its own specific "loaded" hook.
If you want to somehow alter default styles and scripts, you need to use wp_loaded as they are not loaded when init fires.
Please see technical clarification below:
init is run before wp_loaded:
init
register_sidebar
wp_register_sidebar_widget
wp_default_scripts
wp_default_styles
admin_bar_init
add_admin_bar_menus
wp_loaded
source: WP Codex
Additional insight for init hook:
Most of WP is loaded at this stage, and the user is authenticated. WP continues to load on the ‘init’ hook that follows (e.g. widgets), and many plugins instantiate themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.).
source: WP Developer
Additional insight for wp_loaded hook:
This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated.
source: WP Developer
EDIT: also, bear in mind that wp_loaded is executed before admin_init (in case of admin-page request)

How can I send a post request to my Laravel 4.2 app when submitting new order in wordpress, woocommerce

I have a Laravel 4.2 app with REST API, And I want to save the orders in my app database by using api
How can I do this? Should I modify Woocommerce Plugin or do something else?
Possibility #1
You need to make your own wordpress plugin and communicate with your Laravel app API endpoints.
Woocommerce receive order -> Plugin -> Curl -> Laravel API
I think there is ample information out there on how to create your own Wordpress plugin to do that, also you might want to look into the WooCommerce Documentation for relevant functions.
https://premium.wpmudev.org/blog/wordpress-plugin-development-guide/
https://code.tutsplus.com/tutorials/laravel-4-a-start-at-a-restful-api-updated--net-29785
Possibility #2
You can have a script on the Laravel APP side which will query the WooCommerce API and pull the latest WooCommerce orders.
https://docs.woocommerce.com/document/woocommerce-rest-api/
https://gist.github.com/sisou/889971461616661d2ed6 (Laravel 4 Cron inspired from Laravel 5 scheduler)
https://github.com/Indatus/dispatcher
Important : Never modify a core WordPress functions and avoid modifying any plugins (unless done with proper documentation and professional intent). This will avoid you further problem down the line you don't want and don't need. Also it is 99% of the time unnecessary since you can filter plugin functions and execute different task along any WordPress plugin.

Plugin/Custom Code to email me when a plugin update is available

I've tried 2 or three plugins but none seem to be functional. Is there a custom code piece out there or somewhere I can implement a PHP script that will send me an email when a new plugin update is available? Currently have tried WP Update Notifier and Mail-on-Update with zero success.
You can use Wordfence.
Set Email summary frequency to daily.
Use Scan for out of date plugins, themes and WordPress versions to find plugin updates.

Notify When Update(s) Performed

I'm looking to create a tally of the number of core, theme and plugin updates performed on a given WP installation. I envisage that this would involve creating an option into which I store and int value that is incremented every time a core, theme &/or plugin update is successfully performed.
To enable this I was hoping for some hooks are called when each of these update types are performed. However, I can't seem to find any relevant hooks for plugin and theme updates.
For core updates, it looks like I can simply increment the relevant options field whenever the '_core_updated_successfully' hook is called.
Are there any relevant hooks available for plugins and themes?
Can you suggest an alternative methodology?
Thanks in advance.
You can run the upgrader_process_complete action hook. it runs when the download process for a plugin install or update finishes. It takes a type parameter for core, plugin or theme.

Scheduled Posts - Plugin Missed Schedule - WP to Twitter

The feature to schedule posts in Wordpress 3.4.2 seems to be broken.
So I installed the plugin WP Missed Schedule that checks every 5 minutes for posts that match this problem.
I also use the Plugin WP to Twitter that supports the feature to tweet a new post automatically to Twitter - no matter whether the post is planned or not.
Here is the problem:
If there is a scheduled Post it will be published in Wordpress, supported by the Plugin WP Missed Schedule, but not tweeted!
The Code in WP Missed Schedule says:
wp_publish_post($scheduledID)
And the called Hook in WP to Twitter says:
add_action( 'publish_post', ...)
So is the Hook publish_post the right one for the function wp_publish_post?
I did not find any documentation about that.
Hope you can help me :)
I had the same problem on a website using Tweetable. I added this line right after
wp_publish_post($scheduledID)
tweetable_publish_tweet($scheduledID);
But that wasn't the problem. There were a few lines in Tweetable that caused the posting function to exit if the modified date was after the publish date. When the plugin publishes the post it changes the modified date. I commented out the lines in Tweetable and it worked.

Resources