Register a JS file in WordPress on Localhost Only
Today I was adding a live-reload script to a WordPress site, so that I could refresh the page on the fly with Grunt. I only wanted to register said script on my local however. This was what I came up with.
PHP
if (in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) {
wp_register_script('livereload', 'http://localhost:35729/livereload.js?snipver=1', null, false, true);
wp_enqueue_script('livereload');
}
What’s happening here is I’m grabbing the remote address from the $_SERVER global variable provided in PHP. This will return the IP address from which the user is viewing the current page.
Since localhost maps to either 127.0.0.1 or ::1, depending on the Internet Protocol version, I’ll check the current IP address against an array of both. If either matches, then we’ll load the javascript file.
Comments (2)
Leave a Reply
This is a good workaround. I think WordPress should have support for different enviroments: test, develop and production to make it easier.
Link to commentThis article is really a fastidious one it helps new internet users, who are wishing in favor of blogging.
Link to comment