Majeed
Majeed

Reputation: 144

Adding custom JS file in wordpress that get apply on any WP theme.

I can't work on any specific theme file like header.php, footer.php OR function.php within theme directory.

Reason is project always need new theme and theme is get changes in each week. So I want something like that will work on each theme, no matter which theme admin applied.

I tried wp_enqueue_script() but again I need work on theme's function.php file.

Upvotes: 0

Views: 64

Answers (2)

mike-source
mike-source

Reputation: 1015

The only way to do this is to run wp_enqueue_script() from outside of the theme (i.e. NOT in functions.php). The only way you can do that is to use a plugin.

See: http://codex.wordpress.org/Must_Use_Plugins Or: http://codex.wordpress.org/Writing_a_Plugin

Create a plugin by adding a .php file in the /wp-content/plugins/ directory, or possibly even better for this situation create a 'must use' plugin by creating a .php file in the /wp-content/mu-plugins/ directory.

The structure of this .php file should be something like the following:

<?php

/*
Plugin Name: Example Plugin
Description: Any functions that should persist even if the theme changes.
Author: Your Name
Version: 1.0
Author URI: http://yoururl.com
*/

function my_scripts() {
  wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
}
  
add_action('wp_enqueue_scripts', 'my_scripts' );

?>

Upvotes: 2

Marius Neagoe
Marius Neagoe

Reputation: 41

You need to write a plugin that rewrites theme's sections. A good start for creating a plugin is here. If your themes shares the same Javascript you could try to add your things to those Js.

Upvotes: 0

Related Questions