Last active
March 20, 2017 16:22
-
-
Save donaldG/90b2ccce8798f3b0d750eb71ee0c3f02 to your computer and use it in GitHub Desktop.
Replace WordPress srcset on local with live
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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