How to know which website has installed our wordpress plugin? - wordpress

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.

Related

How to disable specific plugin on Wordpress backend / edit product page

I try to find a solution to disable specific plugins on Wordpress admin area. The problem is that for building WooCommerce shops I use Divi Builder, which on product page can sometimes use 50mb of resources when you try to edit it... if I disable some plugins over there then the load time would be much faster. I've found the following code on other topic:
add_filter( 'option_active_plugins', 'lg_disable_cart66_plugin' );
function lg_disable_cart66_plugin($plugins){
if(strpos($_SERVER['REQUEST_URI'], '/store/') === FALSE AND strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === FALSE) {
$key = array_search( 'cart66/cart66.php' , $plugins );
if ( false !== $key ) unset( $plugins[$key] );
}
return $plugins;
}
But don't know how to modify it, so it only disables the chosen plugin on backend. In other words: I don't want the plugin to load when I edit WooCommerce product page.
Some help will be really appreciated.
As ‘option_active_plugins’ is fired before any plugin is loaded we need to drop our code down into mu-plugins directory. Please also remember that those plugins are run before the main query is initialized – that is why we don’t have any access to many functionalities, especially conditional tags which will always return false.
Please paste below code or download gist in mu-plugins folder in your wp-content folder. It would disable the plugin in post & page areas only.
<?php
/*
Plugin Name: Disable Plugin for URl
Plugin URI: https://www.glowlogix.com
Description: Disable plugins for for specific backend pages.
Author: Muhammad Usama M.
Version: 1.0.0
*/
add_filter( 'option_active_plugins', 'disable_plugins_per_page' );
function disable_plugins_per_page( $plugin_list ) {
// Quit immediately if not post edit area.
global $pagenow;
if (( $pagenow == 'post.php' || $pagenow == 'edit.php' )) {
$disable_plugins = array (
// Plugin Name
'hello.php',
'developer/developer.php'
);
$plugins_to_disable = array();
foreach ( $plugin_list as $plugin ) {
if ( true == in_array( $plugin, $disable_plugins ) ) {
//error_log( "Found $plugin in list of active plugins." );
$plugins_to_disable[] = $plugin;
}
}
// If there are plugins to disable then remove them from the list,
// otherwise return the original list.
if ( count( $plugins_to_disable ) ) {
$new_list = array_diff( $plugin_list, $plugins_to_disable );
return $new_list;
}
}
return $plugin_list;
}
You can replace $disable_plugins with the required list of plugins to disable.

Over write plugin templates -- Child plugin concept

