I am trying to accomplish the following:
Whenever my plugin is updated via the wordpress plugin update function, I want it to execute a function which backs up certain plugin files first before the upgrade is running.
I was checking through available hooks on wordpress, however only found the upgrader_process_complete hook, which according to the wordpress codex website:
The upgrader_process_complete action hook is run when the download process for a plugin install or update finishes.
While "the download process" is a little bit unclear, I have checked in the source code and it appears that the hook is called AFTER the plugin has been installed, meaning the plugin files are already overwritten and cannot be backed up anymore.
Is there a way to accomplish this hook or is wordpress missing this functionality to call a function before the plugin update progress is initiated ?
You can use upgrade_pre_install filter of WordPress which is executed before upgrade start to deactivate the plugins. check the snippet below, hope this will help to play around plugin backup before updates.
add_filter( 'upgrader_pre_install', 'deactivate_plugin_before_upgrade_callback', 10, 2 );
function deactivate_plugin_before_upgrade_callback( $return, $plugin ) {
if ( is_wp_error( $return ) ) { //Bypass.
return $return;
}
// When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
if ( wp_doing_cron() ) {
return $return;
}
$plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
if ( empty( $plugin ) ) {
return new WP_Error( 'bad_request', $this->strings['bad_request'] );
}
if ( is_plugin_active( $plugin ) ) {
//You can play with plugin zip download over here
//Deactivate the plugin silently, Prevent deactivation hooks from running.
deactivate_plugins( $plugin, true );
}
return $return;
}
Related
I’m using Easy Digital Downloads for my Wordpress webshop. After someone buys a item it needs to add credits to the MySQL database. I got this working by adding PHP code to shortcode-receipt.php.
This is working correct but when I reload the receipt via browser or mail the PHP code will fire again:
php
<?php
if( edd_is_payment_complete( $payment->ID ) && edd_receipt_show_download_files( $item['id'], $edd_receipt_args, $item ) ) :
?>
Could someone help me out here?
What is the best method to fire PHP code when a payment is successful?
Thanks in advance!
There is action edd_complete_purchase which fires when order is completed.
So, in your case, I would remove the code from the shortcode and created an plugin. Inside the plugin should be something like this
function my_edd_receipt( $payment_id ){
if( edd_is_payment_complete( $payment_id ) && edd_receipt_show_download_files( $item['id'], $edd_receipt_args, $item ) ) :
}
add_action( 'edd_complete_purchase', 'my_edd_receipt');
Good day, I need a plugin to be self-deactivated once the mail is sent to the site owner. However, when I run on local machine the plugins still active in my admin panel.
My code :
if(count($result) == 0){
// Send the mail
send_to_mail();
// self deactivation of this plugin
add_action( 'init', 'deactivate_cronjob_plugin' );
}
// deactivate the plugin
function deactivate_cronjob_plugin(){
if ( is_plugin_active('myPlugin/cron_job.php') ) {
deactivate_plugins('myPlugin/cron_job.php', true);
}
}
I'm using Wordpress 4.9.6, I'm glad if there's any help. Thank you and have a good day.
You need the hole path to the plugin file, like
deactivate_plugins( plugin_basename( __FILE__ ) );
Also the small note that the function is_plugin_active is not necessary. The deactivation works only, if the plugin is active.
I load wordpress with my php code like this to do actions:
define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
require( './wp-load.php' );
How could I also disable plugins temporarily when I want to add posts, etc? This would be to use less server resources...
I don't believe there's actually a good way to do this. The best way is probably to run your function before plugins are loaded. Otherwise you're looking at programatically renaming the /plugins folder or WP_PLUGIN_[DIR/URL] constants which just screams "error prone"
Effectively you can hook into the first action hook available, which is muplugins_loaded: Source. Especially if you have no mu-plugins to be run, it should be run almost instantaneously:
add_action( 'muplugins_loaded', 'run_before_plugins_load' );
function run_before_plugins_load(){
if( condition == met ){
// Insert your post here
if( $post_id = wp_insert_post( $my_post ) ){
exit(); // Post inserted, stop processing anything.
} else {
wp_die( 'Post not inserted' );
}
}
}
From there you'll have inserted a post and stopped any further propagation. Of course, you can replace exit and wp_die with whatever you want - it's just the fastest way I can see to run a WP function without actually loading plugins.
I am developing a custom plugin for woocommerce. For now i support it for few version of woocommerce. So i want to check and show incompatibility error if some is using lower version of woocommerce than the version i minimal support.
I want to show the error message on plugin page in admin panel under the my plugin listed.
I have function to get woocommerce version and checking incompatibility using if else condition. But i have no idea how to display the error message as i want.
So please help.
Thanks in Advance.
This is how I do it in my own plugin:
add_action( 'plugins_loaded', 'so_31217783_version_test' );
function so_31217783_version_test(){
$required_woo = '2.1.0';
if ( ! defined( 'WC_VERSION' ) || version_compare( WC_VERSION, $required_woo, '<' ) ) {
add_action( 'admin_notices', 'so_31217783_admin_notice' );
return false;
}
// add the rest of your actions here
// they will only be triggered if the
// version test has been passed
}
function so_31217783_admin_notice() {
echo '<div class="error"><p>' . sprintf( __( 'My custom plugins requires at least WooCommerce version %s in order to function. Please upgrade WooCommerce.', 'your-custom-function' ), $required_woo ) . '</p></div>';
}
The base explanation is that you check the version of WooCommerce very early on and then shut down your plugin if the minimum version is not met. You also add a function to the admin_notices hook so that you can tell the user what has happened.
So here i find myself, again, struggling with wordpress wysiwyg editor.
a client of mine requested to migrate his website to WP. No probs, a breeze :).
Really was easy, migrated from one DB structure to the other, and everything went OK.
Now I have a problem. The old site, used an editor that added <br> and <p> tags to the content in order to format it (sounds legit to me). But wordpress will not allow these tags. whenever the client tries to edit a post, WP removes all the HTML tags it considers "illegal".
So I went on the search. First I tried to install some recommended plugins I found for this problem (such as this one). Didn't work at all for me (for some others it did i believe)...
Then I found a post that told me to add a function to the function.php file which will remove the filters :
function mod_mce($initArray) {
$initArray['verify_html'] = false;
return $initArray;
}
add_filter('tiny_mce_before_init', 'mod_mce');
and also this:
function my_tinymce( $init ) {
$ext = 'div[id|name|class|style]';
if ( isset( $init['extended_valid_elements'] ) ) {
$init['extended_valid_elements'] .= ',' . $ext;
} else {
$init['extended_valid_elements'] = $ext;
}
return $init;
}
add_filter( 'tiny_mce_before_init', 'my_tinymce' );
functions from this thread.
Nope, didn't work also...
Someone - any idea? It seems so silly, but there is so much debate around this subject...
Thanks
You may try this to remove filters that wpautop uses to filter content and excerpt, just put these in your funcions.php file
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
Reference: WordPess wpautop
To allow older content with p and br to load in tinyMCE
function my_tinymce_config( $init ) {
$init['remove_linebreaks'] = false;
$init['convert_newlines_to_brs'] = true;
$init['remove_redundant_brs'] = false;
return $init;
}
add_filter('tiny_mce_before_init', 'my_tinymce_config');
Reference: tinyMCE Configuration look at Cleanup/Output and try playing with these.
Another way could be helpful Reference
tinyMCE.init({
...
verify_html : false
});