Skip to main content
Home
Drupal life hacks

Main navigation

  • Drupal
  • React
  • WP
  • Contact
  • About
User account menu
  • Log in

Breadcrumb

  1. Home

JavaScript Inclusion in WordPress and Drupal

By admin, 8 April, 2024

The functions `wp_enqueue_script()` in WordPress and `library` in Drupal serve similar purposes, which is to include external JavaScript files into the webpage. However, they have different implementations and usage patterns:

1. **WordPress - `wp_enqueue_script()`**:
  - `wp_enqueue_script()` is a function in WordPress used to enqueue JavaScript files into WordPress.
  - It allows you to register and enqueue JavaScript files to be added to the page.
  - This function ensures that the script is added only once, and dependencies are loaded in the correct order.
  - Example:
    ```php
    wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0', true);
    ```

2. **Drupal - `library`**:
  - In Drupal, JavaScript libraries are defined in `.libraries.yml` files, and they can be included in a theme or module.
  - Libraries can include CSS and JavaScript files and define dependencies between them.
  - The `library` key in a Drupal theme or module `.info.yml` file is used to specify which libraries should be loaded.
  - Example:
    ```yaml
    libraries:
      - my-module/my-library
    ```

In summary, while `wp_enqueue_script()` in WordPress and `library` in Drupal both serve to include JavaScript files, they differ in their implementation details. WordPress uses a function to enqueue scripts, while Drupal defines libraries in `.libraries.yml` files and includes them using the `library` key in `.info.yml` files.

Tags

  • #Drupal Planet

Comments

About text formats

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Powered by Drupal