I'm trying to migrate my HTML to WordPress template. While I'm doing that, I want to keep my CSS files where they are placed and import them to the code. I found out that I can use functions.php file to make it easier.
Below is the code that I added to functions.php:
<?php
function add_theme_codes() {
wp_enqueue_style(‘bootstrap’,get_stylesheet_directory_uri().’/css/bootstrap_grid.css’, ‘all’);
wp_enqueue_style( ‘style’, get_stylesheet_directory_uri().’/css/main.css’, ‘all’);
}
add_action( ‘wp_enqueue_scripts’, ‘add_theme_codes’ );
?>
But still my WordPress site is not importing the CSS files.
Still you have selected the answer I would like to answer on your last desire that you want this to work in a proper way that WordPress do. Please check wp_footer is called before body tag closed or not. Action Hook wp_enqueue_scripts will called in wp_footer function. Please try once and let me know the result.
You can get css file from below code:
<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style') );
}
?>
Or in Header section before wp_head() add below code
<link href="<?php echo get_bloginfo('stylesheet_directory');?>/css/bootstrap_grid.css" rel="stylesheet" />
<link href="<?php echo get_bloginfo('stylesheet_directory');?>/css/main.css" rel="stylesheet" />
Wrap with add_action and this might work
<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_register_style('css-ui',get_template_directory_uri().'/assets/css/style.css' );
wp_enqueue_style('css-ui' ); }
Related
I'm completely new to Codex and PHP, just started learning it two days ago.
I am converting my html and css over to Wordpress.
I have two style sheets, one for my static front-page and the main stylesheet for the rest of the pages. I'm completely going overboard in my head. I'm trying to figure out how to make a if and else statement.
So if the front page is loaded, open this style sheet. If not, load the main stylesheet.
This is what I've tried so far:
function register_more_stylesheets() {
wp_enqueue_style( 'style', get_stylesheet_directory_uri() );
}
function add_my_stylesheet() {
if ( is_front_page() )
wp_register_style( 'homestyle', get_template_directory_uri() . '/homestyle.css' );
}
add_action( 'init', 'register_more_stylesheets' );
add_action( 'wp_enqueue_scripts', 'add_my_stylesheet' );
Another thing I'm getting messed up with is my link rel to all this..
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">
<link rel="stylesheet" type="text/css" href="<?php bloginfo("template_directory"); ?>/homestyle.css" />
This is all in the header.php, I'm linking both, but both seem to be kind of colliding both my stylesheets.
Following basic code convention from Codex WordPress or default Themes you can easily handle this problem.
Usage
wp_enqueue_style( $handle, $src, $deps, $ver, $media );
Using Hook
/**
* Proper way to enqueue scripts and styles
*/
function wpdocs_theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );
Use Something like this:
<?php if(is_home()) { ?>
<link rel="stylesheet" type="text/css" href="<?php bloginfo("template_directory"); ?>/homestyle.css" />
<?php } else { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">
<?php } ?>
Put below code to your functions.php file:
add_action( 'wp_enqueue_scripts', 'register_plugin_styles' );
function register_plugin_styles() {
wp_register_style( 'single-style', get_stylesheet_directory_uri() );
wp_register_style( 'home-style', get_template_directory_uri() . '/homestyle.css' );
if( is_single() )
wp_enqueue_style( 'single-style' );
else
wp_enqueue_style( 'home-style' );
}
I have tried using the following code to embed Bootstrap to Wordpress but it doesn't work. Need help..........
<?php
function resources() {
wp_enqueue_style('style',get_stylesheet_uri());
wp_enqueue_style('bootstrap.min',get_template_directory_uri().'/css/bootstrap.min.css');
wp_enqueue_style('bootstrap',get_template_directory_uri().'/css/bootstrap.css');
wp_enqueue_style('bootstrap-theme.min',get_template_directory_uri().'/css/bootstrap-theme.min.css');
}
add_action('wp_enqueue_scripts', 'resources');
This may be helpful to you
WordPress is to use wp_enqueue_style and wp_enqueue_script from within functions.php. In particular, we need to create a new function that adds (or enqueues) the css style and the js script and then allow WordPress to call it at the right moment by adding a wp_enqueue_scripts action.
/* Add bootstrap support to the Wordpress theme*/
function theme_add_bootstrap() {
wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/css/bootstrap.min.css' );
wp_enqueue_style( 'style-css', get_template_directory_uri() . '/style.css' );
wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . '/js/bootstrap.min.js', array(), '3.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_add_bootstrap' );
For Reference: click here
In order towp_enqueue_scripts in function.php work properly, <?php wp_head(); ?> needs to be placed before the closing </head> and <?php wp_footer(); ?> before closing </body> tag in your template file.
Because you didn't post your template file, so I think this can be a reason causes your problem.
Hope this help
Just use the bootstrap CDN - https://www.bootstrapcdn.com/
In your child theme find the function theme_enqueue_styles() and add the two lines of extra code shown below.
function theme_enqueue_styles() {
// Add the following two lines //
wp_enqueue_style( 'bootstrap-cdn-css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );
wp_enqueue_script( 'bootstrap-cdn-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' );
// ------ -------//
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style') );
}
Make sure your file location of downloaded bootstrap files. and put this code into functions.php
1.stylesheet CSS file
2.script js file
<?php
function load_stylesheets()
{
// enqueue parent styles
wp_enqueue_style('bootstrap', get_stylesheet_directory_uri().' /css/bootstrap.min.css');
wp_enqueue_script('bootstrap', get_stylesheet_directory_uri().' /js/bootstrap.min.js');
}
add_action('wp_enqueue_scripts','load_stylesheets');
?>
I've been following a tutorial online thats taught me how to enqueue stylesheets for Wordpress with the functions folder instead of the headerusing the following code:
function lucieaverillphotography_scripts() {
wp_enqueue_style( 'lucieaverillphotography_style', get_stylesheet_uri() ); }
Followed by:
add_action( 'wp_enqueue_scripts', 'lucieaverillphotography_scripts' );
This has worked, but when I tried to enqueue a separate stylesheet for a page template, it won't work. Here is the code I used, I put it between the two above:
if (is_page_template('page-templates/full-page.php')) {
wp_enqueue_style( 'lucieaverillphotography-full-page' ,
get_template_directory_uri() . '/css/full-page.css');
}
else { wp_enqueue_style( 'lucieaverillphotography_style',
get_template_directory_uri() . '/style.css'); }
I have a feeling that it's something to do with the last part: '/style.css' ... but the only way I can get my theme to recognise the new stylesheet is by adding: '/css/full-page.css' in its place and then it uses the new stylesheet for every page rather than the page template.
If I change it by adding the name of the folder that contains style.css - i.e '/files/style.css' , I lose all my css altogether, which is puzzling as I thought this was the most obvious thing to try.
Try This in functions.php
function lucieaverillphotography_scripts() {
if (is_page_template('page-templates/full-page.php')) {
wp_enqueue_style( 'lucieaverillphotography-full-page' , get_template_directory_uri() . '/css/full-page.css');
}
else{
wp_enqueue_style( 'lucieaverillphotography_style', get_stylesheet_uri() );
}
}
add_action( 'wp_enqueue_scripts', 'lucieaverillphotography_scripts' );
I've started building a theme for Wordpress. Add a certain point I want to add some style to the site. I can get it to work when adding the style to the header, but I want to use the function.php file to accomplish this. This is what I got:
<?php
function addCss(){
wp_enqueue_style('style', get_template_directory_uri() . 'style.css');
}
add_action("wp_enqueue_scripts", "addCss");
?>
It's a very simple function, but for some reason, the function is not fired. I also don't get any errors and style.css is not added to the site according to the console.
My file structure:
I've tried searching for a solution, but couldn't find any. I hope u guys can help me.
Make sure you call <?php wp_head(); ?> in header.php just before ending </head> tag.
function addMyStyle() {
wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css', false, '1.0', 'all' );
}
add_action('wp_head', 'addMyStyle');
get_template_directory_uri() return a path without the "/" at the end.
Add a "/" in your path:
// add this line to create a versionning of your stylesheet
$ver = time();
wp_enqueue_style('style', get_template_directory_uri() . '/style.css', $ver);
I hope that help you !
You need to have wp_head() in header.php
if ( ! function_exists( 'themescripts' ) ) {
function themescripts() {
wp_enqueue_style('style', get_stylesheet_uri(), array());
wp_enqueue_script( 'jquery', array(), '', true);
....
}
add_action( 'wp_enqueue_scripts', 'themescripts' );
}
Also, make sure to have wp_footer() in footer.php file.
I get some issue when putting .css file into the <head>
The function which I use works great but files added after all the <script> tag and I would like to add them before.
function custom_styles() {
echo"
<link rel='stylesheet' href='/css/custom.css' type='text/css' media='all'>
<link rel='stylesheet' href='/css/screen.css' type='text/css' media='all'>
";
}
add_action( 'wp_head', 'custom_styles' 10 );
Any help would be appreciated
It is because wp_head actions defined by user is triggered after the ones defined by wordpress, so your content will be added at the end of <head> tag. You can change the priority of action to value below default ( 10 ) , add_action( 'wp_head', 'custom_styles', 1 );, or you can use more appropriate action wp_enqueue_scripts and method wp_enqueue_style.
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_style( 'style-name', get_stylesheet_directory_uri() . '/css/custom.css' );
});
Try below code
function theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
// add js file in html header
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
see more example