InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress customization tutorials

  • Handle pagination in wordpress custom php code
  • How to and why disable wordpress cron
  • How to customize wordpress image alt tag
  • How to display date in wordpress pages
  • How to display wordpress page list with specific custom field value
  • How to display wordpress top level pages
  • How to hide a post from home and RSS feed in wordpress
  • How to include bootstrap javascript and css in wordpress post
  • How to include wordpress pages in archives
  • How to override priority and change frequency in Yoast xml sitemap
  • How to remove xmlrpc from wordpress headers
  • How to setup wordpress custom query params with pretty url format
  • How to show wordpress pages on front page with skip_home custom field
  • How to use google custom search for wordpress site
  • How to write custom php in wordpress
  • No frills social share links for Wordpress sites
  • Using Wordpress custom field for displaying code
  • Wordpress - add content filter after shortcode execution
  • Wordpress - customize category and tag links
  • Wordpress - customize facebook plugin opengraph meta tags
  • Wordpress - customize posts per page for tag, category and date archive pages
  • Wordpress - exclude specific posts from archive pages
  • Wordpress - get posts/pages with missing meta key
  • Wordpress - how to add filter to description meta tag
  • Wordpress - how to create custom tag cloud
  • Wordpress - how to exclude specific tag posts from a tag archive page
  • Wordpress - write custom php log to separate file
  • Wordpress how to check if a post is being viewed by admin
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress customization

How to include bootstrap javascript and css in wordpress post

By admin | Last updated on Mar 20, 2016

Bootstrap is pretty handy javascript and css library/layer over jquery which provides many useful pre-built functionalities like responsive design, transitions, modals, buttons, etc. In case you want to include twitter bootstrap in a wordpress blog, you may want to include minimum these files:

  1. bootstrap.css
  2. bootstrap-responsive.css
  3. bootstrap.js (or alternatively you can include specific components like bootstrap-button.js, etc.)

To use bootstrap in wordpress for only specific posts (to keep html of other posts light), one approach is to use a custom variable and enqueue bootstrap css and js if that custom variable is set. Here are the steps to achieve this:

  1. Create a new plugin wp-bootstrap-control-include with wp-bootstrap-control-include.php file and js and css folders containing bootstrap files. The plugin enqueues appropriate scripts and css based on custom variable wp-bootstrap-include (or any other variable you prefer). Here is how the code looks like for this:
    <?php
    function wbci_enqueue_js_css () {
      global $post;
      $post_bootstrap_include_value = get_post_meta($post->ID, 'wp-bootstrap-include', true);
      if (in_array($post_bootstrap_include_value, array('true', 'on', 'yes')) ) {
        wp_register_style( 'css/bootstrap-css', plugins_url('bootstrap.css', __FILE__) );
        wp_register_style( 'css/bootstrap-responsive-css', plugins_url('bootstrap-responsive.css', __FILE__) );
        wp_enqueue_style('bootstrap-css');
        wp_enqueue_style('bootstrap-responsive-css');
    
        wp_enqueue_script('bootstrap-js', plugins_url('js/bootstrap.js', __FILE__), array('jquery'), null, true);
      }
    }
    add_action('wp_enqueue_scripts', 'wbci_enqueue_js_css');
    ?>
    
    Note that we have included jquery as dependency for bootstrap.js which makes sure that jquery get included before bootstrap.js.
  2. Install the plugin and activate it.
  3. Set wp-bootstrap-include to true for the desired wordpress post as shown below:
    wordpress-bootstrap-include-custom-field
  4. To test include the following button toggle code (or any other bootstrap code) in a post which has wp-bootstrap-include set to true.
    <button type="button" class="btn btn-primary" data-toggle="button">Single Toggle</button>
    
    Here is the outcome for the above code:

    1. Button (normal state)
      bootstrap-single-toggle-button-unpressed
    2. Button (pressed state)
      bootstrap-single-toggle-button-pressed

Few points to note

  1. I have not included bootstrap icons in this article. But you can easily add support for those by including icon files in img directory in the wordpress plugin.
  2. You can also access a working version of the wp-bootstrap-control-include plugin on github. The plugin has bare minimum functionalities and you may want to add more stuff to it.

Suggested posts:

  1. How to use html pre tag to display code
  2. How to add tag and category to wordpress pages
  3. ReactJS – convert jsx to javascript using babel cli
  4. How to setup wordpress custom query params with pretty url format
  5. How to use google custom search for wordpress site
  6. Vim – how to go back to last edited line/context
  7. Curl – follow redirects
  8. How to show wordpress pages on front page with skip_home custom field
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bootstrap, CSS, Javascript, Tutorials, Wordpress, Wordpress customization
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress