0

So far I did the following task to add theme-dependent CSS/JS files; I added to the theme's functions.php this code, then created the relevant CSS and JS files:

function my_theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_parent_theme_file_uri() . 'style.css' );
}
add_action( 'my_theme_enqueue_styles', 'wp_enqueue_styles' );

function my_theme_enqueue_assets() {
    wp_enqueue_script( 'behavior', get_theme_file_uri( 'behavior.js' ), array(), null, true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );

But what if I want my CSS and JS to be theme-independent? Say, I want to had a few lines of CSS and JS that will be available in all themes whatsoever.

Is there a way to do so in WordPress outside the WordPress User Interface?

  • 1
    You would need to load them in a plugin. – Jacob Peattie Mar 7 '18 at 5:21
  • Oh crap. Any recommendation? – user9303970 Mar 7 '18 at 5:29
  • No I mean make a plugin that has the stylesheet/scripts and enqueues them. – Jacob Peattie Mar 7 '18 at 6:48
  • I am not a PHP programmer but I do program in Bash and JavaScript so if you'll write an (short?) answer on this, I believe I could understand what's being done. So please do, if you want. – user9303970 Mar 7 '18 at 7:12
1

Use the following code to create a simple plugin. Create a directory tia inside plugins directory and save the code in a file tia.php then put it inside tia directory and finally activate from plugin page.

<?php
/**
 * Plugin Name: Theme Independent Assets
 * Author: Obi
 * Version: 0.0.7
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

function tia_enqueue_scripts() {
    wp_enqueue_script( ... );
    wp_enqueue_style( ... );
}
add_action( 'wp_enqueue_scripts', 'tia_enqueue_scripts' );
  • Thanks. Must you notify a version like 7.0.0? null isn't good? Shouldn't it be 1.0? – user9303970 Mar 7 '18 at 9:22
  • Does the CSS file need to have a special declaration in its top part? – user9303970 Mar 7 '18 at 9:24
  • 1
    I like the James Bond code 007! That's why whatever I do I start the version with 0.0.7. You can use whatever you want. No, the CSS doesn't need any meta comment like the theme style.css. Plugin's meta information is stored in plugin main php file, in this plugin the main file is tia.php – obiPlabon Mar 7 '18 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.