Forums

[resolved] wp_register_style() not working for me (5 posts)

  1. spstieng
    Member
    Posted 7 months ago #

    I'm building a new plugin and I need to register a css file.
    Looking at Lester 'GaMerZ' Chan's work, he is using the function called wp_register_style().

    add_action('wp_head', 'email_js');
    function email_js() {
      wp_register_style('wp-email', plugins_url('wp-email/email-css.css'), false, '2.40', 'all');
    }

    This works great for his plugin, but why doesn't it work for me?
    Is it because it's not a wp function?

    Here is my code:

    add_action('init', 'sl_add_scripts');
    
    function sl_add_scripts() {
      wp_register_style('sl_forms', plugins_url('wp-store-locator/style/sl_forms.css'), false, '1.00', 'all');
    }
  2. tomontoast
    Member
    Posted 7 months ago #

    How about using the following code

    <?php
    
        /*
         * This example will work with WordPress 2.7
         */
    
        /*
         * register with hook 'wp_print_styles'
         */
        add_action('wp_print_styles', 'add_my_stylesheet');
    
        /*
         * Enqueue style-file, if it exists.
         */
    
        function add_my_stylesheet() {
            $myStyleFile = WP_PLUGIN_URL . '/style/sl_forms.css';
            if ( file_exists($myStyleFile) ) {
                wp_register_style('myStyleSheets', $myStyleFile);
                wp_enqueue_style( 'myStyleSheets');
            }
        }
    
    ?>

    See http://codex.wordpress.org/Function_Reference/wp_enqueue_style for more details.

  3. spstieng
    Member
    Posted 7 months ago #

    What does it actually mean to enqueue something?
    If a script is enqueued, it's kind of waiting in line to be used?

  4. iamduyu
    Member
    Posted 7 months ago #

    means it will be outputed to the browser in a right time not now.
    e.g: after http header, before <body> element.

  5. spstieng
    Member
    Posted 6 months ago #

    Thanks guys.

    I had done what tomontoast suggested, except for one thing.
    I had forgottenthe wp_enqueue_style.

    Once i Added this, it worked.

    Thanks guys :)

Reply

You must log in to post.

About this Topic