Skip to content

Instantly share code, notes, and snippets.

@daggerhart
Created August 25, 2016 21:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save daggerhart/92ae4eb952c44a22762c09d25c95af82 to your computer and use it in GitHub Desktop.
Save daggerhart/92ae4eb952c44a22762c09d25c95af82 to your computer and use it in GitHub Desktop.
Simple plugin that prevents requests for updats for a given list of plugins.
<?php
/*
* Plugin Name: Lock plugin updates
* Description: Prevent plugin updates
* Version: 1.0.0
* Author: daggerhart
*/
add_filter( 'http_request_args', 'lock_plugins_http_request_args', 5, 2 );
/**
* Prevent lookup of certain plugin updates
* Source: https://markjaquith.wordpress.com/2009/12/14/excluding-your-plugin-or-theme-from-update-checks/
*
* @param $request
* @param $url
*
* @return mixed
*/
function lock_plugins_http_request_args( $request, $url ) {
if ( FALSE === strpos( $url, '//api.wordpress.org/plugins/update-check' ) ) {
return $request; // Not a plugin update request. Bail immediately.
}
if ( empty($request['body']['plugins']) ){
return $request;
}
$plugins = json_decode( $request['body']['plugins'], true );
// get a list of locked plugins from somewhere
$locked_plugins = apply_filters('lock_plugins-locked_plugins', array());
foreach( $locked_plugins as $locked_plugin_basename )
{
$active_index = array_search( $locked_plugin_basename, $plugins->active );
unset( $plugins->active[ $active_index ] );
unset( $plugins->plugins[ $locked_plugin_basename ] );
}
$request['body']['plugins'] = wp_json_encode( $plugins );
return $request;
}
@daggerhart
Copy link
Author

Inspiration: https://twitter.com/danielbachhuber/status/768907891294121984

Usage:

add_filter('lock_plugins-locked_plugins', function($plugins){
  $plugins[] = 'akismet/akismet.php';
});

@obstschale
Copy link

obstschale commented Aug 30, 2016

Nice plugin. But I get some nasty PHP Warnings. You try to get properties of non-object.

I fixed 2 things:

  1. First in line 34-39:
foreach( $locked_plugins as $locked_plugin_basename )
  {
    $active_index = array_search( $locked_plugin_basename, $plugins['active'] );
    unset( $plugins['active'][ $active_index ] );
    unset( $plugins['plugins'][ $locked_plugin_basename ] );
  }
  1. In your comment you call the filter but do not return the array:
add_filter('lock_plugins-locked_plugins', function($plugins){
  $plugins[] = 'akismet/akismet.php';

  return $plugins;
});

@daggerhart
Copy link
Author

Awesome, thanks for the help @obstschale. In case something else comes up, I went ahead and made this into a real repo--
https://github.com/daggerhart/lock-plugins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment