1

everytime I analyze free wordpress.org theme, I am not able to find how the css file is linked in the theme. as far as i learned css file should be linked in header.php using link tag. but everytime I check themes I didn't see any code in which they link their css through link tag. check example of this theme header.php https://imgur.com/a/EwSe7. my question is how they are linking their file, if its not showing in header.php?

1

The correct way to register styles in Wordpress is to enque them through wp_enqueue_style function in your theme's functions.php. You can read and learn how to do it here - wp_enqueue_style

/**
 * 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' );
  • thanks man. if my theme name is abc and my stylesheet is style.cc then i which one is right. 1: wp_enqueue_style( 'style', get_stylesheet_uri() ); OR wp_enqueue_style( 'abc-stylesheet', get_stylesheet_uri() ); – TheDose Dec 6 '17 at 8:04
  • both can be used but I suggest always to use the theme's prefix abc- – Narek Zakarian Dec 6 '17 at 8:10
1

Wordpress uses a system of filters and actions to do specific things at specific times. One of these actions is wp_enqueue_scripts , which is called within the wp_head() functions (which you definitely should find within the header.php).

You can add a function to this action by using the command add_action. Wordpress also has a function to add stylesheets named wp_enqueue_style . This is to ensure that the same stylesheet or javascript is not linked many times within the same document.

So, if you look into your themes functions.php, you will most definitely find something like this:

function include_my_funky_styles() {
    wp_enqueue_style( 'my-style-name', get_stylesheet_uri() );
// this function says "put the stylesheet into the header"
}
add_action( 'wp_enqueue_scripts', 'include_my_funky_styles' );
//this function says "when doing the action enqueue_scripts within the header, also do this

Happy Coding, Kuchenundkakao

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.