In the process of making the wordpress theme monkey wordpress plug-in, we often need to add style files or js script files. Since most users run multiple plug-ins on their websites, various files may be loaded, which can easily cause conflicts. Therefore, the WordPress system provides developers with a good queuing system for scripts and style files, which helps prevent script conflicts between plug-ins. In this article, I mainly introduce how to add Javascript files and CSS files in WordPress. It is especially useful for those who are just starting to learn WordPress theme and plug-in development.
Wrong way
Wordpress provides the wp_head hook to help us add specified header messages, such as common keywords and descriptions, to the head of the page. Many people also use this method to add the external style of the site. file and script file, add the following code:
add_action('wp_head', 'wpb_bad_script'); function wpb_bad_script() { echo ' '; //添加js文件 } ?>
Although this method is simple to use, it is not recommended. This loading method can easily cause conflicts with WordPress scripts.
Wordpress script queuing system
1. Introduction
Wordpress has a strong development community around the world. Many people are very actively involved in the development of WordPress themes and plug-ins, and they can be used for free. In order to prevent various developers from There are always script conflicts during the use of developed plug-ins. WordPress provides a very powerful script loading function wp_enqueue_script. Through this function, we can tell WordPress where to load the script and which frameworks the script depends on. Moreover, this function uses the built-in Javascript library, you can avoid loading the same script multiple times. This helps reduce page load times, as well as avoid conflicts with other themes and plugins.
2. Usage examples
Using the WordPress correct loading script is very simple, the code is as follows:
function wpb_adding_scripts() { wp_register_script('my_amazing_script', plugins_url('amazing_script.js', __FILE__), array('jquery'),'1.1', true); wp_enqueue_script('my_amazing_script'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' ); ?>
You can put the above code into your plug-in file or the functions.php file of your theme.
Explanation:
In the example, we first pass the function wp_register_script(), which receives 5 parameters:
$handle (string) (必須) 腳本名稱. 名稱必須唯一在之后函數(shù) wp_enqueue_script() 會使用到該名稱. Default: None $src (string) (必須) 腳本路徑,可以使用絕對路徑。 Default: None $deps (array) (可選) 腳本依賴包,依賴包會在腳本加載之前預(yù)先加載。 Default: array() $ver (string) (可選)腳本版本控制。 Default: false $in_footer (boolean) (可選) 定義腳本的位置,如果為true腳本會在頁面底部加載,默認(rèn)在head頭部加載。 Default: false
When we use the wp_register_script() function to register the script file, we can use the function wp_enqueue_script() to load the registration script file.
Maybe someone will ask us why we don’t load the script file directly, but register it first and then load it. Isn’t this unnecessary? In fact, this is mainly for other developers on the site to easily reference your core script files in other plug-ins or themes.
How wordpress loads CSS style files
Wordpress The loading of css style files is the same as the script file loading method introduced above. The following example:
function wpb_adding_styles() { wp_register_script('my_stylesheet', plugins_url('my-stylesheet.css', __FILE__)); wp_enqueue_script('my_stylesheet'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_styles' ); ?>
In the above examples, we also used the wp_register_script hook to load the style files.
In the example, we use plugins_url() to get the path of the style file. This is generally used in the plug-in development process. If we develop the theme and use the wp_register_script() function, we can use get_template_directory_uri() to get the style file. If the path is used in a child theme, you can use the function get_stylesheet_directory_uri() to obtain the path. The example is as follows:
function wpb_adding_scripts() { wp_register_script('my_amazing_script', plugins_url('amazing_script.js', __FILE__), array('jquery'),'1.1', true); wp_enqueue_script('my_amazing_script'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' ); ?>
Through the above analysis, I hope you will have a new understanding of adding Javascript files and CSS files to WordPress.

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Web development design is a promising career field. However, this industry also faces many challenges. As more businesses and brands turn to the online marketplace, web developers have the opportunity to demonstrate their skills and succeed in their careers. However, as demand for web development continues to grow, the number of developers is also increasing, resulting in increasingly fierce competition. But it’s exciting that if you have the talent and will, you can always find new ways to create unique designs and ideas. As a web developer, you may need to keep looking for new tools and resources. These new tools and resources not only make your job more convenient, but also improve the quality of your work, thus helping you win more business and customers. The trends of web development are constantly changing.

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader

To create an account on WordPress, simply visit its website, select the registration option, fill in the registration form, and verify your email address. Other ways to register include using a Google account or Apple ID. The benefits of signing up include creating a website, gaining features, joining the community, and gaining support.
