Peter Turner
Peter Turner

Reputation: 355

How to include styles.css in wordpress theme?

I am trying to include my styles.css stylesheet in a wordpress theme I am trying to develop (my first one). Question: How would one go about including it? I know putting the following code in my header.php file works:

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">

however I would rather like to include it through functions.php like so:

<?php
function firstTheme_style(){
    wp_enqueue_style( 'core', 'style.css', false ); 
}
add_action('wp_enqueue_scripts', 'firstTheme_style');
?>

yet this doew not work at all (When i remove the line from my header.phps 'head', my styles are not seen?

Upvotes: 5

Views: 19652

Answers (4)

Alexandra
Alexandra

Reputation: 1136

Just adding some details, maybe it will be helpful for someone. If you are going to include style.css in functions.php of existing theme, check if it uses namespaces. For example, in the Raft theme, that I have to edit, there is a line on the top of functions.php:

namespace Raft;

In this case code

add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_style' );

will generate error "Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback". Define the namespace for callback:

add_action( 'wp_enqueue_scripts', 'NamespaceValue\mytheme_enqueue_style' );

and it works.

Upvotes: 0

Dezefy
Dezefy

Reputation: 2096

The best way to include your styles.css file is to add the following code in your themes functions.php:

function themename_enqueue_style() {
    wp_enqueue_style( 'themename-style', get_stylesheet_uri() ); 
}
add_action( 'wp_enqueue_scripts', 'themename_enqueue_style' );

Upvotes: 1

user15384820
user15384820

Reputation:

the best practice is create a custom function in functions.php

function custom_function(){}

after that try to use this wordpress functions wp_enqueue_style to invoke the css files safety

function custom_function(){
wp_enqueue_style ('any-name',get_template_directory_uri().'/css/style.css');}

and add add_action function

add_action( 'wp_enqueue_scripts', 'custom_function' );

Upvotes: 1

Purvik Dhorajiya
Purvik Dhorajiya

Reputation: 4870

Following method are include style.css.

Method - 1

// add in your header.php
<link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>">

Method - 2

// load css into the website's front-end
function mytheme_enqueue_style() {
    wp_enqueue_style( 'mytheme-style', get_stylesheet_uri() ); 
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_style' );

Method - 3

// Add this code in your functions.php
function add_stylesheet_to_head() {
      echo "<link href='".get_stylesheet_uri()."' rel='stylesheet' type='text/css'>";
}

add_action( 'wp_head', 'add_stylesheet_to_head' );

Upvotes: 19

Related Questions