I am in the process of converting a mobile responsive page, currently built in Bootstrap into WordPress.
Using wp_register_style()
and wp_enqueue_style()
will add the stylesheets to all pages, including the wp-admin. I know I could get around that by using an if ( ! is_admin() )
conditional statement.
Is it better to use the conditional statement, or to just to use:
<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/bootstrap.css" media="screen" />
in the theme’s header.php?
To load scrpits only in front-end pages use:
To load scripts only in login use:
To load scripts only in admin use:
You should always use
wp_enqueue_style()
andwp_enqueue_script()
where possible, unless it it a special case such as the HTML5 Shiv script which requires IE conditional comments.If you use the proper hooks to enqueue your style, it should only be loaded on the front-end. The correct hook to use in your instance would be
wp_enqueue_scripts
, which works for both scripts and styles on the front-end only.If you want to load styles or scripts on the login page or the admin area, use
login_enqueue_scripts
oradmin_enqueue_scripts
, respectively.I recommend that you read the Codex pages for the functions
wp_enqueue_style()
andwp_enqueue_script()
, as well as the pages for the action hookswp_enqueue_scripts
,login_enqueue_scripts
, andadmin_enqueue_scripts
.Just a note on
login_enqueue_scripts
: the scripts and styles enqueued on this hook will render in the source of your page just before</body>
(as of WordPress 3.6.1). See a disscution about style links in body here – https://stackoverflow.com/questions/4957446/load-external-css-file-in-body-tagIf you need to put your scripts and styles for the login page on the
<head>
you’d be better off usinglogin_head
hook and echo-ing your links: