How to include styles.css in wordpress theme?
Asked Answered
A

4

5

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?

Acrefoot answered 23/5, 2016 at 12:45 Comment(1)
codex.wordpress.org/Plugin_API/Action_Reference/wp_head You need <?php wp_head();?> in your header.phpAdz
Z
18

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' );
Zippy answered 23/5, 2016 at 12:53 Comment(2)
Awesome, sorry, noobish question to come: when you say into the websites frontend, does that mean also in the functions.php file?Acrefoot
Method 1 code you have to insert into header.php file of your active theme. Method 2 and Method 3 code can be inserted into active theme functions.php fileZippy
F
1

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' );
Frog answered 23/5, 2016 at 13:6 Comment(0)
B
1

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' );
Barra answered 27/4, 2021 at 1:50 Comment(0)
D
0

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.

Delightful answered 28/2 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.