I'm having issues loading jQuery via wp_enqueue_script - I just discovered this is the correct way to pull in jQuery to avoid posible JS conflicts but I can't get it work. I'm using this example:
function my_init() {
if (!is_admin()) {
// comment out the next two lines to load the local copy of jQuery
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2');
wp_enqueue_script('jquery');
}
}
add_action('init', 'my_init');
I'm getting nothing when I check my source files. From what I can see the syntax is correct, I'm attempting to do this on a local WP install using XAMPP if that matters. Has anyone run into a similar issue? I've seen several questions about this but the fix usually comes down to a syntax error. I'm not seeing one. Any help would be much appreciated. Thanks!
Add the code below to your theme's functions.php in place of what you have.
The version of jQuery you were trying to load was outdated by the way.
Is this a custom theme you're building? If so are you using the wp_head and wp_footer tags?
By de-registering the built in version of jQuery and loading jQuery from the Google CDN you're reducing the time it takes to download the file but it isn't necessary. You could just load jQuery with wp_enqueue_script( 'jquery' );
function my_enqueue_jquery() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js', false, '2.0.3' );
wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_jquery' );
Wordpress 3.8 has jQuery.1.10.3.
wp_deregister_script('jquery') is not good for site.
So, you'd rather upgrade wordpress and use higher version of jQuery included in wordpress.
Please review this Wordpress Theme.
It's much easier to customize and has lots of features. Also easy to learn code tips.
Regards. HanaTheme.
Related
I am building my own wordpress theme and I have such a problem. Media library doesn't work, I can't add any image to the page. I am using the latest version of wordpress.
Files available on github https://github.com/krysba/themes-wordpress
When I turn on the standard wordpress theme, the library works :(
I am asking for help in solving this problem.
If you have not tried, the first thing that i'll tring it's enable debug mode on Wordpress from wp-config.php and check if some alerts or errors can help.
define('WP_DEBUG', true);
I think that you must try to comment all function.php and debugging it row by row.
At the first check i've found a wrong use of the function add_theme_support and in the enqeueing files.
For example the right way to user add_theme_support is on the after_setup_theme action:
function my_setup_theme(){
add_theme_support('post-formats',array('gallery','link','image','quote','status','video','audio'));
}
add_action( 'after_setup_theme', 'my_setup_theme' );
Also the right way to enqueing scripts or styles is that:
function add_theme_scripts() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );
See it on this page: https://developer.wordpress.org/themes/basics/including-css-javascript/
On wordpress, using GeneratePress free template, I'm trying to push my custom script after all others.
The big problem is that all the scripts of a plugin (Visual Portfolio) are always loaded at the very end, after my scripts.
I tried to put a very high $priority parameter in the add_action() functions but it doesn't work.
function register_scripts_and_styles() {
wp_enqueue_script('custom-js', get_stylesheet_directory_uri() . '/js/custom-js.js', null, true);
}
add_action( 'wp_enqueue_scripts', 'register_scripts_and_styles', 99999 );
Do you have any idea how to fix this problem?
Thank you very much.
Here's idea, js and css files in WP use different technique to figure out in what order js\css files should be loaded.
here's function
wp_enqueue_script('custom-js', get_stylesheet_directory_uri() . '/js/custom-js.js', array('js-handle-of-some-dependency','js-handle-of-some-other-dependency'), true);
Each script has it's handle, it's first argument to this function, in your case it's custom-js.
Based on digging into plugin's code it's main js has handle visual-portfolio
So you can just enqueue your script with setting it's handle as dependency.
And final solution will be:
function register_scripts_and_styles() {
wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/js/custom-js.js', array( 'jquery', 'visual-portfolio' ), '1', true );
}
add_action( 'wp_enqueue_scripts', 'register_scripts_and_styles' );
I've also added jquery as dependency as I guess you are using jQuery in your js code, you can remove it if not.
I've also set version to '1' - it's a query string version which will be applied to your JS code so when you do update and move code live you can force user's browsers to load a fresh one just changing version here.
And see last argument true, it denotes that your script will be injected in footer.
If your code relies not only on main.js of this plugin you might need to add more dependencies as this plugin enqueues bunch of scripts, see whole list of them in /wp-content/plugins/visual-portfolio/classes/class-assets.php from line 350, at the end of register_scripts method of class defined in this file. So you really can put exactly all script's handles on which your custom js code relies and WP will figure out right order of linking those files to page for you.
Happy codding :)
to speed up my website, i tired to remove the "wp-includes/js/jquery/jquery-migrate.min.js" and "js/jquery/jquery.js" by below codeļ¼
function my_jquery_enqueue() {
wp_deregister_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'my_jquery_enqueue' );
after this, "wp-includes/js/jquery/jquery-migrate.min.js" and "js/jquery/jquery.js" all removed, but
the Contact form 7 is not working well.
is there a method to remove only "wp-includes/js/jquery/jquery-migrate.min.js" and "js/jquery/jquery.js" but keep other js files? Thank u
Unfortunately practically all wordpress plugins use jquery which is the basis of js in wordpress. If you remove jquery you will be disabling all plugins practically. So it is not advisable to do this. To keep plugins running without jquery you will have to re-program each plugin manually which will be a lot of work unnecessarily.
I am at my wit's end. Of the following two script tags, the first one does not show up in my source code; the second one does. I've tried a number of different ways of presenting it including relative links, the wordpress template URI call, etc, but when the link is VALID it disappears. The same thing happens to my stylesheet. What could possibly cause this? Could it be some sort of security thing?
<script src="https://mywebsite.com/wp-content/themes/aiir-theme/jquery.matchHeight.js" ></script>
<script src="https://mywebsite.com/wp-content/smurf/aiir-theme/jquery.matchHeight.js" ></script>
You shouldn't be "baking in" scripts and styles. Once you get the hang of properly enqueuing your assets, they'll be much easier to load/unload, maintain, manipulate, and otherwise handle - and your future self (and other developers) thank you in advance!
You'll need to make use of the wp_enqueue_scripts Action Hook which uses the wp_enqueue_style() and wp_enqueue_script() functions (which rely on your theme properly utilizing the wp_head() and wp_footer() functions.
add_action( 'wp_enqueue_scripts', 'so_53109688_enqueue_assets' );
function so_53109688_enqueue_assets(){
wp_enqueue_script( 'jquery' ); // Make sure jQuery is loaded
wp_enqueue_script( 'jquery-matchheight', get_stylesheet_directory_uri() . '/jquery.matchHeight.js', array( 'jquery' ), null, true );
wp_enqueue_style( 'jquery-matchheight-styles', get_stylesheet_directory_uri() . '/jquery.matchHeight.css' );
}
I can't attest to why they're disappearing, but if you properly enqueue your assets, they leave a much easier diagnostic trail for bug fixing.
SOLVED!! Someone at the company (not me) installed a plugin that "optimizes" js and css. This was causing it to delete the links from source code; now I can continue with problem solving.
Thanks for the inspiration,
Ric
I am trying to figure out why the version number query string is not being included in any of my JS or CSS files that I set. I have looked through all documentation and tried different methods. I have even tried just removing the version and Wordpress won't add it's own version. Was hoping to get any other ideas to try from someone so I can cache break Cloudflare easier.
wp_enqueue_style( 'theme-stylesheet', get_template_directory_uri() . '/assets/css/style.css', false, '1.0' );
wp_register_script( 'theme-scripts', get_template_directory_uri() . '/assets/js/blacklab.min.js', array( 'jquery' ), '1.0.2', true );
wp_localize_script( 'theme-scripts', 'localVar', $stream_info );
wp_enqueue_script( 'theme-scripts' );
Odds are that you have a plugin or theme function that is removing the query variable. It's impossible to guess where it might be without seeing your code, but if it was built properly, you should be able to search your plugins and theme folders for:
remove_query_arg: Which is the function used to remove the ver string.
script_loader_src: The hook which is often used to run the above function.
There are some plugins which removes the query strings.
In my case, it was W3 Total cache.
This option can be found in W3 Total cache's browser cache section