How can I get current version of custom plugin in wordpress - 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' );

Related

Triggering wp_update_plugins cron to automatically update plugins in 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
}

How to know which website has installed our wordpress plugin?

We are creating a Wordpress plugin and we want to know the urls of the wordpress websites on which our plugin is installed.
What code should we add to the plugin to receive the url of the wordpress website?
We need this information to see what type of websites are installing our plugin.
Note: We will notify users before downloading the plugin and at the time of activation that we will be receiving there website's url.
In a simple way, You can create a get_urls.php in http://example.com/get_urls.php for receiving and storing URLs.
get_urls.php
<?php
if( isset( $_GET['url'] ) ) {
file_put_contents('urls.log', date('[r] ') . $_GET['url'] . "\n", FILE_APPEND );
}
And add below code to your plugin.
add_action( 'activated_plugin', 'send_url_in_activate', 10, 1 );
function send_url_in_activate( $plugin ) {
if( $plugin !== "PLUGIN_DIR" ) { // e.g: woocommerce/woocommerce.php
return;
}
$response = wp_remote_get( 'http://example.com/get_urls.php?url=' . get_site_url() );
}
Don't forget set your plugin dir.

CMB Admin Checkbox if statement

I have a checkbox on a Wordpress admin options page that if checked I want to call a file. After hours of searching I am still coming up short. I am trying to use the advice here: https://github.com/WebDevStudios/CMB2/wiki/Tips-&-Tricks#using-cmb2-helper-functions-and-cmb2_init as well as a confitional statement inside. Is there any advice on what I am doing wrong?
function cmb2_init_check_field_value() {
$checkbox_value = cmb2_get_field_value( 'compel_option_metabox', 'compel_checkbox', get_queried_object_id() );
if($checkbox_value == yes) {
require_once( $this->directory_path . '/post-types/staff.php' );
require_once( $this->directory_path . '/post-types/sermons.php' );
}
}
add_action( 'cmb2_admin_init', 'cmb2_init_check_field_value' );
The cmb2_admin_init hook is too early,so you can not use get_the_ID() to get the post
I just encountered this as well. I couldn't figure out how to get the field value function to work. However, I did figure out how to get the value using the get_post_meta function.
eg:
$checkbox_value = get_post_meta( get_the_ID(), 'compel_checkbox', true);
if($checkbox_value == 'on') {

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).

Buddypress - Groups navigation / redirect to forum (this again)

If you've customized a BP site before, you'll know that the groups nav bp_get_options_nav(); can be a real barrier in getting a site exactly the way you want it.
There was an older solution to involving a permanent redirect (not ideal for SEM purposes) from home/ to forum/....
`
function redirect_to_forum() {
global $bp;
$path = clean_url( $_SERVER['REQUEST_URI'] );
$path = apply_filters( 'bp_uri', $path );
if ( bp_is_group_home() && strpos( $path, $bp->bp_options_nav['groups']['home']['slug'] ) === false )
bp_core_redirect( $path . $bp->bp_options_nav['groups']['forum']['slug'] . '/' );
}
add_action( 'wp', 'redirect_to_forum' );
`
and this one works in BP 1.5
`
function redirect_to_forum() {
global $bp;
$path = clean_url( $_SERVER['REQUEST_URI'] );
$path = apply_filters( 'bp_uri', $path );
if ( bp_is_group_home() && strpos( $path, $bp->bp_options_nav['groups']['home']['slug'] ) === false )
bp_core_redirect( $path . $bp->bp_options_nav['groups']['forum']['slug'] . 'forum/' );
}
add_action( 'bp_init', 'redirect_to_forum' );
`
Is there no other way of moving functionality around in Buddypress Groups without creating explosions? It would be really nice just to be able to change the include file references in /groups/single/home.php to pull the functionality you wanted. For example...
`
elseif ( bp_group_is_visible() ) :
locate_template( array( 'groups/single/** change this to any file within /single/ **' ), true );
`
If you change the home.php include file reference to forumn's, the forum's display just fine, however the add new topic functions and support do not seem to be dialed in... create a new topic and nothing happens... so in order to tap into forum functionality you actually need to be at the "forum" slug i.e. /forum/... is there any way of getting around this?
To summarize... I'm trying to get the forum's functionality working at the group root i.e. "sitename.com/groups/group-name/" WITHOUT a redirect to "sitename.com/groups/group-name/forumn/"
Any thoughts? suggestions? similar experiences?

Resources