Is that possible for overwriting a plugin templates via making a folder in theme file and copy the file in that folder and altering the content [both folder and file name have the same name as arranged in plugin ].
Generally we altering the woocommerce page templates by following these system .Woothemes has implemented something similar for their woocommerce plugin. You copy files from /wp-content/plugins/woocommerce <-->to<--> /wp-content/themes/yourthemes/woocommerce and WP automatically uses the files in the theme folder rather than the plugin folder. This way users can make customizations to their plugins without losing them to a plugin update..Is that possible for all plugin ?
If no , what code that make woocommerce plugin to change the style based on the file inside our theme folders woocoomerce folder ?
or What about CHILD PLUGIN concept ?
is there any possible way ?
Woocommerce checks first does file /wp-content/themes/yourthemes/woocommerce exists and reqire it. If not it require general template from /wp-content/plugins/woocommerce
Simple possible solution in your plugin, you can use.
function loadTemplate( $template_name ){
$plugin_path = plugin_dir_path(__FILE__);
$original_template = $plugin_path . "templates/" . $template_name . ".php";
$theme_path = get_template_directory();
$override_template = $theme_path . "/myplugin/" . $template_name . ".php";
if(file_exists($override_template)){
include( $override_template );
}
else{
include( $original_template );
}
}
And now you can use as you want your loadTemplate() function like so:
function load_teplate_after_content( $template_name ) {
loadTemplate( 'example' );
// Now it will check first for
// wp-content/themes/yourtheme/myplugin/example.php
// and load, if not exists it will load original template from
// /wp-content/plugins/myplugin/templates/example.php
}
add_filter( 'the_content', 'load_teplate_after_content' );
Of course remember to prefix your functions or put it in a Class. This is just simple example.
Not tested. May be some errors.
EDIT:
Just to answer all questions. Here is precisely how woocommerce is doing this
/**
* Get template part (for templates like the shop-loop).
*
* #access public
* #param mixed $slug
* #param string $name (default: '')
* #return void
*/
function wc_get_template_part( $slug, $name = '' ) {
$template = '';
// Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php
if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) {
$template = locate_template( array( "{$slug}-{$name}.php", WC()->template_path() . "{$slug}-{$name}.php" ) );
}
// Get default slug-name.php
if ( ! $template && $name && file_exists( WC()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) {
$template = WC()->plugin_path() . "/templates/{$slug}-{$name}.php";
}
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php
if ( ! $template && ! WC_TEMPLATE_DEBUG_MODE ) {
$template = locate_template( array( "{$slug}.php", WC()->template_path() . "{$slug}.php" ) );
}
// Allow 3rd party plugin filter template file from their plugin
if ( ( ! $template && WC_TEMPLATE_DEBUG_MODE ) || $template ) {
$template = apply_filters( 'wc_get_template_part', $template, $slug, $name );
}
if ( $template ) {
load_template( $template, false );
}
}
EDIT2:
Is that possible for all plugin
It's possible for all plugins which have this feature implemented.
or What about CHILD PLUGIN concept ?
There is no general answer. If plugin provide an API to its functionallities, you could create such child plugin. But depending directly on current plugin code is bad idea. When your parent plugin(1) is gonna change, your plugin will break if you are using function that has been removed or edited.
But as I said, if plugin provide consistent API, and it's enough to write such functionaliity, then yes. But not for all plugins. At least without editing plugins itself.
(1) There is no such thing like child plugin or parent plugin oficially.

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' );

Automatically execute plugin in wordpress without activating them?

Whenever we use any plugin in wordpress , we need to go to plugin option and then we have to activate them to use them , its fine !
Now my question is
what if someone wants to execute the plugin by default without activating them ?
It means just install that plugin and that plugin will automatically execute on our site without any activation.
Thanks Mubeen for your answer but i just found another solution which is very simple and easy to understand !
Just create a folder name
mu-plugins
folder directory should be
/wp-content/mu-plugins
just download any plugin from www.wordpress.com and extract them and simply copied them in this folder , you will see a new tab in your wordpress plugins option as
Must-Use
the plugins under this tab will automatically executed on your site but there is a problem that if you want to deactivate that plugin then you have to delete that plugin from mu-plugins folder.
source:
http://justintadlock.com/archives/2011/02/02/creating-a-custom-functions-plugin-for-end-users
You can use this code for auto activation WordPress plugin, this will help you to solve your auto activation plugin problem.
<?php
// example on admin init, control about register_activation_hook()
add_action( 'admin_init', 'your_activate_plugins_function' );
// the exmple function
function your_activate_plugins_function() {
if ( ! current_user_can('activate_plugins') )
wp_die(__('You do not have sufficient permissions to activate plugins for this site.'));
$plugins = FALSE;
$plugins = get_option('active_plugins'); // get active plugins
if ( $plugins ){
// plugins to active
$pugins_to_active = array(
'hello.php', // Hello Dolly
'adminimize/adminimize.php', // Adminimize
'akismet/akismet.php', // Akismet
'find-any-think/create-plugin-index.php' // Find any think Plugin
);
foreach ( $pugins_to_active as $plugin ) {
if ( ! in_array( $plugin, $plugins ) ) {
array_push( $plugins, $plugin );
update_option( 'active_plugins', $plugins );
}
}
} // end if $plugins
}
?>
Thanks, I Hope your problem will be solve by this code.

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

Resources