Vue Wordpress



Take the pain out of building site search with the Algolia hosted API. Start free now!

Vue Wordpress module comes with set of essential features allowing you to kick-off your new project in matter of seconds. You can use it as a starter pack for your WordPress site as well. Since WordPress can expose a REST API, this project uses Vue.js, Vue-router, Vuex, Vue-resource & Progressive Web App techniques to create an offline-first web client. You can also fork the project and host it on Azure App Services with continuous integration using the deploy.cmd.

Intrigued by the title and just wanna see some code? Skip ahead.
This tutorial was written for Vue 2 and uses “inline templates”. Vue 3 has deprecated this feature, but there are alternatives (like putting your templates in script tags) that you could translate the idea to.

A few months ago, I was building a WordPress website that required a form with a bunch of fancy conditional fields. Different options and info were required for different choices you could make on the form, and our client needed complete control over all fields 1. In addition, the form needed to appear in multiple places in each page, with slightly different configs.

And the header instance of the form needed to be mutually exclusive with the hamburger menu, so that opening one closes the other.

Vue

And the form had text content that was relevant to SEO.

And we wanted the server response to present some cute animated feedback.

Download casio usb devices driver. (Phew.)

The whole thing felt complex enough that I didn’t want to handle all that state manually. I remembered reading Sarah Drasner’s article “Replacing jQuery With Vue.js: No Build Step Necessary” which shows how to replace classic jQuery patterns with simple Vue micro-apps. That seemed like a good place to start, but I quickly realized that things would get messy on the PHP side of WordPress.

What I really needed were reusable components.

Vue Wordpress Seo

PHP → JavaScript

I love the static-first approach of Jamstack tools, like Nuxt, and was looking to do something similar here — send the full content from the server, and progressively enhance on the client side.

But PHP doesn’t have a built-in way to work with components. It does, however, support require-ing files inside other files 2. WordPress has an abstraction of require called get_template_part, that runs relative to the theme folder and is easier to work with. Dividing code into template parts is about the closest thing to components that WordPress provides 3.

Vue, on the other hand, is all about components — but it can only do its thing after the page has loaded and JavaScript is running.

The secret to this marriage of paradigms turns out to be the lesser-known Vue directive inline-template. Its great and wonderful powers allow us to define a Vue component using the markup we already have. It’s the perfect middle ground between getting static HTML from the server, and mounting dynamic DOM elements in the client.

First, the browser gets the HTML, then Vue makes it do stuff. Since the markup is built by WordPress, rather than by Vue in the browser, components can easily use any information that site administrators can edit. And, as opposed to .vue files (which are great for building more app-y things), we can keep the same separation of concerns we use for the whole site — structure and content in PHP, style in CSS, and functionality in JavaScript.

To show how this all fits together, we’re going to build a few features for a recipe blog. First, we’ll add a way for users to rate recipes. Then we’ll build a feedback form based on that rating. Finally, we’ll allow users to filter recipes, based on tags and rating.

We’ll build a few components that share state and live on the same page. To get them to play nicely together — and to make it easy to add additional components in the future — we’ll make the whole page our Vue app, and register components inside it.

Each component will live in its own PHP file and be included in the theme using get_template_part.

Laying the groundwork

There are a few special considerations to take into account when applying Vue to existing pages. The first is that Vue doesn’t want you loading scripts inside it — it will send ominous errors to the console if you do. The easiest way to avoid this is to add a wrapper element around the content for every page, then load scripts outside of it (which is already a common pattern for all kinds of reasons). Something like this:

The second consideration is that Vue has to be called at the end of body element so that it will load after the rest of the DOM is available to parse. We’ll pass true as the fifth argument (in_footer) for the wp_enqueue_script function. Also, to make sure Vue is loaded first, we’ll register it as a dependency of the main script.

Finally, in the main script, we’ll initialize Vue on the site-wrapper element.

The star rating component

Our single post template currently looks like this:

We’ll register the star rating component and add some logic to manage it:

We’ll write the component template in a separate PHP file. The component will comprise six buttons (one for unrated, 5 with stars). Each button will contain an SVG with either a black or transparent fill.

