Skip to content

Instantly share code, notes, and snippets.

@timelsass
Created February 16, 2018 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelsass/c9f0ceb38b3f5b1fd17f334eadf48b41 to your computer and use it in GitHub Desktop.
Save timelsass/c9f0ceb38b3f5b1fd17f334eadf48b41 to your computer and use it in GitHub Desktop.
Filter AJAX Requests and WP calls to BGAPI to Dev.
<?php
/**
* Plugin name: BoldGrid Filter to Dev
* Description: Filter AJAX Requests and WP calls to BGAPI to Dev.
* Plugin URI: https://github.com/timelsass
* Author: Tim Elsass
* Author URI: https://tim.ph
*/
// Filter BoldGrid HTTP Requests made via WordPress.
function bg_filter_wp_to_dev( $preempt, $r, $url ) {
$regex = '/(wp-(?:assets|staging|preview)|api)+(?!-dev)(.*\.boldgrid\.com)+/i';
if ( preg_match( $regex, $url ) ) {
$url = preg_replace( $regex, '$1-dev$2', $url );
wp_remote_request( $url, $r );
$preempt = true;
}
return $preempt;
}
add_action( 'pre_http_request', 'bg_filter_wp_to_dev', 10, 3 );
// Filter BoldGrid HTTP Requests made via AJAX.
function bg_filter_js_to_dev() { ?>
<script>(function($){$.ajaxPrefilter(function(o){o.url=o.url.replace(/(wp-(?:assets|staging|preview)|api)+(?!-dev)(.*\.boldgrid\.com)+/i,'$1-dev$2')})})(jQuery)</script><?php
}
add_action( 'admin_head', 'bg_filter_js_to_dev' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment