<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>enqueue Archive - Robert Skibbe</title>
	<atom:link href="https://robbelroot.de/blog/tag/enqueue/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>alias RobbelRoot – Freelance Full Stack Developer .NET</description>
	<lastBuildDate>Thu, 18 Jan 2024 00:56:08 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://robbelroot.de/wp-content/uploads/2020/12/cropped-favicon-32x32.png</url>
	<title>enqueue Archive - Robert Skibbe</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>WordPress – Loading scripts on specific pages or posts in 2024</title>
		<link>https://robbelroot.de/blog/wordpress-loading-scripts-only-on-specific-pages-or-posts/</link>
					<comments>https://robbelroot.de/blog/wordpress-loading-scripts-only-on-specific-pages-or-posts/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Fri, 11 Feb 2022 08:24:07 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress solutions]]></category>
		<category><![CDATA[WordPress Tutorials (EN)]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[enqueue]]></category>
		<category><![CDATA[enqueue_script]]></category>
		<category><![CDATA[is_page]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[limit]]></category>
		<category><![CDATA[only]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[pages]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[specific]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=8847</guid>

					<description><![CDATA[<p>So you want your WordPress to load a script on specific pages, thus being more efficient and clean – great! Maybe you already know about enqueuing files like JavaScript or CSS already, but you are searching for a way to optimize. As I just stumbled upon this problem myself today &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/wordpress-loading-scripts-only-on-specific-pages-or-posts/">WordPress – Loading scripts on specific pages or posts in 2024</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/WordPress-load-script-on-specific-page-only.png"><img fetchpriority="high" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/WordPress-load-script-on-specific-page-only.png" alt="WordPress load script on specific page only" class="wp-image-8850" title="WordPress load script on specific page only"/></a><figcaption class="wp-element-caption">WordPress load script on specific page only</figcaption></figure>






<p>So you want your WordPress to <strong>load a script on specific pages</strong>, thus <strong>being </strong>more <strong>efficient </strong>and clean – great!</p>



<p><strong>Maybe you </strong>already <strong>know about enqueuing files</strong> like <strong>JavaScript </strong>or <strong>CSS </strong>already, but you are searching for a way to optimize.</p>



<p>As I just <strong>stumbled upon this </strong>problem myself today (and in the past), I thought I <strong>need to share </strong>this.</p>



<p><strong>While </strong>I&#8217;ve done <strong>my </strong>usual <strong>research</strong>, I just <strong>noticed</strong> <strong>that </strong>there <strong>aren&#8217;t</strong> that <strong>many</strong>, nor good <strong>examples </strong>online.</p>



<p>I <strong>was </strong>totally <strong>surprised</strong>, <strong>that </strong>this commonly faced task <strong>isn&#8217;t that widespread</strong> across the WordPress developer community.</p>



<p><strong>Even though </strong>I <strong>found </strong>some lonely <strong>examples</strong>, I had to find out, that <strong>those didn&#8217;t work</strong> at least for my WordPress situation.</p>



<p>So i just <strong>continued looking </strong>for more and <strong>was kinda disappointed</strong>..</p>



<p><strong>Some code </strong>was just <strong>clearly outdated</strong> and there <strong>also </strong>was some <strong>code</strong>, which <strong>didn&#8217;t feel really clean</strong>.</p>



<p>It <strong>doesn&#8217;t feel good stacking</strong> <strong>code into </strong>the &#8222;<strong>functions.php</strong>&#8222;, <strong>messing</strong> it <strong>up more and more</strong>.</p>



<p>I think <strong>one of</strong> the most <strong>common sources</strong> for this to work on are custom<strong> plugins</strong>.</p>



<p><strong>You should</strong> (if you don&#8217;t have already) <strong>make your own plugin</strong>, which is responsible for handling those occasionally needed files.</p>



<p>I mean it <strong>could </strong>also <strong>tackle topics</strong> <strong>regarding </strong>the eventually existing <strong>shortcodes</strong> or custom page types.</p>



<p>Those <strong>could be the ones </strong>actually <strong>needing </strong>your <strong>stylesheets and </strong>client sided, manipulating <strong>JavaScript code</strong>.</p>



<p><strong>Keep in mind</strong> though, that I <strong>won&#8217;t dive</strong> <strong>into building </strong>your own <strong>WordPress plugin</strong> in this post that deep.</p>



<p>This <strong>would be</strong> some kind of <strong>separated topic</strong> for another one of my blog posts!</p>



<h2 class="wp-block-heading" id="why-you-should-consider-loading-scripts-only-for-specific-pages">Why you should consider loading scripts only for specific pages</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Why-should-WordPress-load-script-on-specific-page.jpg"><img decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/Why-should-WordPress-load-script-on-specific-page.jpg" alt="Why should WordPress load script only specific pages" class="wp-image-8907" title="Why should WordPress load script only specific pages"/></a><figcaption class="wp-element-caption">Why should WordPress load script only specific pages</figcaption></figure>



<p>So <strong>after </strong>we had this <strong>brief introduction</strong>, let&#8217;s <strong>dive </strong>a bit <strong>deeper </strong>into the developer aspect of loading &#8222;assets&#8220;.</p>



<p><strong>Those assets </strong>can be <strong>different files like </strong>the normal <strong>styling thingys</strong>, for example &#8222;<em>Cascading Style Sheets</em>&#8220; or in short <strong>CSS</strong>.</p>



<p>They <strong>will enhance </strong>your <strong>visual user experience</strong> by <strong>beautifying </strong>your user <strong>interface</strong>.</p>



<p><strong>Whether </strong>it&#8217;s <strong>about making </strong>those <strong>shiny </strong>glossy <strong>buttons</strong>, to make your user wanna click that thing.</p>



<p>Or like<strong> aligning </strong>your <strong>content </strong>the right way with like the <strong><a href="https://en.wikipedia.org/wiki/CSS_Flexible_Box_Layout" target="_blank" rel="noreferrer noopener">flexbox layout</a></strong> – oh gawd, <strong>thank</strong> you <strong>for having </strong>that.</p>



<p>One <strong>other common </strong>asset-ish <strong>file </strong>would be the <strong>clientside script language</strong> file type called <strong>JavaScript</strong>.</p>



<p>It <strong>will help</strong> you <strong>generating </strong>dynamic <strong>HTML </strong>on the client side, <strong>or sending </strong>like AJAX <strong>requests </strong>to a server.</p>



<p>But <strong>as always</strong>: <strong>Most things having </strong>an <strong>upside</strong>, <strong>will mostly </strong>come at some kind of cost <strong>or </strong>a <strong>downside</strong>.</p>



<p>In the <strong>next step</strong>, we will <strong>talk about </strong>the <strong>common downsides </strong>of those nice helper files.</p>



<h3 class="wp-block-heading" id="the-thing-they-all-share-in-common-using-up-space-taking-time">The thing they all share in common – using up space, taking time</h3>



<p>So <strong>even after generating </strong>that nice <strong>HTML </strong>output <strong>by </strong>for example <strong>PHP and styling </strong>it appropriately <strong>using CSS </strong>– theres one thing.</p>



<p><strong>Those </strong>nice helper <strong>files </strong>will <strong>need webspace</strong>, just like every other downloadable file.</p>



<p>They <strong>have to be downloaded</strong> by the users browser and will mostly consume <strong>one request per file</strong>.</p>



<p><strong>Each one </strong>of those <strong>requests will consume </strong>some <strong>time</strong>, even though if they are fast, right!?</p>



<p>So <strong>we </strong>will <strong>have </strong>to <strong>consider saving</strong> each one of those not really necessary <strong>requests</strong>.</p>



<p>This <strong>will not only</strong> <strong>make </strong>our <strong>users happier</strong> by <strong>saving </strong>some <strong>data </strong>– especially if you are on a mobile network.</p>



<p>It <strong>will also increase</strong> the <strong>loading time </strong>in general, <strong>making </strong>the <strong>user reaching </strong>it goal the <strong>fastest </strong>way possible.</p>



<p><strong>Google and other</strong> search engines <strong>will </strong>also <strong>love </strong>you <strong>for caring about your users</strong> considering that.</p>



<p><strong>I&#8217;m sure</strong> there are <strong>a lot of persons</strong> who <strong>could </strong>actually <strong>improve </strong>their general <strong>SEO by speeding up </strong>their site like this.</p>



<h3 class="wp-block-heading" id="commonly-used-methods-to-tackle-that">Commonly used methods to tackle that</h3>



<h4 class="wp-block-heading" id="a-typical-case-for-caching">A typical case for caching</h4>



<p>The <strong>first and </strong>I think the <strong>most </strong>well-known <strong>mechanism is </strong>actually the <strong><a href="https://en.wikipedia.org/wiki/Web_cache" target="_blank" rel="noreferrer noopener">caching functionality</a></strong> of most browsers.</p>



<p><strong>Not every</strong> page <strong>visit will trigger downloading </strong>the same assets over and over again.</p>



<p><strong>Common assets</strong>, especially <strong>images</strong>, <strong>styles </strong>and <strong>JavaScript </strong>files will be <strong>managed by </strong>your <strong>browser</strong>.</p>



<p>For sure this is <strong>one of the great possibilities</strong> to help the client visiting your websites (and like search engine bots).</p>



<p>But <strong>even if</strong> this is <strong>one nice</strong> available helper, this <strong>won&#8217;t help at the very root </strong>of our problem.</p>



<p>Let&#8217;s <strong>talk about that in a second</strong>..</p>



<h4 class="wp-block-heading" id="minification-for-more-help">Minification for more help</h4>



<p><strong>Another on commonly used </strong>method to tackle that problem is called &#8222;<strong><a href="https://en.wikipedia.org/wiki/Minification_(programming)" target="_blank" rel="noreferrer noopener">minification</a></strong>&#8222;.</p>



<p>It <strong>will basically remove </strong>any <strong>unneeded </strong>character <strong>from </strong>the <strong>output </strong>to make it even more compact.</p>



<p><strong>Some tools </strong>are <strong>even </strong>able to <strong>optimize </strong>the <strong>output by renaming </strong>like variables, functions, etc.</p>



<p>This way the tools <strong>help us by saving</strong> even <strong>more speed</strong>, <strong>storage </strong>and <strong>bandwith</strong>.</p>



<p><strong>Surely </strong>this is actually <strong>nice</strong>, as it can already <strong>make </strong>a <strong>great impact </strong>on the results, but this <strong>isn&#8217;t</strong> like the <strong>top solution</strong>.</p>



<p><strong>Like this</strong>, we <strong>only work on </strong>the <strong>ending </strong>side, <strong>instead </strong>of focusing <strong>on the root </strong>of the problem.</p>



<h2 class="wp-block-heading" id="the-real-approach-loading-scripts-only-on-specific-pages">The real approach – loading scripts only on specific pages</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/WordPress-only-load-necessary-scripts-on-specific-pages.jpg"><img decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/WordPress-only-load-necessary-scripts-on-specific-pages.jpg" alt="WordPress only load necessary scripts on specific pages" class="wp-image-8910" title="WordPress only load necessary scripts on specific pages"/></a><figcaption class="wp-element-caption">WordPress only load necessary scripts on specific pages</figcaption></figure>



<p>So <strong>now </strong>we have come <strong>to the final point</strong>, meaning <strong>what you should</strong> possibly actually <strong>do</strong>.</p>



<p><strong>Avoiding </strong>those <strong>unnecessary requests</strong> <strong>by </strong>just <strong>loading </strong>those <strong>files you actually need</strong>.</p>



<p><strong>In my case</strong> I had this nice, selfmade <strong>plugin</strong>, which was <strong>loading some JavaScript </strong>and <strong>CSS files</strong> into the page.</p>



<p><strong>At first,</strong> it <strong>generated </strong>the <strong>needed HTML structure </strong>and <strong>appended all </strong>these shiny <strong>classes </strong>to the correct elements.</p>



<p><strong>Then </strong>it <strong>would </strong>obviously <strong>rely on </strong>the correct <strong>scripts being loaded </strong>for those pages.</p>



<p>But the <strong>problem was</strong>: I <strong>noticed </strong>that those <strong>scripts were loaded on </strong>literally <strong>every post </strong>on my site.</p>



<h3 class="wp-block-heading" id="even-more-problems-errors">Even more problems – errors</h3>



<p><strong>Next to </strong>those <strong>already discussed problems </strong>like space, speed, requests, etc. <strong>there was one more </strong>problem.</p>



<p><strong>I was</strong> just <strong>busy doing some </strong>other <strong>stuff </strong>on one of the blog posts, when I <strong>noticed errors in the console</strong>.</p>



<p>A <strong>JavaScript file </strong>was <strong>looking for different HTML elements</strong> ready to get manipulated by the script, but wait a second!</p>



<p>The <strong>elements weren&#8217;t there</strong>, because they would <strong>only </strong>be generated <strong>by some shortcode </strong>which wasn&#8217;t used on that post.</p>



<p>Even if i think that <strong>most search engine crawlers </strong>and users browsers will <strong>ignore </strong>console <strong>errors</strong>.</p>



<p><strong>Don&#8217;t pin me down</strong> on this, <strong>but </strong>I think this <strong>counts for </strong>at least <strong>non critical</strong> or ground breaking <strong>errors</strong>.</p>



<p>Still it <strong>doesn&#8217;t feel right </strong>like that, I mean the <strong>page won&#8217;t be rendered correctly</strong>.</p>



<h3 class="wp-block-heading" id="enqueuing-scripts-right-before-our-optimization">Enqueuing Scripts – right before our optimization</h3>



<p><strong>Before </strong>we <strong>go ahead </strong>and actually work on a better solution, let&#8217;s see <strong>how</strong> we could&#8217;ve <strong>done</strong> it <strong>before</strong>.</p>



<p>Basically it&#8217;s <strong>done by hooking</strong> our plugin <strong>into </strong>the <strong><a href="https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/" target="_blank" rel="noreferrer noopener">&#8222;wp_enqueue_scripts&#8220;-action</a></strong> <strong>and </strong>doing some <strong>additional stuff</strong>.</p>



<p>That <strong>additional stuff </strong>is actually <strong>calling </strong>those <strong>additional functions </strong>depending on our needs:</p>



<ul class="wp-block-list">
<li><strong><a href="https://developer.wordpress.org/reference/functions/wp_enqueue_style/" target="_blank" rel="noreferrer noopener">wp_enqueue_style</a></strong> &#8211; for <strong>registering </strong>css <strong>stylesheets</strong></li>



<li><strong><a href="https://developer.wordpress.org/reference/functions/wp_enqueue_script/" target="_blank" rel="noreferrer noopener">wp_enqueue_script</a> </strong>&#8211; for <strong>registering JavaScript </strong>files</li>
</ul>



<p>So <strong>one basic example code</strong> could look similar to this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">function vendor_pluginabbreviation_load_assets()
{
  $plugin_url = plugin_dir_url(__FILE__);
  wp_enqueue_style('pluginabbreviation-style', $plugin_url . 'css/style.css');
  wp_enqueue_script('pluginabbreviation-script', $plugin_url . 'js/script.js', [], '', true);
}
add_action('wp_enqueue_scripts', 'vendor_pluginabbreviation_load_assets');</pre>



<h3 class="wp-block-heading" id="let-s-optimize-only-loading-scripts-for-specific-pages">Let&#8217;s optimize – only loading scripts for specific pages</h3>



<p><strong>Now let&#8217;s</strong> actually <strong>optmize </strong>this code from above, to <strong>only load scripts</strong>, <strong>when </strong>we are <strong>on </strong>a <strong>specific </strong>page/on specific <strong>pages</strong>.</p>



<p>To do so, we need <strong>find out</strong>, <strong>which pages or posts</strong> we are <strong>actually targeting</strong>, so let&#8217;s do this.</p>



<p><strong>Go into</strong> your <strong>WordPress backend</strong> and <strong>check </strong>the <strong>page or post id</strong> <strong>by looking at </strong>the parameter provided in the <strong>URL</strong>:.</p>



<p>Please <strong>don&#8217;t get confused by</strong> my <strong>german </strong>WordPress <strong>installation</strong>, as it will still look similar in other languages.</p>



<h4 class="wp-block-heading" id="finding-out-the-post-page-ids-of-desire">Finding out the post &amp; page ids of desire</h4>



<p>You <strong>can </strong>just <strong>use </strong>your <strong>mouse to hover over</strong> the corresponding <strong>blog post</strong> <strong>and spot </strong>the post <strong>id</strong> <strong>in </strong>the <strong>URL </strong>display <strong>at </strong>the very <strong>bottom</strong>.</p>



<p>The <strong>same way will</strong> also <strong>work for finding </strong>out <strong>page ids</strong>, just <strong>go to</strong> &#8222;<strong>pages</strong>&#8220; and do the same.</p>



<p><strong>Keep in mind</strong>, <strong>that </strong>the <strong>parameter </strong>will <strong>still </strong>be <strong>called </strong>&#8222;post <strong>id</strong>&#8222;, <strong>even </strong>though it is <strong>actually a page</strong>.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Finding-out-WordPress-post-ids-by-hovering-over-the-url.png"><img loading="lazy" decoding="async" width="767" height="957" src="https://robbelroot.de/wp-content/uploads/2022/02/Finding-out-WordPress-post-ids-by-hovering-over-the-url.png" alt="" class="wp-image-8920"/></a></figure>



<h4 class="wp-block-heading" id="checking-for-being-a-post-and-matching-ids">Checking for being a post and matching ids</h4>



<p>In the <strong>next step</strong> we should <strong>take care about</strong> actually <strong>checking </strong>for those <strong>ids</strong> and <strong>requiring them </strong>to be in our favor.</p>



<p>For sure you <strong>can proceed how you would like to</strong>, but in my case, it looks like this.</p>



<p>I <strong>want </strong>those <strong>scripts and styles only to be added</strong>, <strong>if </strong>it&#8217;s one of the <strong>specified ids</strong>, so I <strong>created meaningful variables </strong>for those ids.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$englishConverterPostId = 7275;
$germanConverterPostId = 7194;</pre>



<p>Then it <strong>should already work</strong>, <strong>if </strong>the <strong>WordPress <a href="https://developer.wordpress.org/reference/functions/is_page/" target="_blank" rel="noreferrer noopener">&#8222;is_page&#8220;-function</a></strong> is <strong>returning true </strong>for our ids.</p>



<p>You <strong>can also pass </strong>page <strong>slugs</strong>, <strong>ids</strong>, or <strong>even a mix</strong> consisting of both possibilities to this function as well.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$isConverterPost = is_page([ $englishConverterPostId, $germanConverterPostId ]);</pre>



<p>Sadly I <strong>had to find out that it didn&#8217;t work </strong>for my case, even though it&#8217;s the most recommended approach.</p>



<p>So I did some debugging by checking, <strong>if </strong>the <strong>&#8222;is_page&#8220;-function existed </strong>at this time, first:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print_r([ 'isPageExists' => function_exists('is_page'), ]);</pre>



<p>That actually <strong>resulted into</strong> the output of a <strong>true-ish</strong> value, <strong>meaning</strong> the <strong>function existed</strong> – okay.</p>



<p><strong>Next </strong>I <strong>confirmed </strong>the <strong>page ids</strong> being the right ones, but I <strong>still couldn&#8217;t make it work</strong>, the <strong>function still </strong>returned <strong>false</strong>.</p>



<p>I <strong>didn&#8217;t know</strong> and I <strong>still don&#8217;t know what</strong> I was/am doing <strong>wrong</strong>, so I just <strong>made it work</strong> <strong>another way</strong>.</p>



<p>So I just <strong>created access to</strong> the <strong>global </strong>&#8222;<strong>post</strong>&#8222;-variable and <strong>checked the id</strong> on that one:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">global $post;
$englishConverterPostId = 7275;
$germanConverterPostId = 7194;
$isConverterPost = $post->ID === $germanConverterPostId || $post->ID === $englishConverterPostId;</pre>



<p><strong>Finally </strong>I <strong>could continue </strong>by <strong>checking </strong>that &#8222;<strong>isConverterPost</strong>&#8222;-<strong>variable </strong>to register the scripts depending on that:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">if ($isConverterPost) {
  $plugin_url = plugin_dir_url(__FILE__);
  wp_enqueue_style('dcc-style', $plugin_url . 'css/style.css');
  wp_enqueue_script('dcc-script', $plugin_url . 'js/script.js', [], '', true);
}</pre>



<p>At the very end, <strong>the final script looked like this</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">function rr_dcc_load_plugin_assets()
{
  global $post;
  $englishConverterPostId = 7275;
  $germanConverterPostId = 7194;

  $isConverterPost = $post->ID === $germanConverterPostId || $post->ID === $englishConverterPostId;
   
  if ($isConverterPost) {
    $plugin_url = plugin_dir_url(__FILE__);
    wp_enqueue_style('dcc-style', $plugin_url . 'css/style.css');
    wp_enqueue_script('dcc-script', $plugin_url . 'js/script.js', [], '', true);
  }
}
add_action('wp_enqueue_scripts', 'rr_dcc_load_plugin_assets');</pre>



<p>If you need a <strong>fully working</strong>, <strong>easy to use example</strong>, then just go ahead and <strong>download the example files</strong> below.</p>



<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Conclusion.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/Conclusion.jpg" alt="WordPress load script on specific page – Conclusion" class="wp-image-8867" title="WordPress load script on specific page – Conclusion"/></a><figcaption class="wp-element-caption">WordPress load script on specific page – Conclusion</figcaption></figure>



<p>So <strong>at the end</strong> of this little post we <strong>talked about loading assets</strong> and a good way of doing so.</p>



<p><strong>As loading assets will consume</strong> like <strong>requests</strong>, time &amp; data file by file, we should <strong>avoid loading unnecessary ones</strong>.</p>



<p>We <strong>can help by using</strong> a mechanism called &#8222;<strong>minification</strong>&#8222;, but this <strong>will still conquer</strong> a <strong>general rule </strong>of thumb:</p>



<p>Just <strong>avoid what you don&#8217;t need</strong>, or in our case – <strong>don&#8217;t load</strong> those <strong>unused</strong> asset <strong>files</strong>!</p>



<p><strong>Finally</strong>, we <strong>implemented </strong>a <strong>functionality </strong>by using the well-known <strong>WordPress function</strong> called &#8222;<strong><a href="https://developer.wordpress.org/reference/functions/is_page/" target="_blank" rel="noreferrer noopener">is_page</a></strong>&#8222;.</p>



<p><strong>This way</strong>, we would <strong>only load needed JavaScript &amp; CSS files</strong> on the actual needed pages.</p>



<h2 class="wp-block-heading" id="downloads">Downloads</h2>



<p>Here you will find an <strong>easy to install</strong> and <strong>quick to use example</strong>.</p>



<p>Just <strong>download </strong>the <strong>zip folder</strong>, <strong>unzip </strong>it <strong>and upload </strong>the containing folder <strong>to</strong> your <strong>WordPress plugins </strong>folder.</p>



<p><strong>After that </strong>(or even before) you need to <strong>adjust </strong>the page/post <strong>ids</strong> <strong>to </strong>the ones <strong>you desire</strong>.</p>



<p><strong>Then </strong>you can <strong>activate </strong>this example <strong>plugin </strong>in your WordPress backend and <strong>visit the configured areas</strong>.</p>



<p>The <strong>script file</strong> <strong>and </strong>the <strong>stylesheets </strong>should <strong>only</strong> be <strong>loaded on </strong>the <strong>specified </strong>posts/pages.</p>



<p>Then <strong>visit another</strong> non configured page id and <strong>see that it&#8217;s not loaded there</strong>.</p>



<div class="wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="https://bit.ly/wordpress-load-script-on-specific-page-download" target="_blank" rel="noreferrer noopener">WPLoadScriptOnSpecificPage.zip</a></div>
</div>



<h2 class="wp-block-heading" id="related-posts">Related posts</h2>



<ul class="wp-block-list">
<li><strong><a href="https://robbelroot.de/blog/making-php-generate-a-uuid-with-ease-heres-how/" target="_blank" rel="noreferrer noopener">PHP generating a UUID</a></strong></li>



<li><strong><a href="https://robbelroot.de/blog/hacking-a-webshop/" target="_blank" rel="noreferrer noopener">Hacking a Webshop</a></strong></li>



<li><strong><a href="https://robbelroot.de/blog/converting-davinci-resolve-markers-to-youtube-chapters/" target="_blank" rel="noreferrer noopener">Converting DaVinci Resolve markers to YouTube chapters</a></strong></li>
</ul>



<p></p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/wordpress-loading-scripts-only-on-specific-pages-or-posts/">WordPress – Loading scripts on specific pages or posts in 2024</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/wordpress-loading-scripts-only-on-specific-pages-or-posts/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
