Include a unique identifier to a file name, and then update any mentions of it using Gulp

I am trying to automate the process of renaming a CSS file and updating the reference in my PHP file. Currently, I have a file named base.css and a PHP file called styles.php containing the following code:

wp_enqueue_script('base', get_template_directory_uri() . '/css/base.css');

My goal is to use Gulp to hash rename the base.css file to something like base-375dfd5f.css and then automatically update the reference in styles.php to:

wp_enqueue_script('base', get_template_directory_uri() . '/css/base-375dfd5f.css');

Answer №1

To enhance your workflow, consider incorporating the Gulp rename pipe to adjust the CSS file name within your stream. Additionally, utilize Gulp inject string for updating the PHP file accordingly.

Here's an example implementation:

 var gulp   = require('gulp'),
     rename = require('gulp-rename'),
     inject = require('gulp-inject-string');

 var myHash = generateUniqueHash();

 gulp.task('UpdateCSSFileName', function () {
        return gulp.src('main.css')
                .pipe(rename('main' + myHash + '.css'))
                .pipe(gulp.dest('./dist')
 });

 gulp.task('InjectHashToPHP', function () {
        return gulp.src('index.php')
                .pipe(inject.replace('main.css', 'main' + myHash + '.css'))
                .pipe(gulp.dest('./dist')
 });

Answer №2

After some research, I came across a helpful tool called gulp-cachebust that does exactly what I need: it assigns unique hashes to file names and updates references accordingly.

Latest Update

In the end, I opted for using gulp-rev to generate a manifest and modify file names, along with gulp-rev-replace to update references to those files. This approach has proven to be very effective.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I have a page division with scroll bars and I want to ensure that the entire content of the division is displayed

I am facing an issue with a div and table on my webpage: <div id="tablediv"> <table id="table"> <tr><th>headers</th></tr> <tr><td>contents</td></tr> </table> <d ...

Conceal the form upon submission using Angular JS

Currently trying to understand some Angular concepts and following a tutorial for practice. When I click the button below, a form is displayed. How can I hide this form once it has been submitted? Essentially, I want the form to disappear after submission ...

Adjust the color of the navigation bar as you scroll

I recently used pure JavaScript to change the color of my navbar after scrolling. Surprisingly, it worked perfectly fine on Google Chrome without any issues. However, when I decided to test it on Firefox, the feature stopped working altogether. Can anyone ...

Ensure that adjacent elements operate independently from one another

The code snippet provided above showcases four components: StyledBreadcrumbs, FilterStatusCode, Filter, LinkedTable. The FilterStatusCode component enables users to input search data using TagInput. If the user inputs numerous tags, this component expands ...

Changing the height of columns in an email template for different screen sizes in Outlook

This email was designed specifically for marketing purposes within my company. We obtained a template from themeforest and have made modifications accordingly. While I have managed to address most of the issues caused by Outlook, there is one particular pr ...

The CSS style of the Div element is not being displayed properly when embedded with JavaScript

Currently, I am working on a simple page for practice purposes. The main issue I am facing is with a div element that has a red border and a blue background. Inside the div, there is a script tag calling an external JavaScript file. Surprisingly, the JavaS ...

Create a Seamless Scroll to Anchor Effect with JQuery and a Fixed Navigation Bar

I'm encountering an issue with smooth scrolling on a webpage that has a sticky header. When I click on a navigation link to scroll to a specific section using scrollTop: href.offset().top - 100, the page doesn't behave as expected. It seems to bo ...

the label remains grounded even as the browser autocompletes passwords with the password manager

https://i.stack.imgur.com/vJMyg.png I noticed that the password label on my login form does not float when the browser's password manager auto-completes. Can someone help me fix this issue? Attached is an image for reference. /* login form ...

My div is set to a specific width, but the content continues to overflow beyond its boundaries

Having trouble with my HTML code here - I can't seem to get my div to adjust its height automatically when the content exceeds the width. When I set the width to 70px, the content still overflows. Any suggestions? Here's my code. Thanks in advan ...

Creating an HTML form that resembles StackOverflow's form

I am having an issue with the behavior of my form when inserting multiple tags. I want it to function like the one on this particular website where a scroll bar appears and a new line is created. Is there a way to keep everything on the same row? Check ou ...

What is the best method to retrieve information from two tables on a webpage that have identical classes?

I am facing a challenge in retrieving or selecting data from two different tables that share the same class. My attempts to access this information using 'soup.find_all' have proven to be difficult due to the formatting of the data. There are m ...

The range slider is malfunctioning in the audio player, JQuery, and HTML

I have been struggling with getting the Range slider to work on my custom-built mp3 player using jQuery. Despite trying various codes from different sources, I can't seem to get it to function correctly. Strangely enough, it works fine on Mozilla brow ...

What is the method for formatting within the <textarea> element?

While working on developing a comment system, I recently made the discovery that text areas cannot be formatted to retain paragraphs, line breaks, etc. This was particularly noticeable when comparing sites like StackOverflow, which uses a text editor inste ...

Why does everything seem to move in sync with the table's scrolling?

I recently had an issue resolved that fixed most of my concerns: I needed to make three separate edits, and now when I reintroduce the other two edits, they are included within the table section and scroll along with the table instead of being stuck beneat ...

Ensuring Proper Highlighting for HTML Select Options

I am working on a project involving an HTML Drop-down menu. Check out the code I have so far: http://jsfiddle.net/dineshk/u47xjqLv/ HTML Code <select> <option class="first" style="display:none;">HH</option> <option class= ...

When the text input is selected, automatically scroll to the bottom

I found a codepen example by Chris Coyer and decided to fork it. My goal is to make the label move to the top instead of staying at the bottom, as in his original design. However, I am facing an issue with getting the cursor to move to the bottom when typ ...

Troubleshooting: CSS Animation issue with fade in and transform effects on individual words

I want to achieve a cool effect where each word starts at 0 opacity and translateY(65px), then fades in one word at a time while transitioning to translateY(0). Although the words are fading in, they are not translating as desired. For reference, check out ...

What is the best way to eliminate excess space from the top and bottom of an HTML email?

Trying to craft an HTML email using tables but encountering an issue. When sending the email from Gmail to an iCloud account, there's unwanted white space at the top and bottom. Below is a snippet of the code I've prepared for this query. I exp ...

Best practice for creating two side-by-side cards with responsiveness in Bootstrap and Django

I have created a unique index.html page using Django templates: {% extends "layout.html" %} {% block body %} {% for entry in entries %} <div class="card h-100 mb-3" style="max-width: 540px;"> <a hre ...

Delay the page briefly before redirecting

I want to implement a feature on my WordPress website where after successfully submitting a form, the visitor is shown a message like 'form submitted successfully' for a few seconds before being redirected back to the page they were on. The redir ...