{"id":52,"date":"2020-04-30T11:13:04","date_gmt":"2020-04-30T11:13:04","guid":{"rendered":"https:\/\/codexcoach.com\/?p=52"},"modified":"2022-12-28T09:33:15","modified_gmt":"2022-12-28T09:33:15","slug":"how-to-include-javascript-css-in-wordpress","status":"publish","type":"post","link":"https:\/\/codexcoach.com\/how-to-include-javascript-css-in-wordpress\/","title":{"rendered":"How to Include JavaScript &#038; CSS in WordPress"},"content":{"rendered":"\n<p>When we are customizing any wordpress theme using child theme at some time we need to enqueue any external CSS or JavaScript in site. In this case we can best way to include css and JavaScript in <strong>functions.php<\/strong> file. We can enqueue the JavaScript or CSS using <strong>wp_enqueue_script()<\/strong> or <strong>wp_enqueue_style()<\/strong> functions.<\/p>\n\n\n\n<h2>CSS<\/h2>\n\n\n\n<p>You can enqueue your css in <strong>functions.php<\/strong> file using <code class=\"EnlighterJSRAW\" data-enlighter-theme=\"eclipse\">wp_enqueue_style()<\/code> function.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\n wp_enqueue_style( $handle, $src, $deps, $ver, $media );\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<p>You can include these parameters:<\/p>\n\n\n\n<ul>\n<li><strong>$handle<\/strong>&nbsp;is simply the name of the stylesheet.<\/li>\n\n\n\n<li><strong>$src<\/strong>&nbsp;is where it is located. The rest of the parameters are optional.<\/li>\n\n\n\n<li><strong>$deps<\/strong>&nbsp;refers to whether or not this stylesheet is dependent on another stylesheet. If this is set, this stylesheet will not be loaded unless its dependent stylesheet is loaded first.<\/li>\n\n\n\n<li><strong>$ver<\/strong>&nbsp;sets the version number.<\/li>\n\n\n\n<li><strong>$media<\/strong>&nbsp;can specify which type of media to load this stylesheet in, such as \u2018all\u2019, \u2018screen\u2019, \u2018print\u2019 or \u2018handheld.\u2019<\/li>\n<\/ul>\n\n\n\n<p>If you want to include <strong>custom-style.css<\/strong> file in folder <strong>css<\/strong> in your child theme directory. You can write below in <strong>functions.php<\/strong> file.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\n  wp_enqueue_style( &#39;custom-style&#39;, get_stylesheet_directory_uri() . &#39;\/css\/custom-style.css&#39;,false,&#39;1.5&#39;,&#39;all&#39;);\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<h2>JavaScript<\/h2>\n\n\n\n<p>You must load Javascript file using <code>wp_enqueue_script()<\/code> function. Syntax of <code>wp_enqueue_script()<\/code> is same like to using <code>wp_enqueue_script()<\/code>.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\n  wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer);\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<ul>\n<li><strong>$handle<\/strong>&nbsp;is the name for the script.<\/li>\n\n\n\n<li><strong>$src<\/strong>&nbsp;defines where the script is located.<\/li>\n\n\n\n<li><strong>$deps<\/strong>&nbsp;is an array that can handle any script that your new script depends on, such as jQuery.<\/li>\n\n\n\n<li><strong>$ver<\/strong>&nbsp;lets you list a version number.<\/li>\n\n\n\n<li><strong>$in_footer<\/strong>&nbsp;is a boolean parameter (true\/false) that allows you to place your scripts in the footer of your HTML document rather then in the header, so that it does not delay the loading of the DOM tree.<\/li>\n<\/ul>\n\n\n\n<p>You should enqueue <strong>custom.js<\/strong> like this in your child theme:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\n  wp_enqueue_script( &#39;custom-script&#39;, get_stylesheet_directory_uri() . &#39;\/js\/custom.js&#39;, array ( &#39;jquery&#39; ), 1.1, true);\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<h2>Combining Enqueue Example<\/h2>\n\n\n\n<p>The best way to combine all css and javascript in a single function using <strong>wp_enqueue_scripts<\/strong> WordPress action. It will look like below example. Put below code in <strong>functions.php<\/strong> file and move .css and .js file into &#8220;js&#8221; and &#8220;css&#8221; folder of child theme.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\nadd_action( &#39;wp_enqueue_scripts&#39;, &#39;cxc_enqueue_my_styles_scripts&#39; );\n\nfunction cxc_enqueue_my_styles_scripts() {\n\n\t\/* Enqueue google font & bootstrap css *\/\n\twp_enqueue_style( &#39;font-maven-css&#39;, &#39;https:\/\/fonts.googleapis.com\/css?family=Maven+Pro:400,500,700&#39; );\n\twp_enqueue_style( &#39;bootstrap-css&#39;, get_stylesheet_directory_uri() . &#39;\/css\/bootstrap.css&#39; );\n\n\t\/* Enqueue bootstrap & custom script *\/\n\twp_enqueue_script( &#39;bootstrap-script&#39;, get_stylesheet_directory_uri() . &#39;\/js\/bootstrap.min.js&#39;, array(&#39;jquery&#39;), &#39;1.0.0&#39;, true );\n\twp_enqueue_script( &#39;custom-script&#39;, get_stylesheet_directory_uri() . &#39;\/js\/custom.js&#39;, array(&#39;jquery&#39;), &#39;1.0.0&#39;, true );\n\n\t\/* Localize a registered custom-script with Ajax URL for a JavaScript variable. *\/\n\twp_localize_script( &#39;custom-script&#39;, &#39;myAjax&#39;, array( &#39;ajaxurl&#39; =&gt; admin_url(&#39;admin-ajax.php&#39;) ) );\n}\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<h2>Load CSS File on WordPress Admin<\/h2>\n\n\n\n<p>Sometime you want to load CSS and JavaScript in all admin pages for any customization. You can do that using <strong>admin_enqueue_scripts<\/strong> action. You can enqueue script and style using below code.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\nadd_action( &#39;admin_enqueue_scripts&#39;, &#39;cxc_enqueue_my_styles_scripts_in_admin&#39; );\n\n  function cxc_enqueue_my_styles_scripts_in_admin() {\n    wp_enqueue_style( &#39;custom-admin-css&#39;, get_stylesheet_directory_uri() . &#39;\/css\/custom-admin-style.css&#39; );\n    wp_enqueue_script( &#39;custom-admin-script&#39;, get_stylesheet_directory_uri() . &#39;\/js\/custom-admin.js&#39;, array(&#39;jquery&#39;), &#39;1.0.0&#39;, true );\n}\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<h2>Load CSS File from Plugin on WordPress Admin<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\nadd_action( &#39;admin_enqueue_scripts&#39;, &#39;cxc_enqueue_my_styles_scripts_in_admin_from_plugin&#39; );\n\nfunction cxc_enqueue_my_styles_scripts_in_admin_from_plugin() {\n\twp_enqueue_style( &#39;custom-admin-css&#39;, plugins_url(&#39;custom-admin-style.css&#39;, __FILE__) );\n}\n?&gt;<\/code><\/pre><\/div>\n<div id=\"ezoic-pub-ad-placeholder-118\"><\/div>","protected":false},"excerpt":{"rendered":"<p>When we are customizing any wordpress theme using child theme at some time we need to enqueue any external CSS or JavaScript in site. In this case we can best way to include css and JavaScript in functions.php file. We can enqueue the JavaScript or CSS using wp_enqueue_script() or wp_enqueue_style() functions. CSS You can enqueue [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":1103,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_aib_schema_json_ld":""},"categories":[10,6,7,12],"tags":[],"_links":{"self":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":6,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":1973,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/52\/revisions\/1973"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media\/1103"}],"wp:attachment":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}