-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-local-development-tools.php
77 lines (57 loc) · 1.79 KB
/
wp-local-development-tools.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Plugin Name: WP Local Development Tools
* Description: Bunch of tools that usefull for WP with stage mode
* Version: 0.0.1
* Author: Mohammad Zarei
* Author URI: https://zareidev.ir
* License: GPL v3
*/
add_filter( 'wp_get_attachment_url', function ($url, $post_id) {
if ( !get_production_domain() ) {
return $url;
}
$is_staging = get_post_meta( $post_id, 'staging' );
if ( isset($is_staging[0]) && $is_staging[0] == 'yes') {
return $url;
}
$url = str_ireplace(get_home_url(), get_production_domain(), $url);
return $url;
}, 10, 200 );
add_filter( 'max_srcset_image_width', function ( $max_width ) {
return false;
} );
add_filter( 'wp_calculate_image_srcset', function ( $sources ) {
return false;
});
if (!function_exists('write_log')) {
function write_log($log) {
if (true === WP_DEBUG) {
if (is_array($log) || is_object($log)) {
error_log(print_r($log, true));
} else {
error_log($log);
}
}
}
}
add_filter('the_content', function ($content) {
if (!get_production_domain()) {
return $content;
}
$pattern = "/(src=['\"])(https?:\/\/)([^\/']+)(\/wp-content\/uploads\/[^\s'\"?#]+)(['\"])/i";
$replacement = '$1' . get_production_domain() . '$4$5';
$content = preg_replace($pattern, $replacement, $content);
return $content;
});
add_action( 'add_attachment', function ( $post_ID ) {
add_post_meta( $post_ID, 'staging', 'yes' );
});
if ( !function_exists( 'get_production_domain' ) ) {
function get_production_domain() {
if (defined( 'WP_PRODUCTION_DOMAIN' )) {
return WP_PRODUCTION_DOMAIN;
}
return false;
}
}