aboutsummaryrefslogtreecommitdiff
path: root/build/wp-enqueue-for-beginners/index.html
diff options
context:
space:
mode:
authorbt <bt@btxx.org>2024-06-08 13:22:19 -0400
committerbt <bt@btxx.org>2024-06-08 13:22:19 -0400
commitdcfb172704f3afb68a30425029ec834be2883274 (patch)
tree02ac480745db802d7af03f3213a0c568322170e3 /build/wp-enqueue-for-beginners/index.html
parente146f8a64c793c337999ce316b16ebe5fe6f2dab (diff)
More content porting, on-going markdown changes for lowdown support
Diffstat (limited to 'build/wp-enqueue-for-beginners/index.html')
-rw-r--r--build/wp-enqueue-for-beginners/index.html78
1 files changed, 51 insertions, 27 deletions
diff --git a/build/wp-enqueue-for-beginners/index.html b/build/wp-enqueue-for-beginners/index.html
index 96ccb68..bc795a6 100644
--- a/build/wp-enqueue-for-beginners/index.html
+++ b/build/wp-enqueue-for-beginners/index.html
@@ -1,36 +1,43 @@
<!doctype html>
-<html lang="en" id="top">
+<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<title>WP Enqueue for Beginners</title>
- <link href="https://bt.ht/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
- <style>*{box-sizing:border-box;}body{font-family:sans-serif;margin:0 auto;max-width:650px;padding:1rem;}img{max-width:100%;}pre{overflow:auto;}table{text-align:left;width:100%;}</style>
+ <link href="/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
+ <link href="/rss.xml" type="application/rss+xml" rel="alternate" title="RSS feed for blog posts" />
+<style>*{box-sizing:border-box;}body{font-family:sans-serif;line-height:1.33;margin:0 auto;max-width:650px;padding:1rem;}img{max-width:100%;}pre{border:1px solid;overflow:auto;padding:5px;}table{text-align:left;width:100%;}.footnotes{font-size:90%;}</style>
</head>
<nav>
- <a href="#menu">Menu &darr;</a>
+ <a href="#menu">Menu &darr;</a>
</nav>
<main>
-<h1>WP Enqueue for Beginners</h1>
+<h1 id="wp-enqueue-for-beginners">WP Enqueue for Beginners</h1>
+
<p>2020-05-05</p>
-<p>Throughout my career designing, developing and auditing WordPress themes, I've come across many that include their custom styles / scripts as static HTML elements inside their respective <code>header</code> and <code>footer</code> templates. This is perfectly <em>fine</em>, but there is a cleaner way to include these files.</p>
-<p>This post is purposefully catered for WordPress beginners, so if this seems overly simple, then you're probably already developing WordPress themes that utilize these techniques. (Which is awesome!)</p>
-<h2>Introducing WP Enqueue</h2>
+
+<p>Throughout my career designing, developing and auditing WordPress themes, I&#8217;ve come across many that include their custom styles &#47; scripts as static HTML elements inside their respective <code>header</code> and <code>footer</code> templates. This is perfectly <em>fine</em>, but there is a cleaner way to include these files.</p>
+
+<p>This post is purposefully catered for WordPress beginners, so if this seems overly simple, then you&#8217;re probably already developing WordPress themes that utilize these techniques. (Which is awesome!)</p>
+
+<h2 id="introducing-wp-enqueue">Introducing WP Enqueue</h2>
+
<p>The description of Wp Enqueue from the WordPress documentation:</p>
-<p><blockquote class="wp-block-quote">
- <p>
- Registers the style [script] if source provided (does NOT overwrite) and enqueues
- </p>
-</blockquote></p>
+
<p>In a nutshell: Placing a <code>wp_enqueue_script</code> or <code>wp_enqueue_style</code> script in the <code>functions.php</code> of your custom theme tells WordPress to pull external files into the header or footer of your website. Best practice being: <em>styles into the header, scripts into the footer</em>.</p>
+
<p>I suggest you read the official documentation for more details: <a href="https://developer.wordpress.org/reference/functions/wp_enqueue_script/">wp_enqueue_script</a> and <a href="https://developer.wordpress.org/reference/functions/wp_enqueue_style/">wp_enqueue_style</a>.</p>
-<h2>Enqueue Stylesheets</h2>
+
+<h2 id="enqueue-stylesheets">Enqueue Stylesheets</h2>
+
<p>The default script to enqueue a CSS stylesheet:</p>
+
<pre><code>wp_enqueue_style( $handle, $src, $deps, $ver, $media );
</code></pre>
+
<ul>
<li><code>$handle</code> - the name associated with your stylesheet</li>
<li><code>$src</code> - URL pointing to the directory of the stylesheet itself</li>
@@ -38,51 +45,68 @@
<li><code>$ver</code> - The version number of the stylesheet (used for cache busting)</li>
<li><code>$media</code> - Specify media type (<code>all</code>, <code>print</code>, <code>screen</code>, etc.)</li>
</ul>
+
<p>So, with all those parameters in mind, here is what a standard default enqueue of a CSS stylesheet looks like:</p>
-<pre><code>wp_enqueue_style( 'google-fonts', 'https://fonts.googleapis.com/css?family=Montserrat:200,300,300i,400,600,700,800,900', '', '1.0', '');
+
+<pre><code>wp_enqueue_style( &#39;google-fonts&#39;, &#39;https:&#47;&#47;fonts.googleapis.com&#47;css?family=Montserrat:200,300,300i,400,600,700,800,900&#39;, &#39;&#39;, &#39;1.0&#39;, &#39;&#39;);
</code></pre>
+
<p>In this example we have rendered the following:</p>
+
<ul>
<li><code>$handle</code>: google-fonts</li>
-<li><code>$src</code>: https://fonts.googleapis.com/css?family=Montserrat:200,300,300i,400,600,700,800- 0</li>
+<li><code>$src</code>: <a href="https://fonts.googleapis.com/css?family=Montserrat:200,300,300i,400,600,700,800-">https:&#47;&#47;fonts.googleapis.com&#47;css?family=Montserrat:200,300,300i,400,600,700,800-</a> 0</li>
<li><code>$deps</code>: Null (left blank)</li>
<li><code>$ver</code>: 1.0</li>
<li><code>$media</code>: Null (left blank)</li>
</ul>
+
<p><strong>Important:</strong> Keep in mind that the <code>wp_enqueue_style</code> script will render the stylesheet link into the WordPress header automatically.</p>
-<h2>Enqueue Scripts</h2>
+
+<h2 id="enqueue-scripts">Enqueue Scripts</h2>
+
<p>The default script to enqueue an external JS file:</p>
+
<pre><code>wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
</code></pre>
+
<ul>
<li><code>$handle</code> - the name associated with your script</li>
<li><code>$src</code> - URL pointing to the directory of the script itself</li>
<li><code>$deps</code> - An array of any other scripts needed as dependencies</li>
<li><code>$ver</code> - The version number of the script (used for cache busting)</li>
-<li><code>$in_footer</code> - Set whether the script is loaded in the <code>&lt;head&gt;</code> or just before the <code>&lt;/body&gt;</code></li>
+<li><code>$in_footer</code> - Set whether the script is loaded in the <code>&#60;head&#62;</code> or just before the <code>&#60;&#47;body&#62;</code></li>
</ul>
+
<p>With all those parameters in mind, here is what a standard default enqueue of a Javascript file looks like:</p>
-<pre><code>wp_enqueue_script( 'bxslider', get_template_directory_uri() . '/js/bxslider.js', array('jquery'), '1.0.0', true );
+
+<pre><code>wp_enqueue_script( &#39;bxslider&#39;, get_template_directory_uri() . &#39;&#47;js&#47;bxslider.js&#39;, array(&#39;jquery&#39;), &#39;1.0.0&#39;, true );
</code></pre>
+
<p>In this example we have rendered the following:</p>
+
<ul>
<li><code>$handle</code>: bxslider</li>
-<li><code>$src</code>: get_template_directory<em></em>ri() . /js/bxslider.js'</li>
-<li><code>$deps</code>: array(jquery')</li>
+<li><code>$src</code>: get_template_directory_uri() . &#47;js&#47;bxslider.js&#8217;</li>
+<li><code>$deps</code>: array(jquery&#8217;)</li>
<li><code>$ver</code>: 1.0.0</li>
<li><code>$in_footer</code>: True (<em>places script before closing body tag</em>)</li>
</ul>
-<h2>Packaging Everything Together</h2>
+
+<h2 id="packaging-everything-together">Packaging Everything Together</h2>
+
<p>Now that we have the custom stylesheet and script ready to be loaded into our custom WordPress theme, we just need to properly package them together as a function in our <code>functions.php</code> file:</p>
-<pre><code>// Add styles and scripts to the header/footer
+
+<pre><code>&#47;&#47; Add styles and scripts to the header&#47;footer
function custom_enqueue_scripts() {
- wp_enqueue_style( 'google-fonts', 'https://fonts.googleapis.com/css?family=Montserrat:200,300,300i,400,600,700,800,900');
- wp_enqueue_script( 'bxslider', get_template_directory_uri() . '/js/bxslider.js', array('jquery'), '1.0.0', true );
+ wp_enqueue_style( &#39;google-fonts&#39;, &#39;https:&#47;&#47;fonts.googleapis.com&#47;css?family=Montserrat:200,300,300i,400,600,700,800,900&#39;);
+ wp_enqueue_script( &#39;bxslider&#39;, get_template_directory_uri() . &#39;&#47;js&#47;bxslider.js&#39;, array(&#39;jquery&#39;), &#39;1.0.0&#39;, true );
}
-add_action( 'wp_enqueue_scripts', 'custom_enqueue_scripts');
+add_action( &#39;wp_enqueue_scripts&#39;, &#39;custom_enqueue_scripts&#39;);
</code></pre>
-<p>That's it! Hopefully this helps prevent WordPress newbies from statically rendering their external CSS and JS files directly in template files. Let WordPress do that for you!</p>
+
+<p>That&#8217;s it! Hopefully this helps prevent WordPress newbies from statically rendering their external CSS and JS files directly in template files. Let WordPress do that for you!</p>
<footer role="contentinfo">
<h2>Menu Navigation</h2>
<ul id="menu">