{"id":75507,"date":"2023-01-13T13:29:35","date_gmt":"2023-01-13T13:29:35","guid":{"rendered":"\/tutorials\/?p=75507"},"modified":"2025-04-27T10:38:34","modified_gmt":"2025-04-27T10:38:34","slug":"jquery-in-wordpress","status":"publish","type":"post","link":"\/in\/tutorials\/jquery-in-wordpress","title":{"rendered":"How to Use jQuery in WordPress: 2 Methods (Manually and Using a Plugin)"},"content":{"rendered":"<p>WordPress developers use JavaScript and libraries to create interactive elements on a web page and simplify the coding process. Among the most popular options is jQuery.<\/p><p>What makes using jQuery in WordPress special is the extensive plugin ecosystem. Instead of writing a jQuery script from scratch, developers can save time by employing reusable code snippets to customize WordPress plugins and themes.<\/p><p>If you want to create engaging websites using jQuery, this article will guide you through the entire process. We&rsquo;ll also discuss what jQuery is, its key advantages, and how to add jQuery to WordPress.<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Mega-WordPress-Cheat-EN.pdf\" target=\"_blank\" rel=\"noopener\">Download all in one WordPress cheat sheet<\/a><\/p><p>\n\n\n\n\n\n<div class=\"protip\">\n                    <h2 class=\"featured-snippet title\">What Is jQuery?<\/h2>\n                    <p> jQuery is a JavaScript library used for creating dynamic front-end components, such as rotating sliders. Its lightweight code enables web developers to manipulate HTML elements and validate data more efficiently.<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-why-use-jquery\">Why Use jQuery<\/h2><p>Apart from its plugin ecosystem, other reasons to use a jQuery library in WordPress are:<\/p><ul class=\"wp-block-list\">\n<li><strong>Beginner-friendly<\/strong>. Since it uses a simple and short text format, jQuery code is easy to read and write.<\/li>\n\n\n\n<li><strong>Compactness<\/strong>. A jQuery file is usually smaller than plain JavaScript, letting you perform the same tasks with fewer characters.<\/li>\n\n\n\n<li><strong>Cross-browser compatibility<\/strong>. Popular browsers that support jQuery include <strong>Google Chrome<\/strong>, <strong>Microsoft Edge<\/strong>, <strong>Mozilla Firefox<\/strong>, <strong>Safari<\/strong>, and <strong>Opera<\/strong>.<\/li>\n\n\n\n<li><strong>AJAX support<\/strong>. Developers can implement <a href=\"\/in\/tutorials\/what-is-ajax\">AJAX<\/a> to build a responsive and user-friendly website.<\/li>\n\n\n\n<li><strong>Lower bandwidth usage<\/strong>. jQuery can execute animation effects directly on the user&rsquo;s browser, resulting in lower bandwidth usage.<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-how-to-use-jquery-in-wordpress\">How to Use jQuery in WordPress<\/h2><p>There are two ways of adding jQuery scripts to a WordPress site &ndash; manually or using a WordPress plugin.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/in\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner-1024x300.png\" alt=\"\" class=\"wp-image-111781\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-1024x300.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-300x88.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-150x44.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-768x225.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-1536x450.png 1536w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>Let&rsquo;s start with the manual method.<\/p><h3 class=\"wp-block-heading\" id=\"h-adding-jquery-manually\">Adding jQuery Manually<\/h3><p>If you have experience with JavaScript and WordPress development, follow these steps:<\/p><p><strong>1. Switch to the Compatibility Mode<\/strong><\/p><p>Before you start adding your own custom jQuery scripts to WordPress, it&rsquo;s crucial to understand jQuery&rsquo;s compatibility mode.<\/p><p>By default, jQuery uses the <strong>$ <\/strong>sign as a shortcut:<\/p><pre class=\"wp-block-preformatted\">$(document) .ready (function() {\n$(\"button\") .click (function () {<\/pre><p>However, other JavaScript libraries on your site also implement the dollar sign in their syntax. To avoid conflicts, enter the compatibility mode by replacing the <strong>$ <\/strong>sign with <strong>jQuery<\/strong>.<\/p><p>Take a look at the following code:<\/p><pre class=\"wp-block-preformatted\">jQuery(document) .ready (function() {\njQuery(\"button\") .click (function () {<\/pre><p>The only problem with this mode is, writing jQuery<strong> <\/strong>instead of the shortcut means incorporating more characters, leading to bloated scripts.<\/p><p>To solve this issue, pick a new variable name to replace the dollar sign. The most common one is <strong>$j.<\/strong><\/p><p>Simply add this code at the top of your jQuery scripts:<\/p><pre class=\"wp-block-preformatted\">var $j = jQuery.noConflict()<\/pre><p><strong>2. Make a Script File<\/strong><\/p><p>After that, create a custom script file with the .js extension. Hostinger users can follow these steps:<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> For beginners, we recommend <a href=\"\/in\/tutorials\/how-to-create-wordpress-child-theme\">creating a WordPress child theme<\/a> first. This way, you can customize the styling, layout parameters, and scripts without changing the parent theme directory.<\/p><\/div>\n\n\n\n<\/p><ol class=\"wp-block-list\">\n<li>Access <strong>hPanel <\/strong>&rarr; <strong>Hosting <\/strong>&rarr; <strong>Manage<\/strong>.<\/li>\n\n\n\n<li>On the left menu, select <strong>Files <\/strong>&rarr;<strong> File Manager<\/strong>.<\/li>\n\n\n\n<li>Enter <strong>public_html<\/strong> &rarr;<strong> wp-content<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/in\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/01\/The-wp-content-folder-in-hPanel_s-File-Manager.png\"><img decoding=\"async\" width=\"1024\" height=\"485\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/01\/The-wp-content-folder-in-hPanel_s-File-Manager-1024x485.png\" alt=\"The wp-content folder in hPanel's File Manager\" class=\"wp-image-75510\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/The-wp-content-folder-in-hPanel_s-File-Manager.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/The-wp-content-folder-in-hPanel_s-File-Manager-300x142.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/The-wp-content-folder-in-hPanel_s-File-Manager-150x71.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/The-wp-content-folder-in-hPanel_s-File-Manager-768x364.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><ol class=\"wp-block-list\" start=\"4\">\n<li>Open the <strong>themes<\/strong> folder<strong> <\/strong>and select one of the installed themes.<\/li>\n\n\n\n<li>On the left menu, click <strong>New Folder<\/strong> and name the folder <strong>js<\/strong>.<\/li>\n\n\n\n<li>Click <strong>New File<\/strong> to create a new .js file &ndash; for instance: <strong>new_script.js<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"\/in\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/01\/Newly-created-.js-file-in-the-theme_s-folder.png\"><img decoding=\"async\" width=\"1024\" height=\"214\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/01\/Newly-created-.js-file-in-the-theme_s-folder-1024x214.png\" alt=\"Newly created .js file in the theme's folder\" class=\"wp-image-75511\" style=\"width:840px;height:175px\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/Newly-created-.js-file-in-the-theme_s-folder.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/Newly-created-.js-file-in-the-theme_s-folder-300x63.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/Newly-created-.js-file-in-the-theme_s-folder-150x31.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/Newly-created-.js-file-in-the-theme_s-folder-768x161.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><ol class=\"wp-block-list\" start=\"7\">\n<li>Add jQuery scripts to the file.<\/li>\n\n\n\n<li>Repeat the same steps to customize WordPress plugins.<\/li>\n<\/ol><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>If you&rsquo;re unsure how to code a script, our guide <a href=\"\/in\/tutorials\/what-is-jquery\/\">What Is jQuery? A Beginner&rsquo;s Introduction to the jQuery Library<\/a> explains its main features.<\/p>\n                <\/div>\n\n\n\n<\/p><p><strong>3. Add Code Into the functions.php File<\/strong><\/p><p>Before adding inline scripts to WordPress, locate the<strong> functions.php<\/strong> file in the theme&rsquo;s folder.<\/p><figure class=\"wp-block-image aligncenter size-large\"><a href=\"\/in\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/01\/The-functions.php-file-in-the-theme_s-folder.png\"><img decoding=\"async\" width=\"1024\" height=\"341\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/01\/The-functions.php-file-in-the-theme_s-folder-1024x341.png\" alt=\"The functions.php file in the theme's folder\n\" class=\"wp-image-75512\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/The-functions.php-file-in-the-theme_s-folder.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/The-functions.php-file-in-the-theme_s-folder-300x100.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/The-functions.php-file-in-the-theme_s-folder-150x50.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/01\/The-functions.php-file-in-the-theme_s-folder-768x256.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>Open the file and add the following jQuery function at the top:<\/p><pre class=\"wp-block-preformatted\">function add_my_scripts() {\r\n    wp_enqueue_script( &lsquo;new-script', get_template_directory_uri() . '\/js\/new_script.js', array( 'jquery' ), '1.0.0', true );\r\n}\r\nadd_action( 'wp_enqueue_scripts', 'add_my_scripts' )<\/pre><p>Make sure to replace <strong>new-script<\/strong> and <strong>new_script.js<\/strong> with the actual file names.<\/p><p>By inserting the <strong>wp_enqueue_script<\/strong> function into the PHP code, users can add a custom script and inform WordPress that it relies on jQuery.<\/p><p>It also helps WordPress locate the script file and customize the theme based on your jQuery scripts.<\/p><h3 class=\"wp-block-heading\" id=\"h-adding-jquery-via-a-wordpress-plugin\">Adding jQuery via a WordPress Plugin<\/h3><p>If adding scripts to WordPress manually is too technical for you, we recommend using jQuery WordPress plugins.<\/p><p>Here&rsquo;s how:<\/p><p><strong>1. Pick a Plugin<\/strong><\/p><p>WordPress offers various plugins to create interactive websites with jQuery. Here are our top picks for different use cases:<\/p><ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/wordpress.org\/plugins\/jquery-updater\/\" target=\"_blank\" rel=\"noreferrer noopener\">jQuery Updater<\/a>. While jQuery comes pre-loaded with WordPress, the platform often uses an outdated version. jQuery Updater ensures that your site automatically installs the latest version of this JavaScript library.<\/li>\n\n\n\n<li><a href=\"https:\/\/wordpress.org\/plugins\/advanced-custom-fields\/\" target=\"_blank\" rel=\"noreferrer noopener\">Advanced Custom Fields<\/a>. This tool is suitable for adding custom fields to posts and pages. Some of its <a href=\"https:\/\/www.advancedcustomfields.com\/resources\/\" target=\"_blank\" rel=\"noreferrer noopener\">jQuery-based fields<\/a> include Color Picker, Google Map, Date Picker, and Time Picker.<\/li>\n\n\n\n<li><a href=\"https:\/\/wordpress.org\/plugins\/jquery-validation-for-contact-form-7\/\" target=\"_blank\" rel=\"noreferrer noopener\">jQuery Validation For Contact Form 7<\/a>. This plugin lets you display error messages when users enter invalid data on <a href=\"https:\/\/wordpress.org\/plugins\/contact-form-7\/\" target=\"_blank\" rel=\"noreferrer noopener\">Contact Form 7<\/a>. You can create validation rules for URLs, dates, credit cards, and phone numbers.<\/li>\n\n\n\n<li><a href=\"https:\/\/wordpress.org\/plugins\/jquery-post-splitter\/\" target=\"_blank\" rel=\"noreferrer noopener\">jQuery Post Splitter<\/a>. Compatible with most WordPress themes, this plugin lets you split posts and pages into sliders.<\/li>\n<\/ul><p>Although <a href=\"\/in\/tutorials\/wordpress\/how-to-install-wordpress-plugins\">installing these plugins<\/a> simplifies the website designing process, you still need a basic knowledge of JavaScript and jQuery libraries to use them.<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Suggested Reading<\/h4>\n                    <p><a href=\"\/in\/tutorials\/wordpress-javascript\">How to Add JavaScript to WordPress<\/a><br>\n<a href=\"\/in\/tutorials\/wordpress-theme-editor\/\">WordPress Theme Editor<\/a><br>\n<a href=\"\/in\/tutorials\/wordpress-file-manager\">WordPress File Manager<\/a><br>\n<a href=\"\/in\/tutorials\/wordpress-affiliate-plugin\">Best WordPress Affiliate Plugins<\/a><br>\n<a href=\"\/in\/tutorials\/wordpress-jquery-is-not-defined\">WordPress jQuery Is Not Defined Error<\/a><br>\n<a href=\"\/in\/tutorials\/wordpress-security-plugins\">The 7 Best WordPress Security Plugins<\/a><br>\n<a href=\"\/in\/tutorials\/wordpress-project-management\">15 Best WordPress Project Management Plugins<\/a><\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Knowing how to use jQuery in WordPress is crucial for any site owners and developers.<\/p><p>jQuery offers a lightweight solution to create interactive elements, edit pages, and customize the overall look of a WordPress website.<\/p><p>To add jQuery to WordPress scripts manually, switch to its compatibility mode, create a script file, and customize the <strong>functions.php<\/strong> file in your theme&rsquo;s folder.<\/p><p>For beginners, WordPress jQuery plugins like <strong>Advanced Custom Fields<\/strong> will help with the process.<\/p><p>We hope this article will help you develop a more unique and engaging website. Good luck.<\/p><h2 class=\"wp-block-heading\" id=\"h-jquery-in-wordpress-faqs\">jQuery in WordPress FAQs<\/h2><p>Below is some additional information about jQuery in WordPress.<\/p><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1680762668872\"><h3 class=\"schema-faq-question\">Is jQuery Easy to Learn?<\/h3> <p class=\"schema-faq-answer\">jQuery is generally considered easy to learn for beginners due to its concise syntax and intuitive API. Its popularity has led to a wealth of documentation and community support, making it a popular choice for front-end development.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1680762688202\"><h3 class=\"schema-faq-question\">What Are Some of the Drawbacks of Using jQuery?<\/h3> <p class=\"schema-faq-answer\">jQuery drawbacks include increased page load times, overreliance leading to code bloat, accessibility issues, browser feature alternatives, and modern web development trends favoring alternative solutions like native JavaScript and newer frameworks. Careful consideration of jQuery&rsquo;s use is important.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1680762704157\"><h3 class=\"schema-faq-question\">How Do I Upgrade to the Latest jQuery Version?<\/h3> <p class=\"schema-faq-answer\">Upgrading to a newer version of jQuery makes a website more secure and faster. Upgrading to a newer version of jQuery includes identifying where jQuery is being used, adding the new version of jQuery in a testing mode, and testing areas of the site that use jQuery for issues.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>WordPress developers use JavaScript and libraries to create interactive elements on a web page and simplify the coding process. Among the most popular options is jQuery. What makes using jQuery in WordPress special is the extensive plugin ecosystem. Instead of writing a jQuery script from scratch, developers can save time by employing reusable code snippets [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/in\/tutorials\/jquery-in-wordpress\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":332,"featured_media":81823,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","footnotes":""},"categories":[22639,22633],"tags":[],"class_list":["post-75507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-advanced","category-wordpress"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/jquery-in-wordpress","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/jquery-wordpress","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/jquery-in-wordpress","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-use-jquery-in-wordpress-2-methods-manually-and-using-a-plugin","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-use-jquery-in-wordpress-2-methods-manually-and-using-a-plugin","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/jquery-wordpress","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/jquery-wordpress","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/jquery-wordpress","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/jquery-in-wordpress","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/jquery-in-wordpress","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/jquery-in-wordpress","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/jquery-in-wordpress","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/75507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/users\/332"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/comments?post=75507"}],"version-history":[{"count":24,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/75507\/revisions"}],"predecessor-version":[{"id":127471,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/75507\/revisions\/127471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media\/81823"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media?parent=75507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/categories?post=75507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/tags?post=75507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}