How to Create a Headless WordPress Site with Angular

Introduction

According to W3Techs, WordPress is the most widely used content management system in the world. More than 43 % of all websites are developed using WordPress on the internet. WordPress, on the other hand, is a monolithic platform with a closely integrated frontend and backend that makes it unmanageable for developers who want to create digital experiences that are not dependent on the theme-based structure of WordPress.

It’s possible that detaching the front end from the backend will solve your problems if you’ve ever felt locked or stuck when working with WordPress. Developers from a WordPress development company that chooses to go headless will have access to a wealth of new opportunities, including new frameworks, technological stacks, and delivery channels, as a result of their decision.

Here we are going to talk about how to build a headless WordPress website with Angular. Because big enterprises like Upwork, Freelancer, PayPal, Deutsche Bank, Forbes, and Guardian use Angular as their front-end development technology.

What is Headless WordPress?

A headless WordPress website is a site that mainly uses WordPress for content management and uses any other technology to build the front end and display the content to the website visitors.

The headless WordPress CMS, also known as a decoupling CMS, differs greatly from a traditional content management system (CMS) in terms of architecture. The headless CMS architecture separates the front end and back end of the site, splitting it into two virtually autonomous systems. In contrast to the one system that produces, stores, and manages information, the other system exposes data to end-users through graphical user interfaces (UIs) such as a website, smartphone application, or Internet of Things device. When developing a headless When developing a headless WordPress website, skilled WordPress developers use a REST API to communicate between the backend and the front end.

Rest API allows developers to interact with cross-technology interfaces as long as both speak the same language of JSON. WordPress Rest API spits out data in JSON format, which is understandable by a large number of web technologies. JSON is a text-based representation of JavaScript objects which contains data in key-value pairs.

"names": [
     {
       "id": 0,
       "name": "John Doe"
     },
     {
       "id": 1,
       "name": "Richard Lindley"
     },
     {
       "id": 2,
       "name": "Jack Moore"
     }
   ],

Today, I will demonstrate the power of WordPress Rest API by using it with Angular and WordPress as headless CMS for my basic web application.

The Benefits of Using Headless WordPress CMS

Being an open-source platform, WordPress offers great flexibility to create any website. Using WordPress as a headless CMS allows you to create the front-end of your web application using any web technology and manage its content using one of the most popular CMS.

You might come across a situation where you need to add a blog feature to an existing web app built with either Vue.js or React. Instead of coding the full-fledged content management system in the native framework, use headless WordPress CMS to manage the content using Rest API.

The following are some of the advantages of headless WordPress as compared to traditional WordPress, that makes it more effective to use:

  • Flexibility: A Headless CMS allows for greater flexibility in terms of programming language and framework selection, removing many of the constraints associated with the traditional paradigm. Additionally, similar data can be reused across multiple projects with a simple export, which is advantageous in the event of a CMS change.
  • Security: It is less vulnerable and prone to third-party application problems because your material is stored apart from your front-end delivery system.
  • Multichannel: A Headless CMS enables you to manage your content across various channels, providing significantly increased adaptability and a seamless user experience. The material is regarded as “pure” because the REST API transmits the data in JSON format, which could be reused and read by any front end.
  • Optimized site loading time: Because the presentation layers are separated and the logic for presenting data is relocated to the client side, the speed of data transmission is significantly increased.
  • Deeper visual control: Even the most complex WordPress themes or web page templates can’t be changed. If you’re a front-end developer or a content creator, the headless architecture gives you a lot of freedom when it comes to how your content looks on the web.

How to Set Up a WordPress Site with Angular

Let’s begin with setting up the WordPress site, as this will act as a data source for our Front-end Angular application.

How-to-set-up-WordPress-site-with-AngularJS

Rest API will be enabled by default. If you want to restrict the Rest API access then you can use Basic-Auth or Oauth2 authentication method

Go to Settings → Permalinks and select either Post name or Custom Structure.

WordPress-screenshot

Since we are working with API calls, download the chrome extension for Postman. Inside the Postman, enter the URL in the following format:

https://example.com/wp-json/wp/v2/posts

The above URL will fetch the post data inside your WordPress site.

Wp-json-image

Getting Started with Angular

Getting Started with Angular

What is Angular?

