• Hey there,

    I just spent hours trying to register/enqueue a css file without success…
    I just followed the codex example…

    I’m sure that :

    – It’s a custom theme, not a child theme
    – The demo.css file exists (root of the theme folder)

    Here is my code :

    <?php

    add_action( ‘wp_enqueue_scripts’, ‘my_css’ );

    function my_css() {
    echo DebugSomething;
    wp_register_script(‘my_stylesheet’, get_template_directory_uri() . “demo.css”);
    wp_enqueue_script(‘my_stylesheet’);

    }

    ?>

    I’ve tried echoing something in my_css(), nothing happens.
    If i replace

    – add_action( ‘wp_enqueue_scripts’, ‘my_css’ );
    by
    add_action( ‘init’, ‘my_css’ );

    The echo works fine… But the css file still wont load…

    Am i missing something ?

    Thanks A LOT for you help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • you need to use “wp_enqueue_style”, you are loading style and not script ??

    Thread Starter bigboybifdick

    (@bigboybifdick)

    Hey,

    Thanks for answering. Sorry i was a bit confused, i tried a lot of things (desperado mooode) and i paste the bad code.

    It still wont work like this :

    // Here i can use _scripts, i read it was the same
    add_action( 'wp_enqueue_scripts', 'my_css_method' );
    
    function my_css_method() {
    // i corrected and replaced _script by _style
    wp_enqueue_style('my_stylesheet_unique_name',get_template_directory_uri() . "/demo.css", __FILE__);
    
    }

    The weird thing tough, is that the css is loaded in the admin if i replace

    add_action( 'wp_enqueue_scripts', 'my_css_method' );
    //by
    add_action( 'admin_enqueue_scripts', 'my_css_method' );

    So wp_enqueue_scripts is not working… Any idea why ?

    Thanks a lot,

    just put this code

    function my_css(){
    
    	wp_enqueue_style('test-style',get_template_directory_uri().'/test.css');
    
    }
    
    add_action( 'wp_enqueue_scripts','my_css' );

    and about your question, “wp_enqueue_scripts” only loads on front and “admin_enqueue_scripts” loads in backend

    Thread Starter bigboybifdick

    (@bigboybifdick)

    Hey,

    Thanks again man, but im’ not sure you’re getting what’s my problem here.
    The code you sent me is exactly the same i already have :

    add_action( 'wp_enqueue_scripts', 'my_css_method' );
    
    function my_css_method() {
    	wp_enqueue_style('test-style',get_template_directory_uri().'/demo.css');
    
    }

    I know that “wp_enqueue_scripts” only loads on front and “admin_enqueue_scripts” loads in backend ??

    My problem is that the css file loads fine when i use admin_enqueue (on the admin side of course), but it does not load on the front when i use wp_enqueue. My css file (demo.css) contains a {background-color:red} so i can know if it works or not, and it doesn’t in the front.

    So :

    The admin background is red if i use admin_enqueue (and i can see the file on the source code), but it’s not on the fnort side if i use wp_enqueue (and i don’t the file in the source).

    I also added an echo “test”, on both of my tests (front and admin enqueue), i can see it in the admin, not in the front.

    So it seems that the wp_enqueue_scripts doesn’t work for me… :/

    Here is my full functions.php file :

    <?php
    ////
    // SOME MENU STUFF
    ////
    add_action( 'init', 'register_my_menus' );
    function register_my_menus() {
      register_nav_menus(
        array(
          'header-menu' => __( 'Header Menu' ),
          'extra-menu' => __( 'Extra Menu' )
        )
      );
    }
    
    function add_menuclass($ulclass) {
       return preg_replace('/<a /', '<a class="pageload-link"', $ulclass);
    }
    add_filter('wp_nav_menu','add_menuclass');
    
    ////
    // ENQUEUE SCRIPTS PART
    ////
    add_action( 'wp_enqueue_scripts', 'my_css_method' );
    
    function my_css_method() {
    	wp_enqueue_style('test-style',get_template_directory_uri().'/demo.css');
    
    }
    
    ?>

    Could anyone help me ?

    Thanks a lot,

    Thread Starter bigboybifdick

    (@bigboybifdick)

    add_action('wp_enqueue_scripts', 'my-css');

    doesn’t fire the function at all as i can’t even see my echo “Test”;

    :/

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Enqueue and Register CSS -> Not Working’ is closed to new replies.