0

What I have:

A basic plugin that's calling a stylesheet contained in the plugin folder:

function my_login_enqueues() {

    // First attempt:
    //wp_enqueue_style( 'custom-login', 'style-login.css' );

    // Second attempt:
    $plugin_stylesheet = plugins_url( 'style-login.css', __FILE__ );
    wp_enqueue_style( 'custom-login', $plugin_stylesheet );

}
add_action( 'login_enqueue_scripts', 'my_login_enqueues' );

My problem:

I'm receiving the following heading error at wp-login.php:

Warning: Cannot modify header information - headers already sent by (output started at [...]) in [...]/wp-content/plugins/members-only/members-only.php on line 398

Note: The Members Only deals with wp-login.php.

My question:

How can I apply a style-sheet to wp-login.php using a plugin?

Note: I'm knowingly not using functions.php on a per theme basis.

2

You can do this two way.But I prefer to go for the second way.You need to put this code in your plugins file.

First way:

function my_loginlcustomization() {
  echo '<style type="text/css">
    h1 a {
     background-image: url(' . plugin_dir_url( __FILE__ ).'/login/logo.png) !important;
    }
  </style>';
}
add_action('login_head', 'my_loginlcustomization');

Second Way(I prefer):

function my_loginlcustomization() {

wp_register_style('custom_loginstyle', plugins_url('/css/login.css', __FILE__));
wp_enqueue_style("custom_loginstyle");
}
add_action('login_head', 'my_loginlcustomization'); 

Thanks

  • Is it important to use wp_register_style? The style seems to be applied without it. – Clarus Dignus Nov 27 '14 at 16:14
  • When registered, it's added to the global dependencies array. You can then enqueue/load it to the DOM when- and where ever you need it. This is actually good practice to not load it on every page/request. But you will have to split up the process and put the restrictions between both functions. – kaiser Nov 27 '14 at 17:24
  • 1
    @ClarusDignus wp_register_style is not mandatory but it is the best practice if you want to maintain.What it does is that first is REGISTER the stylesheet under this name and it IMPLEMENTS the stylesheet.So there will be no overriding issue under this DEFINED name – Rocker Maruf Nov 27 '14 at 17:38
  • 1
    To further clarify the best practices part about registering styles/scripts, it allows other developers down the road to change the priority your style is loaded, remove it, etc, without actually changing your plugin. – Andrew Bartel Nov 27 '14 at 17:53
0

The code was actually working fine:

function my_login_enqueues() {
    wp_enqueue_style( 'custom-login', plugins_url( '/css/style-login.css', __FILE__ ) );
}
add_action( 'login_enqueue_scripts', 'my_login_enqueues' );

The header error was cause by an ouput I had placed outside of and before my function:

echo '<script language="javascript">';
echo 'alert("Plugin is active!")';
echo '</script>';

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.