Angular is considered a web application development tool that’s always been chosen by web developers and programmers because of its progressive features. Compared to other JavaScript frameworks, Angular offers unique functionalities and advantages. As a result, Angular has become one of the most popular frameworks in the world. With its agile development process, the project is able to save a lot of money as well as time on the development side of the project. The bottom line is, as a business owner, if you are thinking about developing a web app but are confused about how to choose the right framework to use, then AngularJS will be the ideal choice to make.

Setting up Angular App

In this article, we will be building a blog page that will list the post title and content. All the backend content management of the project will be developed with headless WordPress.

To build Angular apps, you need to have the following dependencies installed on your computer.

  • NodeJS
  • Node Package Manager (NPM)
  • Angular CLI
  • Text Editor such as Sublime or Visual Studio Code.
  • Git for version controlling (Optional)

To check whether you have installed Node.js and NPM, Use the following command line to check the version, also check whether you installed Node.js and NPM properly or not. 

node -v

npm -v

Install Angular through CLI

Once you completed the installation of (Node Package Manager) NPM, you can install Angular CLI in the following steps. Open your terminal or command prompt in your system and enter the below command.

npm install -g @angular/cli

The above command installs the Angular globally in your system, g stands for global installation.

Check the Angular CLI version

Once the installation is completed, use the following command to check whether the Angular application installation is completed successfully or not. Also, check the current Angular version CLI.

ng v

Create a New Angular App with the CLI

After setting up the environment, open the command line and run the following code to create the project

ng new blog

Where the blog is the project name. Once the project is created, run the following command and you will be able to see the angular app project output on http://localhost:4200

ng serve

Lastly, open your angular codebase with any code editor of your choice. We will go with VSCode, and let’s get our hands dirty with codes.

Now open the app.module.ts and import the HttpClientModule like below I was given.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
 
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
 
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Now open the app.component.ts and import the HttpClient, Once you added that next declare the post variable and get the post data through the WordPress post URL. Below I provided how to get the data through the WordPress Rest API URL.

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  posts = [];
 
  constructor(private http: HttpClient){}
 
  ngOnInit(): void{
    this.http.get('https://example.com/wp-json/wp/v2/posts').subscribe(data =>{
      for(let key in data){
        if(data.hasOwnProperty(key)){
          this.posts.push(data[key]);
        }
      }
      console.log(this.posts);
    })
  }
 
}

The final step is to display the post in the loop with ul > li tags like the below I have given. In the code, you see we used ngFor to loop the Rest API post data in the app.component.html.

<ul>
  <li *ngFor="let post of posts">
 	<h1>{{post.title.rendered}}</h1>   
      <p [innerHTML]='post.content.rendered'></p>
  </li>
</ul>
What is Headless WordPress?
Headless WordPress is a CMS setup where WordPress is used for managing content while a different technology is used for the front-end display to visitors​1​.
Why use Headless WordPress?
Benefits include greater flexibility in programming, enhanced security, multi-channel content management, improved site loading time, and deeper visual control​1​.
How to set up WordPress for Angular?
Enable Rest API in WordPress, adjust permalink settings, and use tools like Postman to test API endpoints​1​.
How to get started with Angular for a Headless WordPress site?
Install necessary dependencies like NodeJS, NPM, and Angular CLI. Create a new Angular app using CLI and develop your front-end while interacting with WordPress through REST API​1​.

Summary

Headless WordPress and the benefits it comes with are here to stay for a while. Its popularity will only continue to grow as more developers and site owners come to understand the upsides of a headless option. You’ve now learned how WordPress works and why it’s so often used. Our dedicated developers can help you integrate Headless WordPress with Angular and design effective solutions for you. WordPress’s widespread use is largely thanks to the numerous advantages it offers.

Now you know what headless WordPress is and how to integrate it with angular and take the advantage of the features both of them offer. But what if I tell you, that you can also integrate headless WordPress with Vue as well as Headless WordPress with React is also possible, you can go through the blog or can contact our WordPress developers or JavaScript developers and they will help you in fulfilling the requirements.

author
Vish Shah is Technical Consultant at WPWeb Infotech since 2015. He has vast experience in working with various industries across the globe. He writes about the latest web development technologies and shares his thoughts regularly.

Leave a comment