Created
February 2, 2021 14:30
-
-
Save desrosj/11bca39409058cd87ee8ee520f93db26 to your computer and use it in GitHub Desktop.
A function for toggling auto-update state for WordPress plugins and themes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Changes the auto-update status for plugins and themes. | |
* | |
* @param string $name The name of the plugin or theme to change the auto-update status of. For themes, this should be | |
* the theme slug (`twentytwentyone`). For plugins, this should be the path to the plugin file | |
* relative to the plugins directory (`gutenberg/gutenberg.php`). | |
* @param string $type The type of item to update. Accepted values are `plugin` or `theme`. Default is `plugin`. | |
* @param bool $enable Whether to enable (true) or disable (false). Default is true, or enable. | |
* @return bool True if the value was updated, false otherwise. | |
*/ | |
function change_auto_update( $name, $type = 'plugin', $enable = true ) { | |
if ( 'theme' === $type ) { | |
$key = 'auto_update_themes'; | |
} else { | |
$key = 'auto_update_plugins'; | |
} | |
$auto_update_settings = get_site_option( $key, array() ); | |
if ( $enable ) { | |
if ( in_array( $name, $auto_update_settings, true ) ) { | |
return false; | |
} | |
$auto_update_settings[] = $name; | |
} else { | |
if ( ! in_array( $name, $auto_update_settings, true ) ) { | |
return false; | |
} | |
unset( $auto_update_settings[ array_search( $name, $auto_update_settings, true ) ] ); | |
} | |
return update_site_option( $key, array_unique( $auto_update_settings ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment