0

I want to use a variable inside the function wp_enqueue_style(), right now the only way i figure out to do so is to define a constant but it needs to be variable

 define( 'NAME', 'mystyle' );
 wp_enqueue_style(NAME .'-style', ABS_URL . /includes/box.css' , false ); 

How could I add that variable to this function ? I tried this and it doesnt work, any insights on this?

 $name="mystyle";
 wp_enqueue_style($name .'-style', ABS_URL . /includes/box.css' , false ); 
  • In what way did it not work? – janh Nov 2 '17 at 22:00
  • the variable $name is empty, it calls the box.css style but the id is "-style-css" instead of "mystyle-style-css" – Johanna Ferreira Nov 2 '17 at 23:32
  • Is that the actual code? I can’t see any reason it wouldn’t work. – Jacob Peattie Nov 3 '17 at 0:33
  • How could $name be empty if you set it with $name="mystyle" just in the previous line? – janh Nov 3 '17 at 22:14
1

this syntax doesn't seem right to me.

First of all what is ABS_URL? If it's a constant that you've defined earlier, then there is an ' missing before /includes/box.css'. The code should look like this:

$name = 'mystyle';
wp_enqueue_style( $name .'-style', ABS_URL . '/includes/box.css', false );

It works as it should, the output is: <link rel='stylesheet' id='mystyle-style-css' href='whatever-there-is-in-ABS_URL-constant' type='text/css' media='all' />

Also, make sure if you need to define this constant at all - if you want to load stylesheet from your theme's folder or its subfolders, use get_stylesheet_directory_uri() function:

$name = 'mystyle';
wp_enqueue_style( $name .'-style', esc_url( get_stylesheet_directory_uri() ) . '/includes/box.css' , false ); 
  • yeah, it was something really dumb, i didnt pass that variabe as an argument because the wp_enqueue_style was inside a function – Johanna Ferreira Nov 4 '17 at 15:26

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.