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?
- How to change title tag at page after loaded post?
- Following WordPress Markup Is mandatory, got awkward resultyby wordpress standard markup?
- How do I create plugin or theme using MVC pattern?
- How to limit number of number of categories displayed by categories widget
- PowerPress mobile media player
- When is the proper time to minify css and js with git workflow?
- Strange Situation When Try To Retrieve Github Gist Using wp_remote_get
- How to hide or rename “X” and “x-child” references in website source?
- How to Get Current Custom Post Type Associated Taxonomy Term
- Hide shortcodes “code” when using the “editor” the same as wordpress do with “gallery”
- Plugin templates vs Parent Theme
- Selectively update themes in WordPress multisite
- jQuery does not enqueue for my purpose… (before an inline script in the footer)
- Widget HTML Display Problem
- How to create a backend for a custom theme?
- Plugin – Make sure jquery is loaded in my settings page plus my JS file
- wp_nonce_field displaying twice
- jQuery in WordPress Plugin
- Warning: include(): https:// wrapper is disabled in the server configuration by allow_url_include=0
- Plugin is not generating title tags on any pages or posts
- Is it necessary to do validation again when retrieving data from database?
- How to Control CSS of Admin On Creating only a Specific Custom Post Type
- Can’t change the style of a submit input type? [closed]
- Make a plugin page out of influence of the theme’s style
- Update Data parameter of a wp_localize_script() call
- Custom entity search and display
- Template directory in plugin
- jquery & ajax sending data to php
- should i be checking for jquery before enqueing it in a plugin
- get_the_tags with separator control?
- How to only load css for used blocks on frontend
- 400 Bad Request, in wordpress theme development, wp_ajax
- jQuery instantly executes (a button click and css change) on load
- How to include jQuery into my plugin so I can use it on plugin page?
- Ajax submit result opens in admin-ajax.php
- Add child pages to submenu automatically
- how to get the post id in plugin
- Why in this archive page that call query_posts() function show only the last 10 posts?
- Jquery was not found on this server
- insert query on a custom table using ajax with jQuery plugin Jeditable
- my own SVN for a plugin/theme
- Issue on Setting $icon_url Parameter on WP add_menu_page()
- How to enqueue scripts in right way in a plugin?
- Why enqueue styles on hook?
- Getting a WordPress Debug Strategy
- unable to wp_enqueue_script(‘suggest’);
- 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
- PHP File_exist() not working – Checking if File Exist in WordPress Theme Directory
- Why would you use esc_attr() on internal functions?
- Invalid hook call on save, not edit when using swiper slider
- Proper way to use useSelect
- Conditional Generation of Image Sizes using add_image_size
- How to add plugin options in wp editor page
- Data not insert and update through ajax and jQuery in admin page?
- WP ajax requests not stacking?
- How to find where an object first instantiatiation
- Need help about understand api, wp, $ syntax in WordPress plugin script
- Gutenberg blocks error: Each child in a list should have a unique “key” prop
- Insert Into Post Not Working For Audio File Using jQuery
- Full documentation about $args for register_rest_route?
- WP Still Generating 150×150 Thumbnail Size Even After Un-Setting Small Size in Functions.php
- How to disable a jQuery plugin on WordPress plugin page
- jquery in wordpress plugin with depdendency
- ajax working when function is on child theme but not in plugin page
- modify show UI of a registered taxonomy
- Does WordPress default CSS have Grids?
- jQuery UI tab does not work in the plugin page
- Using HTML links within translatable string
- How to resize WordPress images on upload to specific height and width without cropping it
- How to overwrite iris color pallates from theme to plugin
- Create fixed static pages
- How to get terms for taxonomy
- Removed jQuery script from header.php , any problems?
- Ajax +wordpress onClick link redirect to new page and create html content
- How to replace settings in WordPress plugin from a theme
- Use options to control jQuery plugin
- Save temporary registration data
- Thickbox in Plugin settings page?
- How to localize data array in plugin’s option page
- How to remove/replace current page template?
- WordPress dynamic widget by location?
- sanitizing/escaping plugin options
- Load php file with jquery in single.php
- WordPress Page Reload Takes forever during theme development
- Loading Images from Javascript on the Front End
- Adjust query on single