Skip to content

Instantly share code, notes, and snippets.

View timwhitlock's full-sized avatar

Tim Whitlock timwhitlock

View GitHub Profile
@timwhitlock
timwhitlock / get-only-wp-json.php
Created February 11, 2017 15:00
Disable non-GET requests to WordPress REST API
<?php
/**
* Plugin Name: Disable POSTing to REST API
* Version: 0
*/
add_filter( 'rest_authentication_errors', function( $access = null ){
if( 'GET' !== $_SERVER['REQUEST_METHOD'] ){
return new WP_Error( 'rest_cannot_access', 'Nope', array( 'status' => 405 ) );
}
@timwhitlock
timwhitlock / disable-wp-json.php
Created February 11, 2017 12:39
Disable WordPress REST API completely
<?php
/**
* Plugin Name: Disable REST API
* Version: 0
*/
// completely disable wp-json access
add_filter( 'rest_authentication_errors', function( $access ){
return new WP_Error( 'rest_cannot_access', 'Bye', array( 'status' => 403 ) );
} );

Keybase proof

I hereby claim:

  • I am timwhitlock on github.
  • I am timwhitlock (https://keybase.io/timwhitlock) on keybase.
  • I have a public key ASAqRSGF2gFgfMTquG-FBo71hJ229f0ZOlYS8-SbbTp-8Qo

To claim this, I am signing this object:

@timwhitlock
timwhitlock / mu-example.php
Created August 20, 2016 11:16
Example of adding an unregistered MU plugin to Loco Translate
<?php
/**
* MU plugins inside directories are not returned in `get_mu_plugins`.
* This filter modifies the array obtained from Wordpress when Loco grabs it.
*
* Note that this filter only runs once per script execution, because the value is cached.
* Define the function *before* Loco Translate plugin is even included by WP.
*/
function add_unregistered_plugins_to_loco( array $plugins ){
@timwhitlock
timwhitlock / dos2unix.sh
Created August 4, 2015 06:04
Simple dos2unix script
#!/bin/bash
# Actually just strips carriage returns
cp -p "$1" "$1.dos"
tr -d '\r' < "$1.dos" > "$1"
rm -f "$1.dos"
@timwhitlock
timwhitlock / iterator-quirks.php
Created May 22, 2015 08:34
Shows the behaviour of array pointer functions called on custom Iterator objects
<?php
/**
* A warning to myself that custom iterators cannot entirely masquerade as native arrays for legacy code compatibility:
*
* - Custom classes implementing the Iterator interface work with `foreach`, but not with native array pointer functions.
* - Built-in iterators do work with `current`, `key` and `next` but the iterator methods cannot be overridden in subclasses.
*/
/**
@timwhitlock
timwhitlock / example.php
Created October 22, 2014 10:25
Warn that deprecated WPLANG constant will be ignored
<?php
/**
* display warning in Wordpress admin that WPLANG constant will be ignored
*/
is_admin() and add_action( 'admin_notices', function(){
if( defined('WPLANG') && WPLANG && 3 < (int) $GLOBALS['wp_version'] ){
echo '<div class="error"><p><strong>Warning</strong> <code>WPLANG</code> is deprecated and should be removed from wp-config.php</p></div>';
}
} );
@timwhitlock
timwhitlock / AssetManager.php
Created August 19, 2014 20:34
Exposes source maps to dynamic Assetic routes in debug mode
<?php
namespace my\TestBundle\Assetic;
use Assetic\Factory\LazyAssetManager;
/**
* Overridden asset manager that exposes source maps referenced in CSS and JS
*/
class AssetManager extends LazyAssetManager {
@timwhitlock
timwhitlock / wp-config.php
Last active November 15, 2017 06:09
Enable mysqi driver in Wordpress >= 3.9
<?php
/* wp-config.php */
/* use mysqli driver, WP>=3.9 */
define('WP_USE_EXT_MYSQL', false );
@timwhitlock
timwhitlock / guzzle-model-arrays.php
Created February 11, 2014 16:25
Testing ways to wrap guzzle models in a typed array
<?php
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Mock\MockPlugin;
use Guzzle\Service\Client;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Service\Resource\Model;
use Guzzle\Tests\GuzzleTestCase;
/**