'add_action() function in wordpress not working [duplicate]
I am new in Wordpress and I am writing this code in my functions.php but it isn't working. This is my code:
<?php
function myFunction(){
wp_enqueue_style('style',get_stylesheet_uri());
}
add_action('wp_enqueue_scripts','myFunction');
This function must add my style.css in my page but is not working.
Solution 1:[1]
Add in header file
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
Solution 2:[2]
Please see all the parameters of wp_enqueue_scripts
Try below code :
/**
* Proper way to enqueue scripts and styles
*/
function myFunction() {
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', 'myFunction' );
Solution 3:[3]
Try with this:
add_action( 'after_setup_theme', 'yourtheme_theme_setup' );
if ( ! function_exists( 'yourtheme_theme_setup' ) ) {
function yourtheme_theme_setup() {
add_action( 'wp_enqueue_scripts', 'yourtheme_scripts' );
}
}
if ( ! function_exists( 'yourtheme_scripts' ) ) {
function yourtheme_scripts() {
wp_enqueue_style( 'main_style', get_stylesheet_uri(), array() );
}
}
Here the wp_enqueue_scripts action gets called inside the after_setup_theme hook.
https://developer.wordpress.org/reference/functions/wp_enqueue_style/
Solution 4:[4]
just you should use the wp_head() in the <head> tag for applying styles
<!DOCTYPE html>
<html>
<head>
<?php wp_head(); ?>
</head>
<body>
...
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Meldin Xavier |
| Solution 2 | Manthan Dave |
| Solution 3 | dingo_d |
| Solution 4 | Ramin eghbalian |
