Managed-WP.™

The Step-by-Step Guide to Developing a Headless WordPress Site With Vue.js

The Step-by-Step Guide to Developing a Headless WordPress Site With Vue.js cover

Until recently, developers relied on standard content management systems (CMS) to keep everything in order. Everything refers to everything from the backend, such as data and content management, roles, permissions, and administrative activities, to the frontend, which is largely made up of PHP-generated views.

However, with the introduction of modern frontend development, a significant shift is taking place. There is no place for traditional, monolithic CMSs in the vast ecosystem that has sprung up around content management systems. Serverless architectures, API-centric microservices, and static site generators, on the other hand, have proven to be here to stay.

In this tutorial, we'll utilize Vue as a frontend framework to build a headless WordPress environment using the WordPress API to manage backend content. This article's source code is accessible on GitHub. To get started immediately, feel free to fork and clone the source.

‎ ‎

History of Vue

Vue.js is a simple web app framework that can be used to create interactive front-end projects.

Evan You developed VueJs. He works on AngularJS projects at Google. He takes the parts of AngularJs that we loved and creates something really lightweight. The initial source code was made available in July 2013, then VueJs was made available in February 2014.

Evan You was working on a project at Google Creative Labs when they needed to quickly develop a pretty large UI interface. Writing a lot of repeating HTML was obviously time- and resource-consuming, which is why Evan began exploring for a pre-existing solution for this purpose. To his amazement, he realized that there was no tool, library, or framework that was specifically designed for fast prototyping! At the time, Angular was extensively utilized, React.js was just getting started, and frameworks like Backbone.js were employed for large-scale MVC projects. Neither of these complicated frameworks was appropriate for the type of project that required something highly flexible and lightweight merely for rapid UI development.

‎ ‎

Whats the advantages and disadvantages of Vue.js?

First, we'll look at the advantages.

1. Extremely Fast Performance

In this day and age of lightning-fast apps, your website should not take more than a few seconds to load in order to prevent losing visitors. Because the frontend is distinct from WordPress and can be created with high-performance and scalability in mind utilizing contemporary frontend technologies, choosing a headless WordPress approach is beneficial to your website's overall user experience.

2. Granular Management

Choosing headless architecture allows you greater control over your design layout, content display, and how users interact with your application's frontend. It also allows you to safeguard and access your backend material from a single spot.

3. Enhanced Scalability

Scaling WordPress may be difficult at times since you do not have total control over all of the components and functions that operate WordPress, especially if you are not a developer. However, because WordPress is decoupled, it is simple to scale each component separately, and you can simply determine which component requires scaling.

4. Increased Security

We cannot emphasize enough the security benefits of headless WordPress, since decoupled WordPress provides superior protection against hackers and DDoS assaults. Because your important backend data is segregated from your frontend, the user-facing website, the headless WordPress strategy makes it impossible for hackers to access it.

5. Design that is lightweight

You will have more control over your frontend design's structure and layout. Furthermore, you will be able to work on the front end with greater freedom and customization; due to REST API calls, you will be able to take advantage of new web tools and install them on the front end.

6. Multi-Channel Content Distribution

Because headless WordPress communicates your content to the frontend via an API-based system; you can show your content anywhere and on any device, including desktop, website, mobile applications, and touch screen kiosk. It is also feasible to fully utilize Augmented Reality, Virtual Reality, and Internet of Things devices to show and convey your API-based system content.

And the disatvantage of Vue.js

As we continue, we'll go more into the disadvantages of headless, but for now, here are its primary drawbacks:

1. If you separate the backend from the frontend of your website, you will have an increased workload while maintaining several website instances.

2. Because it requires more personnel to be added to your team in addition to additional cash for running several instances, implementing it can be an expensive endeavor.

3. If your material is not displayed in the same manner across all platforms, making it available on several platforms may result in an inconsistent experience for your users.

‎ ‎

Prerequisites

You'll need the following to follow along with this article:

  • Node.js and npm. Use the node -v && npm -v commands to check if they are installed or to install them.
  • A fundamental understanding of JavaScript and Vue
  • A working WordPress account and website
  • You'll need a WordPress Business account or Managed-WP.™ PRO plan to use the Plugin functionality later in this post.

‎ ‎

The question now is, what are the three most effective ways to use WordPress and Vue?

You really hadn't prepared yourself for this, had you?

1. Within the WordPress administration dashboard, build a single-page application (SPA) with the help of Vue.

In the WordPress admin panel, you are able to construct a "sub-website" using Vue. This is possible. You could be wondering why you would act in such a way.

When developing complicated plugins or even just constructing unique admin pages, you may find that the administration panel requires the addition of a tab system or numerous page systems.

When using WordPress's standard procedures, each administrative page would need to be refreshed; as a result, the user experience would not be very satisfying. Additionally, utilizing the standard WP UI components may not necessarily result in an aesthetic result.

This is an example of a specialized administration page.

SureCart admin portal in WordPress

WP Rocket admin portal in WordPress

