This article explains the Limit to pages or posts function and Exclude pages or posts function in CSS Optimizer and how to use it.
Limit to / Exclude
By default, CSS Optimizer will run on all front-end pages of your website. Using either Limit to pages or posts or Exclude pages or posts, you can tell CSS Optimizer to run on selected pages only.
Which should I use?
Since the two functions are inherently in conflict with each other, you can only use one at a time. As a guide, if you have a small number of pages you want CSS Optimizer to run on then use Limit to pages or posts. If you have a small number of pages you don’t want CSS Optimizer to run on then use Exclude pages or posts.
To choose a function simply enter a value into the its field. As soon as a value is entered, you will not be able to enter any values into the other function.
To reset and choose the other function, just clear all the values and click on an empty space.
How to select pages / posts
Limit to and Exclude are versatile functions that can take a list of different types of values like:
- Magic strings enclosed in square brackets. Example: [Home]
- Any portion of a URL or URL slug. Example: 2021/09/20/hello-world
- Page ID. Example: id=2
Note, when using the second type (any portion of a URL) it will affect any URL containing the entered value. For example, sample-page will affect pages with URLs like /sample-page, /sample-page-1, /example/1-sample-page, etc.
You can enter a list of values by typing each value in a new line.
Examples
1. Magic strings
In this example, we will set CSS Optimizer to only run on the home page. To do this we will use the Limit to pages or posts function and select the home page with Magic string value.
- Go to CSS Optimizer Options page.
- In the Limit to pages or posts field, enter the magic string enclosed in square brackets. For our example, it is [Home].
- Click on Save Changes.
To verify, let’s check the source of the home page.
We can see the .purge.css file that shows CSS Optimizer is running on this page.
Let’s check the source of a page other than the home page. Like the blog page.
We can see the .purge.css file has changed back to .css file. CSS Optimizer is not running on this blog page. Now we are sure that CSS Optimizer is only running on the home page.
2. Any portion of a URL
In this example, we will set CSS Optimizer to only run on the blog post titled Hello World. We will use Limit to pages or posts function and select the page using a portion of the page’s URL.
- Open the page to be selected.
- Copy a part of the URL.
- Go to CSS Optimizer Options page.
- In the Limit to pages or posts field, paste the URL.
- Click on Save Changes.
Now CSS Optimizer will only run on page’s with a URL that contains the words 2021/09/20/hello-world.
Alternatively, you can use a URL slug. Here’s how to find it:
- Find the post/page in Wordpress and click edit.
- In the Permalink section, look for URL slug.
- Copy and paste the URL slug in a new line.
For this example, I have pasted the URL slug into the Exclude pages or posts field. This tells CSS Optimizer to run on all front-end pages except for pages with a URL containing the word “sample-page”.
3. Page ID
In this example, we will set CSS Optimizer to not run on specific pages. To do this, we will find the page ID then enter them into Exclude pages or posts field.
- Find the post/page in Wordpress and click edit.
- Find the page ID from the URL of the edit page/post. In our example, it is 2.
- Go to CSS Optimizer Options page.
- In Exclude pages or posts field, type “id=“ followed by the page ID. In our example, it is “id=2”
- Click on Save Changes.
Now, CSS Optimizer will run on all pages except for the page/post with the page ID of 2.