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.
Related
I code custom plugins for Wordpress. I use Microsoft Visual Studio Code to do the coding and FileZilla to transfer the files.
With almost every php file that I code, I'm not sure if it's something that's happening when I download my plugin and then upload it to a new site, or if it's when I install it on a new site from a zip file, but when I check the code I find that the entire code has ended up with double line breaks!
i.e.,
function gdvcaddons_admin() {
add_menu_page("GD Addons Settings", "GD Addons Settings", "administrator", "gdvcaddons-settings", "gdvcaddons_settings_page");
}
add_action('admin_menu', 'gdvcaddons_admin');
// DEFINE OPTIONS TO BE SAVED //
function gdvcaddons_settings() {
register_setting( 'gdvcaddons-settings-group', 'recaptcha_site_key' );
register_setting( 'gdvcaddons-settings-group', 'recaptcha_private_key' );
}
add_action( 'admin_init', 'gdvcaddons_settings' );
// DECLARE FILE CONTAINING OPTIONS FORM //
function gdvcaddons_settings_page(){
include('inc/gdvcaddons-settings-form.php');
}
Becomes:
function gdvcaddons_admin() {
add_menu_page("GD Addons Settings", "GD Addons Settings", "administrator", "gdvcaddons-settings", "gdvcaddons_settings_page");
}
add_action('admin_menu', 'gdvcaddons_admin');
// DEFINE OPTIONS TO BE SAVED //
function gdvcaddons_settings() {
register_setting( 'gdvcaddons-settings-group', 'recaptcha_site_key' );
register_setting( 'gdvcaddons-settings-group', 'recaptcha_private_key' );
}
add_action( 'admin_init', 'gdvcaddons_settings' );
// DECLARE FILE CONTAINING OPTIONS FORM //
function gdvcaddons_settings_page(){
include('inc/gdvcaddons-settings-form.php');
}
It is quite a problem because it keeps causing Yoast SEO's sitemaps to break and I keep having to open all my php files via FTP and do regex search/replace "\s\n" with "\n".
Does anyone have any idea what might be causing this issue and how I can prevent it from reoccurring?
Try to change transfer type
For single FTPs
Global Settings.
I'm trying to change the flag to a custom flag in svg, and I found this "pll_custom_flag" function in their document(https://polylang.pro/doc/filter-reference/)
There, it says "The filter needs to be added in a plugin or mu-plugin before the action ‘plugins_loaded’ has been fired. It can’t work in the functions.php of a theme."
However, I don't know where and how to add this action specifically.
Here is the action I I customized for my website.
add_filter( ‘pll_custom_flag’, ‘pll_custom_flag’, 10, 2 );
function pll_custom_flag( $flag, $code ) {
$flag[‘url’] = "http://example.com/wp-content/polylang/flags-custom/{$code}.svg";
$flag[‘height’] = 24;
return $flag;
}
I also added two svg images to a new directory(flags-custom) I created under the polylang directory.
I have almost zero knowledge in PHP, and I would greatly appreciate it if anyone could help me on this.
Thank you.
Solution 1:
You need to create plugin for this filter. Create one and put this filter into it.
Solution 2 (simpler):
Put file (example: polylang_filters.php) into /wp-content/mu-plugins with this filter.
After 1 or 2 go to Polylang Settings -> Url modification and click save. No changes required just click save to update urls.
If you have your custom flags in folder they will show up.
PS.
I would prefer to keep my flags in theme folder:
add_filter( 'pll_custom_flag', 'pll_custom_flag', 10, 2 );
function pll_custom_flag( $flag, $code ) {
$flag['url'] = get_template_directory_uri()."/img/lang_flags/{$code}.svg";
$flag['width'] = 20;
$flag['height'] = 20;
return $flag;
}
In my flag folder I have flags:
gb.svg
de.svg
etc...
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;
}
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.