Within the WordPress administration panel, this is the user interface for the WP Rocket and SureCart plugin. As can be seen, the user interface has been completely customized and features its own subpages organized in tabs. Each tab corresponds to a sub-URL, making it simple to send the information to another person.

Within the WordPress administration panel, it functions similarly to a miniature website.

Using Vue and Vue-router, you will have little trouble doing this task. There is only one component that might be challenging, and that is managing URLs. It is true that having two "websites" co-existing might be challenging. There is a possibility of URL conflicts.

Don't be concerned, I've got your back here. There are only two settings in the Vue Router instance that need to be modified: the default option requires that you set the value using your own personalized WP page route.

In the mode drop-down menu, select "hash" as the value to use. Hashes, rather than modifying the complete URL, will be used when the "hash" mode is active.

It is also feasible, for instance, to replace the page that serves as the content editor.

That function is performed by the Elementor editor.

Black Editor(

This standard WordPress editor frequently uses… (with hashes for each sub-url)

In the admin panel, you have the ability to establish a sub-website for virtually anything, including the plugin page, editor, dashboard, middle office, etc.

Consequently, in order to develop a standalone Vue SPA, you would make use of Vue CLI or any other tool, set the router's base + mode option to the information that I provided before, export it, and then load it into WordPress using the appropriate WP hooks.

2. Create frontend and administrative components that are reusable by employing Vue to do so.

On a website, you will almost certainly engage in some form of micro-interaction at some point.

Toggles for menus, dropdowns, accordions, carousels, and so on are some examples.

A user interface kit is exactly what you see here!

But you may also have certain components of user interface that interact with data in an asynchronous manner, such as buttons to like or vote on content, forms, a bespoke media player, etc.

/! It is possible that you may need to utilize the Runtime + Compiler version of Vue when integrating Vue components into WordPress or any other backend template.

Installation – Vue.js

Vue.js is a Framework for Progressive JavaScript Applications.

Some of the components would interact with the data being shown, while others would not.

Depending on that, you won't be able to make the same sort of components as before.

Some may utilize their own customized template, while others will make use of the automatically created markup.

and make use of the current markup

Vue.component ('alert-box', { 
template:
<div class="demo-alert-box">
<strong>Error!</strong>
<slot></slot>
</div>
}}
<my-component inline-template> 
<div>
<p>These are compiled as the component's own template.</p>
<p>Not parent's transclusion content.</p>
</div>
</my-component>

You could even utilize Vue to produce native Web Components, which you could then repurpose across other projects like ionicons (which is made with Stencil JS, but you get my point).

Those components might be utilized in either the WP PHP templates or the functions that return HTML depending on your preference.

You are able to give data to instances or components from WP by stringifying it and saving it to a global variable or by passing it straight to the component prop.

The plan is to load Vue and then link your Vue instances to certain IDs in the HTML document.

function  themeslug_enqueue style() { 
wp_enqueue_scripts('my-theme''style.css', false );


function themeslug_ enqueue script() {
Wp_enqueue_script( 'my-js', "filename.js', false );

add_action('w_enqueue_scripts', 'themeslug_enqueue_style' );
add_action('w_enqueue_style', ' themeslug_enqueue_script' );

Reload the JS and/or CSS for your Vue components.

<div id="app"> 
{{ message }}
< div>
var app = new Vue( {
el: '#app',
data: {
message: 'Hello Vue!'
}
})

The top portion is located in your WordPress template files, while the lower portion is comprised of your scripts that are loaded via wp enqueue.

3. Develop a standalone frontend website with Vue and use WordPress as your data source.

Lastly, but certainly not least.

Some of you might find this statement to be self-explanatory, but others might not.

Since version 4.7, WordPress has included a REST application programming interface (API), which allows users to make calls to specific endpoints in order to get data (such as posts, pages, and custom fields with the assistance of plugins…).

A Headless Content Management System is another name for this.

In point of fact, these approaches may be narrowed down to a greater number than you might think.

Your separate frontend that uses the WP REST API may be any of the following:

  • A single-page application or website (SPA)
  • The SSR application or website
  • A webpage that is produced statically (JAMStack)
  • A PWA
  • A mobile app
  • Another monolithic backend that uses a template engine that is both simpler and quicker

To put it succinctly, any other client that is capable of consuming the API and in which you have complete control over the frontend.

In essence, WordPress would merely function in the same capacity as a data source, much like a backend API would. Additionally, plugins like as ACF and Pods have the ability to offer custom data via the API.

You could also add new endpoints to the WordPress API and make advantage of the authentication mechanism by providing a produced nonce in addition to other things.

Summary

Headless WordPress, along with the many benefits it brings, is here to stay for the foreseeable future. As more website owners and developers become aware of the benefits of using a headless alternative, its popularity is only going to continue to rise in the coming years.

You've been given an overview of the benefits, advantages, and drawbacks of headless WordPress in this article, and you've also been shown how to construct and deploy your very first instance of headless WordPress using Vue.js. You have now made significant progress in implementing WordPress without a graphical user interface.

What are some of the best practices that you've seen for launching a headless WordPress site? We would appreciate it if you would share them in the comments area.


Popular Posts