As a rule of thumb, I like to give a component’s top element a class name that is identical to that of the component itself. Amigo driver download for windows. This makes it easy to reason between markup and CSS (e.g. <star-rating> can be thought of as .star-rating).

And now we’ll include it in our page template.

All the HTML inside the template is valid and understood by the browser, except for <star-rating>. We can go the extra mile to fix that by using Vue’s is directive:

Now let’s say that the maximum rating isn’t necessarily 5, but is controllable by the website’s editor using Advanced Custom Fields, a popular WordPress plugin that adds custom fields for pages, posts and other WordPress content. All we need to do is inject that value as a prop of the component that we’ll call maxRating:

And in our script, let’s register the prop and replace the magic number 5:

In order to save the rating of the specific recipe, we’ll need to pass in the ID of the post. Again, same idea:

Now we can include the same component file in the archive page (a loop of posts), without any additional setup:

The feedback form

Vue Wordpress

The moment a user rates a recipe is a great opportunity to ask for more feedback, so let’s add a little form that appears right after the rating is set.

Notice that we’re appending a unique string (in this case, recipe-id) to each form element’s ID. This is to make sure they all have unique IDs, even if there are multiple copies of the form on the page.

So, where do we want this form to live? It needs to know the recipe’s rating so it knows it needs to open. We’re just building good ol’ components, so let’s use composition to place the form inside the <star-rating>:

If at this point you’re thinking, “We really should be composing both components into a single parent component that handles the rating state,” then please give yourself 10 points and wait patiently.

A small progressive enhancement we can add to make the form usable without JavaScript, is to give it the traditional PHP action and then override it in Vue. We’ll use @submit.prevent to prevent the original action, then run a submit method to send the form data in JavaScript.

Then, assuming we want to use fetch, our submit method can be something like this:

OK, so what do we want to do in .then and .catch? Let’s add a component that will show real-time feedback for the form’s submit status. First let’s add the state to track sending, success, and failure, and a computed property telling us if we’re pending results.

To add the markup for each message type (success, failure, pending), we could make another component like the others we’ve built so far. But since these messages are meaningless when the server renders the page, we’re better off rendering them only when necessary. To do this we’re going to place our markup in a native HTML <template> tag, which doesn’t render anything in the browser. Then we’ll reference it by id as our component’s template.

Why add v-if='false' at the top, you ask? It’s a tricky little thing. Once Vue picks up the HTML <template>, it will immediately think of it as a Vue <template> and render it. Unless, you guessed it, we tell Vue not to render it. A bit of a hack, but there you have it.

Vue Wordpress

Since we only need this markup once on the page, we’ll include the PHP component in the footer.

Now we’ll register the component with Vue…

…and call it inside our form component:

Since we registered <form-status> using Vue.component, it’s available globally, without specifically including it in the parent’s components: { }.

Filtering recipes

Now that users can personalize some bits of their experience on our blog, we can add all kinds of useful functionality. Specifically, let’s allow users to set a minimum rating they want to see, using an input at the top of the page.
The first thing we need is some global state to track the minimum rating set by the user. Since we started off by initializing a Vue app on the whole page, global state will just be data on the Vue instance:

And where can we put the controls to change this? Since the whole page is the app, the answer is almost anywhere. For instance, at the top of the archive page:

As long as it’s inside our site-wrapper and not inside another component, it’ll just work. If we want, we could also build a filtering component that would change the global state. And if we wanted to get all fancy, we could even add Vuex to the mix (since Vuex can’t persist state between pages by default, we could add something like vuex-persist to use localStorage).

So, now we need to hide or show a recipe based on the filter. To do this, we’ll need to wrap the recipe content in its own component, with a v-show directive. It’s probably best to use the same component for both the single page and the archive page. Unfortunately, neither require nor get_template_part can pass parameters into the called file — but we can use global variables:

We can then use $is_archive_item as a global variable inside the PHP component file to check if it is set and true. Since we won’t need to hide the content on the single post page, we’ll conditionally add the v-show directive.

In this specific example, we could have also tested with is_archive() inside the component, but in most cases we’ll need to set explicit props.

We’ll need to move the rating state and logic up into the <recipe-content> component so it can know if it needs to hide itself. Inside <star-rating>, we’ll make a custom v-model by replacing rating with value, and this.rating = i with $emit('input', i) as well . So our component registration will now look like this:

