Intrigued by the title and just wanna see some code? Skip ahead.
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.
And the form had text content that was relevant to SEO.
And we wanted the server response to present some cute animated feedback.
(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. 
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.



We’ll register the star rating component and add some logic to manage it:
// main.js
Vue.component(star-rating, {
data () {
return {
rating: 0
}
},
methods: {
rate (i) { this.rating = i }
},
watch: {
rating (val) {
// prevent rating from going out of bounds by checking it to on every change
if (val < 0) this.rating = 0 else if (val > 5)
this.rating = 5
// … some logic to save to localStorage or somewhere else
}
}
})
// make sure to initialize Vue after registering all components
new Vue({
el: document.getElementById(site-wrapper)
})
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.

Rate recipe:



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. This makes it easy to reason between markup and CSS (e.g.  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 . 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:

>

Rate recipe:


And in our script, let’s register the prop and replace the magic number 5:
// main.js
Vue.component(star-rating, {
props: {
maxRating: {
type: Number,
default: 5 // highlight
}
},
data () {
return {
rating: 0
}
},
methods: {
rate (i) { this.rating = i }
},
watch: {
rating (val) {
// prevent rating from going out of bounds by checking it to on every change
if (val < 0) this.rating = 0 else if (val > maxRating)
this.rating = maxRating
// … some logic to save to localStorage or somewhere else
}
}
})
In order to save the rating of the specific recipe, we’ll need to pass in the ID of the post. Again, same idea:

recipe-id=>

Rate recipe:


// main.js
Vue.component(star-rating, {
props: {
maxRating: {
// Same as before
},
recipeId: {
type: String,
required: true
}
},
// …
watch: {
rating (val) {
// Same as before
// on every change, save to some storage
// e.g. localStorage or posting to a WP comments endpoint
someKindOfStorageDefinedElsewhere.save(this.recipeId, this.rating)
}
},
mounted () {
this.rating = someKindOfStorageDefinedElsewhere.load(this.recipeId)
}
})
Now we can include the same component file in the archive page (a loop of posts), without any additional setup:



The feedback form
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.
// main.js
Vue.component(feedback-form, {
props: {
recipeId: {
type: String,
required: true
},
show: { type: Boolean, default: false }
},
data () {
return {
name: ,
subject:
// … other form fields
}
}
})

v-if=showForm(recipe-id)>

>
v-model=name>


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 :

recipe-id=>

Rate recipe:



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.

>

>
v-model=name>


Then, assuming we want to use fetch, our submit method can be something like this:
// main.js
Vue.component(feedback-form, {
// Same as before
methods: {
submit () {
const form = this.$el.querySelector(form)
const URL = form.action
const formData = new FormData(form)
fetch(URL, {method: POST, body: formData})
.then(result => { … })
.catch(error => { … })
}
}
})
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.
// main.js
Vue.component(feedback-form, {
// Same as before
data () {
return {
name: ,
subject:
// … other form fields
sent: false,
success: false,
?? error: null
}
},
methods: {
submit () {
const form = this.$el.querySelector(form)
const URL = form.action
const formData = new FormData(form)
fetch(URL, {method: POST, body: formData})
.then(result => {
this.success = true
})
.catch(error => {
this.error = error
})
this.sent = true
}
}
})
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