3

The WordPress Codex points to an extremely simple way to change the URL of the wp-content directory, which is, adding this in wp-config.php:

define( 'WP_CONTENT_URL', 'http://example.com/wp-content');

And that does the job of serving most of the JavaScript and CSS files on my website from the different domain, except two.

http://mywebsite.com/wp-includes/js/comment-reply.js
http://mywebsite.com/wp-includes/js/quicktags.js

The reason is very clear — these JavaScript files are served from within the wp-includes directory, which is not affected by the aforementioned rule.

So, is there a similar or simple method to have them served from example.com i.e. the domain of my choice? or is unregistering and enqueuing the scripts from a custom URL (done in functions.php) the only way?

3 Answers 3

3

Okay, I've tried numerous (literally) codes and methods, and failed miserably. So, like I said in the question, the only safe way seems to be re-registering (unregistering and enqueuing) the scripts.

The Scripts:

http://mywebsite.com/wp-includes/js/comment-reply.js
http://mywebsite.com/wp-includes/js/quicktags.js

Should be served from:

http://example.com/wp-includes/js/comment-reply.js
http://example.com/wp-includes/js/quicktags.js

Code in functions.php:

add_action('wp_enqueue_scripts','wpse56742_register_script');
function wpse56742_register_script(){

    //Register and enqueue Comment Reply script
    wp_deregister_script('comment-reply');
    wp_register_script('comment-reply', 'http://example.com/wp-includes/js/comment-reply.js', false, false);
    wp_enqueue_script( 'comment-reply' );

    //Re-register quicktags script
    wp_deregister_script('quicktags');
    wp_register_script('quicktags', 'http://example.com/wp-includes/js/quicktags.js', false, false, true);
    wp_localize_script( 'quicktags', 'quicktagsL10n', array(
        'wordLookup' => __('Enter a word to look up:'),
        'dictionaryLookup' => esc_attr(__('Dictionary lookup')),
        'lookup' => esc_attr(__('lookup')),
        'closeAllOpenTags' => esc_attr(__('Close all open tags')),
        'closeTags' => esc_attr(__('close tags')),
        'enterURL' => __('Enter the URL'),
        'enterImageURL' => __('Enter the URL of the image'),
        'enterImageDescription' => __('Enter a description of the image'),
        'fullscreen' => __('fullscreen'),
        'toggleFullscreen' => esc_attr( __('Toggle fullscreen mode') ),
        'textdirection' => esc_attr( __('text direction') ),
        'toggleTextdirection' => esc_attr( __('Toggle Editor Text Direction') )
    ));
    wp_enqueue_script( 'quicktags' );

}

As for the code used in wp_localize_script, I got it straight from trunk (you can also get it for your WP version, for example, for WP 3.4, the link would be http://core.svn.wordpress.org/tags/3.4/wp-includes/media.php).


FAQ: Why is quicktags.js loaded in the front-end in the first place? Because I am using the plugin — Basic Comment Quicktags

1
1

I know the question is very old, but I think it may still have the attention of some WordPress fans. Till now, WordPress didn't implement a way to define "WP_INCLUDES_URL" like we can define "WP_CONTENT_URL".

So here is an easy workaround, that would do the trick for scripts loaded from wp-includes, using "script_loader_src" filter:

function filter_wpincludes( $src, $handle ) { 
    $src = str_replace(site_url("/wp-includes"), "https://static.example.com/wp-includes", $src); 
    return $src; 
}; 

// then just add the filter 
add_filter( 'script_loader_src', 'filter_wpincludes', 10, 2 ); 

Of course this code belongs to your functions.php inside your theme. This filter runs on every script being loaded by WordPress, then the function replaces the old domain with the new one.

In the code above, the following link:

 http://mywebsite.com/wp-includes/js/comment-reply.js

Will be replaced with this link:

 https://static.example.com/wp-includes/js/comment-reply.js
-1

The function wp_enqueue_script function has an optional second parameter where you can specify a source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.