We’ll add v-model in star-rating.php and change rating to value. In addition, we can now move the <feedback-form> up into <recipe-content>:

Now everything is set up so the initial render shows all recipes, and then the user can filter them based on their rating. Moving forward, we could add all kinds of parameters to filter content. And it doesn’t have to be based on user input — we can allow filtering based on the content itself (e.g. number of ingredients or cooking time) by passing the data from PHP to Vue.

Conclusion

Well, that was a bit of a long ride, but look at what we’ve built: independent, composable, maintainable, interactive, progressively enhanced components in our WordPress theme. We brought together the best of all worlds!

I’ve been using this approach in production for a while now, and I love the way it allows me to reason about the different parts of my themes. I hope I’ve inspired you to try it out too.

  1. Of course, two days before launch, the client’s legal department decided they don’t want to collect all that info. Currently the live form is but a shadow of its development self.
  2. Fun fact: Rasmus Lerdorf said that his original intent was for PHP to be templating only, with all business logic handled in C. Let that sink in for a moment. Then clear an hour from your schedule and watch the whole talk.
  3. There are third-party WordPress templating engines that can compile down to optimized PHP. Twig, for example, comes to mind. We’re trying to go the reverse route and send vanilla PHP to be handled by JavaScript.

Over the past several years, I’ve been prying more and more at the limits of the WordPress REST API as they apply to creating ‘headless’ sites and applications. Using the ‘headless’ methodology we can side-step server side rendering (SSR) in favor of JavaScript applications that request data as needed to re-render different views for the site. But it also allows us to decouple the storage of data from the presentation of it, which means that we could have lots of different apps or sites using the same data store in different ways.

At ALT Lab, we tend to bias towards speed over complexity, but one of the biggest challenges I see Indie Makers face when creating web apps is the need for some kind of persistent data storage that is secure, easy to access, and easy to initiate.

In fact, I believe one of the reasons that the idea of a Spreadsheet as a Database has taken off is BECAUSE these things are difficult to initiate for many people who deal with projects of a smaller scope or with questionable life cycles.

No one wants to npm install Mongoose ORM (Object Relational Mapper) and create a new MongoDB instance for a site, app, or project that may never take off. I think most other Indie Hackers are with me here that the act of making is more important than the technical wizardry we can incant.

In the following blog post, I’ll attempt to describe a pattern of headless web app development using Vue and WordPress that most people who have experience doing basic PHP work in WordPress and front end web development can readily implement.

The Application Use Case

The application we created was meant to help people crowd source Wi-Fi hot spots, so the app has a tight integration with the Google Maps and Google Places APIs to help with the geospatial aspects of this. You can see one of the example screens below:

In terms of the data model, that is also fairly standard with only a few fields.

We store some basic info about the particular place, including the longitude and latitude, and then have the API endpoint calculate a distance from the user based on query string parameters. The resulting object that gets saved looks like this:

There are a lot of interesting aspects to this project, so I’m not going to breakdown all the nuts and bolts of this app and will instead focus on the ‘headless’ aspects of this project. If you’re interested in looking at the source code, you can find it here on GitHub. In the future, I’ll break this out a more specialized starter theme.

Application Architecture

A lot of the examples of Headless WordPress sites out there involve using a separate application stack, like Nuxt or Gatsby for example, to pull from the WordPress API as solely a data store. However, the pattern I’ve chosen to use here allows that same kind of access, but instead of using another stack to pull from WordPress, I developed the guts of a WordPress theme into a flexible SPA (single page application) that reads from and writes to the WordPress API.

Rendering and Routing

Download alereon driver. To do this, I pared down all of the PHP templates in the theme directory to just the index.php file, so that no matter what route gets hit on the web server, the SPA container gets loaded.

Best themes for wordpress

There entire content of the index.php look like this:

This loads a basically blank HTML page with a single div that wraps the SPA.

Later, in the root file of our JavaScript application, we target that div and render all of our Vue components inside of it:

This allows me to do a few different things.

First, using the window.WP_OPTIONS object I can pass data from my WordPress install into the JavaScript application, which allows me to set particular pieces of sensitive data, like the Google API key, in a way that is friendly to WP conventions. I’ll talk about where this comes from in the section about the app’s JavaScript.

