Skip to content

Instantly share code, notes, and snippets.

@donaldG
Last active March 20, 2017 16:22
Show Gist options
  • Save donaldG/90b2ccce8798f3b0d750eb71ee0c3f02 to your computer and use it in GitHub Desktop.
Save donaldG/90b2ccce8798f3b0d750eb71ee0c3f02 to your computer and use it in GitHub Desktop.
Replace WordPress srcset on local with live
<?php
//this goes in the bottom of your LOCAL config file
//these should already be set but in case they aren't:
//$env = $_SERVER['SERVER_NAME'];
//$domain = 'yourSiteName/';
//$local_url = 'http://' . $env . '/' . $domain;
//define('WP_ENV', $env);
//define('WP_HOME', $local_url);
//define('WP_SITEURL', $local_url);
function filter_image_srcset( $data ) {
global $wpdb;
$siteurl = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl' LIMIT 1" );
$siteurl = $siteurl->option_value . '/';
$new_data = array();
foreach ( $data as $key => $val ) {
foreach ( $val as $val_key => $val_val ) {
if ( strpos( $val['url'], WP_SITEURL ) !== false ) {
$val['url'] = str_replace( WP_SITEURL, $siteurl, $val['url'] );
}
$new_data[ $key ] = $val;
}
}
$data = array_replace( $data, $new_data );
return $data;
}
add_filter( 'wp_calculate_image_srcset', 'filter_image_srcset' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment