Triggering wp_update_plugins cron to automatically update plugins in WordPress - wordpress

I am trying to force plugin updates with WordPress, but it doesn't seem to be working. I need to force these updates within a custom plugin itself.
I added the following filter into my plugin:
add_filter( 'auto_update_plugin', '__return_true' );
I made sure that nothing in the wp_config file disallows auto-updates. Even though I didn't need to, to be safe I also set:
define( 'FS_METHOD', 'direct' );
and define( 'WP_AUTO_UPDATE_CORE', true );
I then installed the Advanced Cron Manager plugin to trigger the wp_update_plugins event, but this did not update any plugins.
I decided to simply call the wp_maybe_auto_update() function within my plugin on init... and it worked and updated my plugin - but also disabled it!
I am wondering if anyone knows why running the wp_update_plugins cron event wouldn't be updating any plugins? There must be a really simple solution here that I'm missing! Your help would be much appreciated!

You could use the following function to update a plugin programatically:
function upgrade_plugin( $plugin_slug ) {
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
wp_cache_flush();
$upgrader = new Plugin_Upgrader();
$upgraded = $upgrader->upgrade( $plugin_slug );
return $upgraded;
}
& you could use that in conjunction with get_plugins
// Check if get_plugins() function exists. This is required on the front end of the
// site, since it is in a file that is normally only loaded in the admin.
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
and then loop over it to update the plugins
foreach ( $all_plugins as $key => $value ) {
upgrade_plugin( $key );
}
I found this article useful when putting together this answer: https://wpreset.com/programmatically-automatically-download-install-activate-wordpress-plugins/

From the answer above I got this to work, you just need to remove the extra / from include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader-skin.php';
to make it: include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';

When I am relying on a cron event (Testing with WP Crontrol plugin) to take care of plugin update, following code seems to be working with given required files:
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . 'wp-admin/includes/misc.php';
$upgrader = new Plugin_Upgrader();
$result = $upgrader->upgrade($plugin_slug);
if (is_wp_error($result) || !$result) {
// Check if error, else updated successfully
}

Related

WordPress replace header.php from a plugin

I have searched and cant find any answer.
How can I replace the themes header.php template from a custom plugin?
Everywhere I look it seems like it cant be done, and you can only change template parts from a theme/child theme.
But how does plugins like Elementor page builder do it then in their theme builder?
Thanks,
Daniel
I also needed to include a custom header.php from my plugin. Because you can register templates like page.php, post.php or archive.php from a plugin via the ${type}_template hook, I thought it shouldn't be that difficult to do the same for the header.php, but you can't load a custom header.php from directories other than parent or child theme's one.
When traversing down the get_header function you'll see the locate_template function being invoked to load the header template. But there is no way to hook into that function and to replace the path for the loaded header template:
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
$located = STYLESHEETPATH . '/' . $template_name;
break;
} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
$located = TEMPLATEPATH . '/' . $template_name;
break;
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
}
if ( $load && '' !== $located ) {
load_template( $located, $require_once, $args );
}
return $located;
}
One solution to bypass this limitation would be to get rid of the get_header function within your plugin's templates and to include the plugin's header.php via require or require_once statement from within the plugin's templates. But than you will have to implement your own get_header hook and locate_template functionality to enable replacement from the parent or child theme. See this answer for more information on limitations when omitting the get_header function:
https://wordpress.stackexchange.com/a/5195/80177
I have experience using a similar page-builder to elementor called site origin.
To the best of my knowledge wordpress page-builders don't manipulate or replace the themes template files. Instead, a lot of the page-builder content is displayed in the front end with the_content() found in your template file. In addition, it appends a bunch of css and js scripts to the web page in order to handle the styling of the elements and interactivity.
As for your original question... If I understand correctly, there's no need for this plugin because the header.php file can be freely edited/replaced in the theme editor!
In the admin section, go to 'appearance' > 'editor'. Then under "theme files", select the template for header.php.
I used this solution
add_action('get_header', 'wpbet_replace_theme_header');
function wpbet_replace_theme_header(){
require plugin_dir_path( __FILE__ ) . 'templates/headers/header-padrao.php';
$templates = [];
$templates[] = 'header.php';
remove_all_actions( 'wp_head' );
ob_start();
locate_template( $templates, true );
ob_get_clean();
}
Elementor does not replace the header.php. It adopts whatever header.php the active theme has. It simply takes over the_content() part.
header.php is essential part & asset of a theme.
You may build a page template with a different header (not different header.php)

How can I get current version of custom plugin in wordpress

I want to compare the current version of my custom plug-in with update version of plug-in.
I get updated version of plug-in in response from server.
How can I get current version of my plug-in?
You can use get_file_data() function which is available on frontend and backend. For example:
get_file_data('/some/real/path/to/your/plugin', array('Version'), 'plugin');
if you use get_plugin_data() on the frontend it will throw an error Call to undefined function get_plugin_data(). Here is the correct way to get plugin header data.
if ( is_admin() ) {
if( ! function_exists( 'get_plugin_data' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$plugin_data = get_plugin_data( __FILE__ );
echo "<pre>";
print_r( $plugin_data );
echo "</pre>";
}
You could save you version in the options table for easy retrieval. But you can also use get_plugin_data for more details about a given plugin.
<?php
$data = get_plugin_data( "akismet/akismet.php", false, false );
?>
Please note that get_plugin_data is only available in the WordPress admin, it's not a front-end available function.
For users who land on this question to find out the current version of WordPress itself use this function.
// it will show only numeric value i.e 5.8.2
echo get_bloginfo( 'version' );

How can I disable WordPress plugin updates?

I've found a great plugin for WordPress under GPLv2 license and made a lot of changes in source code, plugin does something else now.
I modified author (with original plugin author's credits), URL, version number (from xxx 1.5 to YYY 1.0).
Everything works great, but when WordPress checks for plugin updates it treats my plugin YYY 1.0 as xxx 1.0 and displays notification about available update.
My changed plugin YYY 1.0 was installed by copying files from my computer, not from WP repository.
What else do I have to change?
Disable plugin update
Add this code in your plugin root file.
add_filter('site_transient_update_plugins', 'remove_update_notification');
function remove_update_notification($value) {
unset($value->response[ plugin_basename(__FILE__) ]);
return $value;
}
Put this code in the theme functions.php file. This is working for me and I'm using it. Also this is for specific plugin. Here you need to change plugin main file url to match to that of your plugin.
function my_filter_plugin_updates( $value ) {
if( isset( $value->response['facebook-comments-plugin/facebook-comments.php'] ) ) {
unset( $value->response['facebook-comments-plugin/facebook-comments.php'] );
}
return $value;
}
add_filter( 'site_transient_update_plugins', 'my_filter_plugin_updates' );
Here:
"facebook-comments-plugin" => facebook comments plugin folder name
"facebook-comments.php" => plugin main file.this may be different like index.php
Hope this would be help.
The simplest and effective way is to change the version of the plugin which you don't want to get update.
For an example
if I don't want wptouch to get updated, I open it's defination file, which is like:
/*
Plugin Name: WPtouch Mobile Plugin
Plugin URI: http://www.wptouch.com/
Version: 4.0.4
*/
Here in the Version change 4.0.4 to 9999
like:
/*
Plugin Name: WPtouch Mobile Plugin
Plugin URI: http://www.wptouch.com/
Version: 9999
*/
In the plugin file, there will be a function that will check for updates. The original author could have named this anything, so you will have to go through the code and check each function and what it does. I would imagine the function will be quite obvious as to what it does.
Alternatively you can add this to your plugin file:
add_filter( 'http_request_args', 'dm_prevent_update_check', 10, 2 );
function dm_prevent_update_check( $r, $url ) {
if ( 0 === strpos( $url, 'http://api.wordpress.org/plugins/update-check/' ) ) {
$my_plugin = plugin_basename( __FILE__ );
$plugins = unserialize( $r['body']['plugins'] );
unset( $plugins->plugins[$my_plugin] );
unset( $plugins->active[array_search( $my_plugin, $plugins->active )] );
$r['body']['plugins'] = serialize( $plugins );
}
return $r;
}
Credits: http://developersmind.com/2010/06/12/preventing-wordpress-from-checking-for-updates-for-a-plugin/
add_filter('site_transient_update_plugins', '__return_false');
in function.php add above code and disable all plugins updates
Add this line to wp-config.php to disable plugin updates:
define('DISALLOW_FILE_MODS',true);
One easy solution was to change the version of plugin in plugin file.
For example if plugin version is 1.2.1. You can make it like below (100.9.5 something that plugin author will never reach to )
<?php
/*
* Plugin Name: Your Plugin Name
* Description: Plugin description.
* Version: 100.9.5
*/
Here's an updated version of Mark Jaquith's script:
WP Updates have switched to HTTPS
Unserialize was blocked on my shared hosting
This uses json_decode and json_encode instead
Credit: Block Plugin Update
.
add_filter( 'http_request_args', 'widget_disable_update', 10, 2 );
function widget_disable_update( $r, $url ) {
if ( 0 === strpos( $url, 'https://api.wordpress.org/plugins/update-check/' ) ) {
$my_plugin = plugin_basename( __FILE__ );
$plugins = json_decode( $r['body']['plugins'], true );
unset( $plugins['plugins'][$my_plugin] );
unset( $plugins['active'][array_search( $my_plugin, $plugins['active'] )] );
$r['body']['plugins'] = json_encode( $plugins );
}
return $r;
}
Disable plugin updates manually:
Open functions.php file (go to your activated themes folder)
Copy and paste the following code:
remove_action( 'load-update-core.php', 'wp_update_plugins' );
add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );
Save changes, and you’re done
Just for completeness, here is one more plugin meant to block updates of selected other plugins:
https://github.com/daggerhart/lock-plugins
Some information about its background and mode of function can be found here (in German).

How do I get the name of the file that is being used to render the current page?

Let's say I have a WordPress installation, with a page named "About". If I go to http://example.com/about, I know from WordPress' template hierarchy page that I'm looking at the theme file page.php.
I'm wondering if there's a way to display that fact (for theme debugging) on the page somewhere? Like what function (or code) would I call to display the current PHP page that is being used to render the page I'm looking at.
I could do something with $_SERVER['PHP_SELF'], but I'm looking for a way where I don't have to edit every PHP file. Like something that spits out the list of files it's using as the pages are called.
It can be printed in the Html source code like this:
add_action( 'wp_head', 'so_9405896_show_template', 999 );
function so_9405896_show_template() {
global $template;
echo '
<!--
TEMPLATE = ' . basename($template) .'
-->
';
}
Or for easier visualization, directly in the content with this:
add_filter( 'the_content', 'so_9405896_the_content_filter', 20, 1 );
function so_9405896_the_content_filter( $content )
{
if( is_admin() || !current_user_can( 'administrator' ) )
return $content;
global $template;
$the_templ = '<strong style="background-color: #CCC;padding:10px">TEMPLATE = '
. basename( $template ) . '</strong><br />';
$content = sprintf( $the_templ . '%s', $content );
return $content;
}
Which results in:
As far as I've seen there's no built in option to enable such logging, only for errors.
I'm not sure what editor you use, but most common text editors allow you to do a find replace across an entire folder.
I'd suggest doing a temporary replace on includes and require's to add an echo of the PHP_SELF. Just make sure to add a comment or something before the echo so that you can easily replace them with nothing when you're done.
A quick search on the WordPress plugin repository brings up a WordPress Debug Bar Template Trace.
I just manually type it into the template, i.e. ARCHIVE.PHP, CATEGORY-1.PHP when I'm building it. Just remember to delete it once the site goes live.
Simple and easy, if not so graceful.
For those looking for a newer answer:
<?php if ( is_user_logged_in() ) { global $template; echo basename($template); } ?>
This checks if a user is logged in, then only shows the template name. This can be handy if you need to hack your way on a live site without adding functions.

Change language only on theme

I want to keep the admin in english but the theme in another language. By adding language files to the theme directory nothing happens, the option to switch language wont come up. Is this possible without adding languages to wp-content?
I found a solution using the locale filter and load theme textdomain
The solution looks like this:
define('LOCALE', 'en');
add_action( 'after_theme_setup', 'site_setup' );
function site_setup() {
//Make the theme available for translation
load_theme_textdomain( 'mytheme', get_template_directory() . '/languages' );
$locale = get_locale(); //Will get what you defined earlier
$locale_file = get_template_directory() . "/languages/$locale.php";
if ( is_readable( $locale_file ) ) require_once( $locale_file );
}

Resources