I am trying to add a CSS style to a WordPress theme that I am developing as is shown in this tutorial: http://www.wpbeginner.com/wp-tutorials/how-to-properly-add-javascripts-and-styles-in-wordpress/ but seems don’t work and I cant’ understand why.
So this is the head.php file of my personal theme:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<title><?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> » Blog Archive <?php } ?> <?php wp_title(); ?></title>
<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" /> <!-- leave this for stats -->
<!--
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
-->
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<!-- <?php bloginfo('stylesheet_directory'); ?>/ -->
<script language="javascript" type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js.js"></script>
<!--
<script language="javascript" type="text/javascript">
if(f)
{
write_css('<?php bloginfo('stylesheet_directory'); ?>');
}
</script>
-->
<?php wp_head(); ?>
</head>
<body>
<center>
<div id="page">
<div id="header">
<h1><a href="<?php echo get_settings('home'); ?>"><?php bloginfo('name'); ?></a></h1>
<div class="description"><?php bloginfo('description'); ?></div>
</div>
<hr />
And in my functions.php file I have put the following code:
<?php
function wpb_adding_styles() {
wp_register_script('my_stylesheet', plugins_url('style.css', __FILE__));
wp_enqueue_script('my_stylesheet');
}
add_action('wp_enqueue_scripts', 'wpb_adding_styles');
?>
But the file style.css is not loaded. Why? What am I missing?
In particular the thing that I can’t understand in the previous tutorial is how and hwere the wpb_adding_styles() is called because in the header.php file it is never called.
Someone can help me?
Tnx
Andrea
You are using
plugins_url
– therefore pointing to completly different directory than your current theme. If you are just trying to enqueue a theme style do it like this:the
wp_enqueue_style
takes some parameters. In the example above I am using:array()
of dependencies (in this case empty) – for example if we had two styles enqueued we could set in the second onearray('main-css')
and WP would then WAITED until the the style with the ID of main-css style is loaded before loading it.PS. No need to register it first. It is just as safe as registering and then calling it via enqueue. If you want to register your scripts, but not directly load them in your pages, you can register the files once, and then load them when you need them just by calling
wp_enqueue_script( $handle )
(just pass the $handle and the rest will be taken from thewp_register_script()
). So if you want to just load it (which is 99% of times) you can just omit it.Refference:
As for your header.php file this part:
should be deleted and any script should be added to the WP just the same way you are adding styles. For example:
Same thing goes for other elements like for example google fonts etc. Use
wp_enqueue_scripts
to add them as well:Where I have ADD_YOUR_DESIRED_FONT_FAMILIES_HERE you should add font families from Google font. If you want multiple just divide them with
|
. For example if I want Roboto font and PT Serif font I would write it like thisRoboto:400,700,300|PT+Serif:400,700,400italic
When registering and enqueueing CSS files you need to use
wp_register_style()
andwp_enqueue_style()
.If this code is in your theme’s functions.php file you want to use
get_template_directory_uri()
when referencing the path of your custom CSS files.Your code is very close, but should be corrected to the following: