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 How-Tos

  • angularjs in wordpress
  • Convert wordpress page to posts and vice versa
  • Find all user created wordpress custom field keys
  • How to automate wordpress sandbox setup on Linux
  • How to capture php code or included file output in a variable
  • How to check the performance of a plugin using mysql query log
  • How to migrate wordpress from root to sub directory
  • How to upgrade wordpress manually
  • How to view wordpress current version
  • Setup xdebug for remote wordpress debugging
  • Wordpress - disable theme and plugin editing
  • Wordpress - get wpdb class method names
  • Wordpress multisite network vs normal installation
  • display method names from php WP_Query object
  • find if a wordpress page is leaf page
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress How To

How to include angularjs javascript in wordpress post

By admin | Last updated on Apr 12, 2016

AngularJS is great framework for declaring dynamic views in HTML documents. To include AngularJS we just need to include the angularjs javascript file in the wordpress. These are two possible ways to include AngularJS in wordpress post:

  1. We can just include the AngularJS javascript (angular.js) in post or page content itself.
  2. angular.js can be included in page header or footer using a plugin. We can use a custom variable to decide if a specific post needs to have it or not.

Using first approach is pretty trivial. The second approach is little more cleaner. I’ll cover 2nd approach in this article.

WordPress plugin to include AngularJS

Here are the steps to create and activate the plugin to include AngularJS in wordpress:

  1. Create a new plugin wp-angularjs-control-include with wp-angularjs-control-include.php file and js folder containing angular.js files. The plugin enqueues angular.js scripts based on custom variable wp-angularjs-include (or any other variable you prefer). Here is how the code looks like for this:
    <?php
    function waci_enqueue_js () {
      global $post;
      $post_angularjs_include_value = get_post_meta($post->ID, 'wp-angularjs-include', true);
      if (in_array($post_angularjs_include_value, array('true', '1')) ) {
        wp_enqueue_script('angularjs-js', plugins_url('js/angular.js', __FILE__), null, null, true/*in_footer*/);
      }
    }
    add_action('wp_enqueue_scripts', 'waci_enqueue_js');
    ?>
    
    Note that this will include angular.js in the html page footer. Including js in footer is a recommended practice from rendering perspective.
  2. Install and activate the plugin.
  3. Set wp-angularjs-include to true for the desired wordpress post as shown below:
    wordpress-angularjs-include-custom-field

Sample AngularJS app

To create a sample wordpress app include the following code in your post:

<div ng-app>
  <label>Name:</label>
  <input type="text" ng-model="yourName" ng-init="yourName='Foo'">
  <hr>
  <b>Hello {{yourName}}!</b>
<div/>
Note the ng-app in div tag and ng-model="yourName" in input tag. This should be outcome of the above code:
wordpress-angularjs-include-example

Few points to note

  1. AngularJS can be placed anywhere in the document. It only applies to relevant part of the HTML document once the document is ready as per the following code in angular.js:
    jqLite(document).ready(function() {
      angularInit(document, bootstrap);
    });
  2. AngularJS may have some impact on SEO as some content is replaced dynamically on the page. Hence it seems more suitable dynamic apps.

Suggested posts:

  1. How to use html pre tag to display code
  2. How to add tag and category to wordpress pages
  3. How to create Linux instance on Amazon AWS/EC2 Classic
  4. AngularJS – display html without escaping using ng-bind-html
  5. How to show wordpress pages on front page with skip_home custom field
  6. How to use Google custom search with lazy loading approach
  7. Get file size in bytes on Linux
  8. How to use google custom search for wordpress site
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged AngularJS, Javascript, Tutorials, Web Development, Wordpress, Wordpress How To
  • 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