1

I try to load the main stylesheet with enqueue_style - but it isn't working. Here is my code from functions.php:

function my_scripts() {
      wp_enqueue_style( 'main-style', get_template_directory_uri() . '/style.css', false, '1.0', 'all' );
      }
add_action( 'wp_enqueue_scripts', 'my_scripts' );

Loading the stylesheet in the header.php by using a link-tag works fine. I can't find the error - it seems that code is ignored - but function.php is loading.

I also tried to use get_template_directory_uri() instead of get_template_directory_uri().

Because it is the recommended way to link to styles and scripts I want to get this work. I also want to use enqueue_scripts for some js and jQuery scripts.

Thanks for any hint or advice

The solution: I forgot to call wp_head() in the header.php - added and everything works :-)

8
  • Do you see Errors (404) in your developer tool bar?
    – kaiser
    Commented Sep 20, 2013 at 11:02
  • there are no errors in the developer tool bar, is there a special point to check? I have the wp developer plugins installed, wp_debug is true.
    – Guido
    Commented Sep 20, 2013 at 14:11
  • I meant your browsers dev toolbar (F12 or Ctrl + Shift + I) and your Network or Console tab...
    – kaiser
    Commented Sep 20, 2013 at 14:22
  • ok, no there are no 404 errors - load of the page is 200, pictures are 304.
    – Guido
    Commented Sep 20, 2013 at 14:41
  • Please look at your scripts. Go to your Elements/DOM tab and locate your script: Is it there? Is in the Network tab? Can you see it under Resources > Frames > Scripts?
    – kaiser
    Commented Sep 20, 2013 at 15:30

4 Answers 4

3

The solution: I forgot to call wp_head() in the header.php - added and everything works :-)

1

get_template_directory_uri() has no trailing slash.

From Codex:

Note: Does not return a trailing slash following the directory address.

Use get_template_directory_uri() . '/style.css' and it will works (it should...).

2
  • thanks - I deleted the slash by testing around - added it again, but unfortunately it does not work.
    – Guido
    Commented Sep 20, 2013 at 11:08
  • 1
    @Guido to avoid someone give again and again my answer, you should edit the question adding the trailing slash if does not work. That aside, have you read kaiser comment?
    – gmazzap
    Commented Sep 20, 2013 at 11:20
0

Update your code to bellow:

 function my_scripts() {
          wp_enqueue_style( 'main-style', get_template_directory_uri() . '/style.css', false, '1.0', 'all' );
          }
    add_action( 'wp_enqueue_scripts', 'my_scripts' );

Note: get_template_directory_uri() has no trailing slash(/).

Whenever you have error related to css or js file loading first check in developer tools like firebug whether the link or script tags for css or js respectively loaded or not if loaded then get the url from there and execute in browser if coding coming then ok or else correct the path then change the same in code accordingly.

3
  • thanks - I tested already - it doesn't work with or without leading slash. directory is done, file is there, but it's not loadings the style.css
    – Guido
    Commented Sep 20, 2013 at 14:14
  • can you execute the file url in browser and let here know what you are getting. Like 404 or 403
    – Manish
    Commented Sep 20, 2013 at 14:36
  • executing the file url in the browser shows the correct style.css file. If I call the style.css within the header.php file (with a link tag) it works. Just doing it by wp_enqueue_style ends up with no css - I really wonder why. The file is there.
    – Guido
    Commented Sep 20, 2013 at 18:26
0

I don't have reputation to comment. This may work Try script for style too. Like this

function my_scripts() {
          wp_enqueue_script( 'main-style', get_template_directory_uri() . '/style.css', false, '1.0', 'all' );
          }
    add_action( 'wp_enqueue_scripts', 'my_scripts' );

or try

get_stylesheet_directory_uri();
1
  • thanks - it is not working that way. I also checked the slash and corrected in the orig. posting.
    – Guido
    Commented Sep 20, 2013 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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