Second, since this index file gets rendered every page load, it allows me to use the Vue Router package to define the routes of the application. Since this was a fairly simple application, I only had four total routes with only one of them being dynamic:

It’s worth noting that I only tried this with the router configured to hash mode, which makes URLs that look like this: /#/search

I may play around with HTML5 History Mode, at a later date to see if that would work as well.

Creating APIs

One of the nice things about using the WordPress API is that there is already a commonly used controller pattern for extending the WordPress API with your own routes.

The total API work for this project consisted of one controller file that had three functions that required some actual business logic.

The particular endpoints that I needed to create for this project involve getting a list of items, getting a single item by id, and creating a new item. Thus, you can see functions in this file tied to those specific routes that execute logic based on the desired outcome.

To activate these routes, all I need to do is require the controller, create a new instance, and initialize it from the theme’s functions.php file:

Doing this makes those routes accessible via the API, but also adds a new namespace to the /wp-json response that documents the routes and their parameters:

To round off the data model for this project, I went ahead and created a custom post type called map-points where we will store this data using certain WP post conventions. This allows us to segregate our data in an understandable way while still allowing people to do some editing of the objects from the WordPress admin area, which is key for less technical users.

Since we’re storing each map-point as a post object, here is a basic mapping of the JSON keys with how they are stored in the database:

Wordpress Vue Theme

Looking at any of the get/create_items function in the API controller should give you an example of how to map your own custom objects onto the WP Post abstraction fairly painlessly.

Front End Code

Since the front end is created using a SPA approach, all of the application UI is authored using Vue single file components, which allow us to create loosely coupled components with scoped styles and functionality. However, to compile all of these Vue templates into JavaScript that we can execute, we need to introduce a build step using Webpack. The JavaScript structure for the project looks like this:

As I develop, Webpack watches for changes in the front end templates and and compiles down to a single JS file in the distdirectory. From there I include the main.jsfile in the WordPress wp_enqueue_scriptshook while also injecting any additional needed data, like site url or a nonce, using the wp_localize_scriptfunction:

Wordpress Premium Themes

Using wp_localize_script we can pass along data that can be helpful to constructing the front end of the application, but we need to create a nonce to authenticate with the REST API for all requests that would require authentication.

If you look at the following example method used when sending a POST request to create a new place in the database, you can see how these global variables get utilized within the Vue component methods:

In the above example, the addNewLocation method invokes the create_item method of MapPointController to add a new map point to the database, mapping the JSON data model we have here to WP post and meta fields. But since we control all of the logic behind each endpoint, we can add in patterns that allow us to do some expressive querying with GET APIs as well.

In the following example, the front end code relies on the Geolocation API in the browser to get the user’s current position, which is then used for a contextual search if available.

When the SearchPage component get mounted, it call the geolocate method, which gets pass a set of success and failure callbacks that both eventually call the getPlaces method with varying parameters. Inside of getPlaces we can see an example of calling the API with an optional set or query parameters. Within the get_items method of the MapPointController those query parameters are examined and influence how the results are returned by ordering the resultant places by distance from the user if provided.

Building a UI with Speed

In addition to the backend parts of this project that helped me get moving quickly, I also employed a few frameworks that helped me speed things along.

The first of which is BootstrapVue, which is really comprehensive collection of Bootstrap 4 components and plugins. Using this, I was able to create what I felt like was a very usable interface for all devices with minimal coding.

I also use the vue2-google-maps package throughout the project to render maps, activate the Google places autocomplete input, and render driving directions. I’ll more than likely write a whole post on this at some point because there are a ton of useful ways to use this particular set of APIs.

Final Thoughts

As yet another iteration of my playing with Vue and the WordPress API, I felt like things have coalesced here in a way that I’m really starting to dig. Part of that for me is identifying clear patterns for wiring the guts of these systems together in a way that provides some meaningful benefit.

Vue Wordpress Headless

Using some fairly simple API controller conventions for WordPress and somewhat breaking the mold for what a theme could be, we arrive at some interesting possibilities for creating powerful things quickly and easily using tools that are widely available.