Home CMS Tutorial WordPress Share a WordPress plugin that merges CSS and JS

Share a WordPress plugin that merges CSS and JS

Apr 25, 2021 pm 05:42 PM
wordpress plugin

The following tutorial column from WordPress will introduce to you a WordPress plug-in that combines CSS and JS. I hope it will be helpful to friends who need it!

We usually consider merging multiple CSS or JS into one file for output. In order to solve this problem, I wrote this WordPress plug-in. Used to achieve this purpose in WordPress.

Implementation Principle

If you have read my previous "PHP Combined Compressed CSS Output Modular CSS Writing" you can first understand some of my processing methods for cache files. And some ideas for merging files and compressing code. The implementation principle of this plug-in is basically the same as that introduced in the article above.

Installation

First, download the plugin, I host it on my GitHub, you can download it on this page. After downloading, unzip it and directly upload the entire unzipped folder to your WordPress website plug-in directory. Then go to the background to enable it.

Use

to enter the background. There is a submenu "Minify" under the "Settings" menu. After entering, you can set its various options, and there are Comments to understand the functionality.

You must modify the theme to use wp-minify, because the way you output css and js is different. In your theme, delete the original CSS and JS output, and use the following method to output JS:

<?php wp_minfiy_js([&#39;/wp-content/themes/yourtheme/js/base.js&#39;,&#39;/wp-content/themes/yourtheme/js/module.js&#39;,&#39;/wp-content/themes/yourtheme/js/others.js&#39;]); ?>
Copy after login

The method of outputting css is the same, but use another function wp_minify_css(). The parameter of the function is an array, and the element inside the array is the path to the script file. Taking the WordPress installation directory as the root directory, write the complete relative path.

Why should we write scripts relative to the root directory of WordPress? Rather than relative to your current theme directory? In fact, the reason is very simple, because the css or js you reference is not necessarily in the theme directory. Sometimes you will reference styles or scripts in other directories. Of course, this is a relative path. You can even reference the script in the upper-level directory of WordPress, such as "/../test.js", but note that the beginning of the script must start with /, otherwise it may cause relative path errors.

Of course, the specific usage methods can be easily modified or used after you understand the principles. For more information, please read my blog: http://www.tangshuang.net/2432.html

The above is the detailed content of Share a WordPress plugin that merges CSS and JS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add online payment functionality to WordPress plugin How to add online payment functionality to WordPress plugin Sep 05, 2023 pm 04:19 PM

How to Add Online Payment Function to WordPress Plugin With the rapid development of the e-commerce industry, adding online payment function to the website has become a critical need. For those who use WordPress as a website development platform, there are many ready-made plugins that can help them achieve this goal. This article will introduce how to add online payment functionality to WordPress plug-in and provide code samples for reference. Determine the payment interface Before adding the online payment function, you must first determine the payment interface to use. current city

How to develop a feature that automatically updates a WordPress plugin How to develop a feature that automatically updates a WordPress plugin Sep 05, 2023 am 10:40 AM

How to Develop an Auto-Updating WordPress Plugin WordPress is a very popular open source content management system (CMS) with a rich plugin market to extend its functionality. To ensure that plugins are always up to date and secure, developers need to implement automatic updates. In this article, we’ll walk you through how to develop an auto-updating WordPress plugin and provide code examples to help you get started quickly. Preparation Before starting development, you need to prepare the following key steps: Create

How to use WordPress plug-in to implement email subscription function How to use WordPress plug-in to implement email subscription function Sep 05, 2023 pm 06:37 PM

How to use WordPress plug-in to implement email subscription function In today’s Internet age, email subscription function has become an indispensable part of website operation. Through the email subscription function, we can push the latest news, activities, offers and other information to users in a timely manner to enhance user stickiness and interactivity. In the WordPress website, we can implement the email subscription function by using plug-ins. The following will introduce how to use the WordPress plug-in to implement the email subscription function. Step 1: Choose the right plugin

How to develop a WordPress plugin that automatically generates project progress How to develop a WordPress plugin that automatically generates project progress Sep 05, 2023 am 08:48 AM

How to develop a WordPress plug-in that automatically generates project progress. In the process of project management, it is very important to understand the project progress. For users who use WordPress to build websites, being able to directly view project progress in the WordPress backend will greatly improve work efficiency. Therefore, it is very beneficial to develop a WordPress plugin that automatically generates project progress. This article describes how to develop such a plug-in and provides code examples. Plugin Overview The main functions of this plugin are

How to use WordPress plug-in to implement instant query function How to use WordPress plug-in to implement instant query function Sep 06, 2023 pm 12:39 PM

How to use WordPress plug-ins to achieve instant query function WordPress is a powerful blog and website building platform. Using WordPress plug-ins can further expand the functions of the website. In many cases, users need to perform real-time queries to obtain the latest data. Next, we will introduce how to use WordPress plug-ins to implement instant query functions and provide some code samples for reference. First, we need to choose a suitable WordPress plug-in to achieve instant query

How to add online voting functionality to WordPress plugin How to add online voting functionality to WordPress plugin Sep 05, 2023 am 11:09 AM

How to Add Online Polling Function to WordPress Plugin As one of the most popular content management systems, WordPress provides a rich plugin ecosystem that can easily extend the functionality of the website. In this article, we will explore how to add online voting functionality to a WordPress plugin. To achieve this goal, we will use WordPress core functionality and an open source plugin called "WP-Polls". 1. Download and install the "WP-Polls" plugin First, we

How to avoid Chinese garbled characters in WordPress How to avoid Chinese garbled characters in WordPress Mar 05, 2024 pm 09:36 PM

How to avoid Chinese garbled characters in WordPress requires specific code examples. In the process of using WordPress websites, many users will encounter the problem of Chinese garbled characters. Garbled Chinese characters will cause trouble for users when reading and browsing the website, and may also affect the user experience and search engine optimization of the website. In this article, we will introduce some methods to solve the Chinese garbled problem in WordPress and provide specific code examples. Set the database character set: First, make sure the database character set is set correctly to support the

How to develop a WordPress plugin that automatically generates e-books How to develop a WordPress plugin that automatically generates e-books Sep 05, 2023 am 08:01 AM

How to develop a WordPress plug-in that automatically generates e-books. With the popularity of social media and e-readers, e-books have become one of the important ways for people to obtain and share knowledge. As a WordPress developer, you may be faced with the need to create and publish e-books. To simplify this process, we can develop a WordPress plugin that automatically generates e-books. This article will teach you how to develop such a plug-in and provide code examples for reference. Step 1: Create the basic file structure of the plugin first

See all articles