load css and js files in wordpress core

Viewed 222

I want to include bootstrap inside my WordPress installation. I know how to include scripts and styles to a theme, but this isn't what I want.

I've looked to the WordPress core files and I've tried to add the bootstrap files to the script-loader.php file but they will not be loaded. Is there any way to add these files to the WordPress core loaded styles and scripts?

for example, I've added this line to the default wordpress stylesheet/script loader function:

$styles->add( 'bootstrap', "/includes/css/bootstrap$suffix.css" );
$scripts->add( 'bootstrap', "/includes/js/bootstrap$suffix.js", array( 'jquery' ) );

but this will not work.

1 Answers

I would do this by altering your functions.php in your current theme. There are a 2 functions you should know about. The first one is to add CSS.

wp_enqueue_style( $handle, $src, $deps, $ver, $media );

Documentation

The second one is to add JS

wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer);

Documentation

Wordpress has a basic documentation on how to do it both:

Documentation

Eventually you going to have something like this:

function add_theme_scripts() {
  wp_enqueue_style( 'style', get_stylesheet_uri() );

  wp_enqueue_style( 'slider', get_template_directory_uri() . '/css/slider.css', array(), '1.1', 'all');

  wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);

    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
      wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );

Good luck!

Related