The first is a pattern called a closure. It’s not unique to jQuery. You could just as easily write
(function(someVar) {
// Inside the closure, someVar == "test"
})("test");
Basically, you’re manually passing jQuery into the closure by referencing the jQuery
object externally and aliasing it to $
inside the context of the closure.
The second pattern is unique to jQuery. It’s one of the library’s shortcuts to the DOM ready event. The following calls are all equivalent:
jQuery(document).ready(function($) {
// Use $ inside here like normal
});
jQuery.ready(function($) {
// Use $ inside here like normal
});
jQuery(function($) {
// Use $ inside here like normal
});
In all three examples, you’re also passing the jQuery
object into your function as the variable $
so that it’s available within the local scope.
Related Posts:
- ajaxurl not defined on front end
- Why does WordPress use outdated jQuery v1.12.4?
- How to include jQuery and JavaScript files correctly?
- jQuery in header or footer
- How to Add a .js file Only in one specific Page Dynamically to Head
- WordPress custom taxonomy check box to dropdown
- WP Plugin Running before jQuery
- Ajax: Populate with content from a post’s ID not working – duplicating current page html instead
- Using function from enqueued .js file in theme in plugin?
- Loading jQuery library from WordPress admin
- How to get the value entered in the input field in wordpres
- How react js and other Javascript Technologies works on WordPress plugin?
- Where to store PHP files created by plugin / themes
- How to post data to same page in wordpress
- Understanding WordPress functions’ naming conventions
- Test to see if jQuery or Prototype is queued by another plugin?
- Is there widely accepted phpDoc syntax for documenting which hook calls a function?
- How do I Make a Theme “plugin-ready”?
- Show a user their recently viewed posts
- How to trap “Publish” button to check for meta box validation?
- How to use filter hook ‘post_updated_messages’ in coherence with action hook ‘save_post’
- Add new user and add meta at once
- Custom user profile, registration, login page with theme
- Use js script from one plugin in another plugin
- How to Display Custom Post Type’s Gallery (images ) in Through WP_Query
- What is better way to use Bootstrap inside admin panel?
- Error : “Updating failed: The response is not a valid JSON response” with custom shortcode
- What’s the better way to add an inline script?
- Metabox Not Showing on Custom Post Type But On Pages and Post
- Backslashes being stripped from CSS
- Ajax in WordPress – path issue
- How to determine if the current file is loaded in a plugin or in a theme?
- Filter, or any way to dynamically change theme screenshot image?
- Finding the paragraphs in content
- How to avoid loading same script twice?
- Can I individually style items in the backend widget list?
- WP_Query returns no results
- Finding posts containing matching array elements in a meta field usign WP_Query
- Should action callbacks start with a verb?
- How to Create Custom HTML Tag on Editor in `Text(HTML)` mode
- Is It Always a Best Practice to Decouple the Frontend from the Admin Area When Developing a WordPress Application?
- Beginner advice
- How to render a time-of-day string like ’16:42′ with a site’s chosen time format?
- Are block templates incompatible with serialize_blocks?
- How to filter users list on user_status field with get_users()
- Can Page Templates be Applied to Archive and Post Templates?
- Is there some way to provide the user a list of existing content in a CPT
- Change the look and feel of admin pages
- how to search users by ajax live search
- Form doesnt save to database
- Notice: Trying to get property ‘term_id’ of non-object
- What is the proper method of using global $post?
- Display WordPress the_content() via data attributes
- javascript datatables in a plugin
- How to change title tag at page after loaded post?
- Selectively update themes in WordPress multisite
- Plugin – Make sure jquery is loaded in my settings page plus my JS file
- Template directory in plugin
- should i be checking for jquery before enqueing it in a plugin
- jQuery instantly executes (a button click and css change) on load
- Getting a WordPress Debug Strategy
- Drawing the line between theme & plugin on large scale bespoke projects
- making a plugin that moves other plugins wp_head actions to wp_footer
- Apply styles to blockquote element with the WYSIWYG editor
- Conditional Generation of Image Sizes using add_image_size
- WP Still Generating 150×150 Thumbnail Size Even After Un-Setting Small Size in Functions.php
- Does WordPress default CSS have Grids?
- How to resize WordPress images on upload to specific height and width without cropping it
- How to localize data array in plugin’s option page
- rewrite_rules problem
- Why does website stretch and white space on load? [duplicate]
- Ajax on the Administration Side of plugin – returns 0
- Pass custom props to
- Include external po file for 3th party plugin to theme
- How to make premium plugin? I want to limit it until verification
- setTimeout not working in jquery
- How to hide/remvoe unnecessary field/section in post edit section ( Dashboard )
- Scripts/styles not loading on cloned WP Site when logged in
- How to get values from Tinymce visual editor popup?
- redirect_to how to make it simply work with get parameter or similar?
- Determine if the current page, is being edited
- Is it possible to modify an Elated plugin portfolio-list template in such a way that it will not conflict with future plugin updates?
- Ajax call not working with
- Woocommerce Custom Checkout
- Dynamic sidebar areas not working on the Theme Customizer
- Looping single post in a theme
- Merge Codes using redux framework
- WordPress permalink setting
- Custom theme and plugin updating
- Script work on non logged in user but not work for logged in user?
- Adding class to the parent of current-post-ancestor / current-menu-parent / current-post-parent
- problem with blank page
- grouping my widgets wordpress
- Cannot echo a JS variable to a jQuery plugin coming from wp_localize_script
- Create and style menu
- If I want to create new taxonomies (e.g. Project / Documents / Etc…) is it better to create them in the theme’s functions.php or within a plugin? [duplicate]
- How to add something after a function
- How do I use (or mimic) document.getElementById() on a page loaded from WordPress database?
- how many rupee or dollar charge to client to make theme [closed]
- Remove Gutenberg Buttons Block