Using Vue in WordPress

workingwebsites

Lisa Armstrong

Posted on April 3, 2019

Using Vue in WordPress

Like peanut butter and jam sometimes two good things are even better together.

In this tutorial I'll show you how to use Vue in a WordPress site. The tutorial is meant for those who have coded in WordPress and Vue or is interested in either one. Knowledge in PHP, WordPress, Javascript, Vue will help.

Vue Programmers: Why WordPress?

Statistically, WordPress runs about a third of the world's websites. If you've created something in Vue you'd like to share, WordPress is a good platform to make it available on.

You can customize your own site, or share your code on WordPress' plugin repository.

WordPress Programmers: Why Vue?

The short answer is, Vue is known for its simplicity and robustness making it a solid choice for development. If you're adding functionality to your site, it makes a good tool to get up and running with. It's in JavaScript, so it's familiar to most developers.

WordPress uses React, can I even use Vue?
Of course! Although some features in WordPress may be in React, you are not limited to it.

WordPress is open source, do what makes you happy.

Tutorial Overview

We'll make a short code that displays something generated in Vue. We'll be using a simple example because this is more about how to put Vue and WordPress together rather than what they do.

The approach is:

  1. Create a plugin in WordPress to use the code
  2. Create a shortcode in the plugin
  3. Create your Vue code
  4. Load Vue, then your code file.
  5. Make sure your .js file is loaded after the dom is drawn.

That's basically it. Let's get into the details.

WordPress: The Plugin.

Plugins are both simple and robust, they allow you to add functionality to your site. This plugin will be a simple 'hello world' type. We'll call it 'wp-vue-tutorial', so let's begin:

1) Create a new folder in your WordPress plugin directory.

This is where the files will be kept, it should be the name of the plugin. For this tutorial let's use:
/wp-content/plugins/wp-vue-tutorial

We need a 'master file', so let's make that.

2) In the directory, create a master file with the plugin name.

This file called 'wp-vue-tutorial.php' so it should look like:
/wp-content/plugins/wp-vue-tutorial/wp-vue-tutorial.php

Yes, the plugin name, folder name and master file name are all the same so WordPress knows where to find it.

Once WordPress opens it up, it needs some more info on what this thing is. That's included in the Header. We're keeping it simple, so in wp-vue-tutorial.php, add the following code:

<?php
/**
 * Plugin Name: WordPress Vue Tutorial
 * Description: A demo on how to use Vue in WordPress.
 */
?>

More info: https://developer.wordpress.org/plugins/

If you go to your WordPress Dashboard, you'll see your plugin.
Dashboard -> Plugins
It should appear in the list.

3) Activate your plugin.

Click Activate on your plugin.

Now it's loaded, but not much happens because there's no code to run. So, let's create a shortcode.

A shortcode tells WordPress to insert the content generated by your PHP code into the content of the page or post.
Ex. The user enters something like [myshortcode] and when the page is displayed, it shows 'This is content from my shortcode.'

Note: WordPress' current text editor (Gutenberg) allows you to put in blocks of content in. A shortcode will still work with that, in fact there is a block specifically for short codes. We're using shortcodes in this tutorial because it's a standard that works.

For more info: https://codex.wordpress.org/Shortcode_API

4) Create a shortcode in your Plugin.

Add the following code to your plugin file (wp-vue-tutorial.php):

 //Add shortscode
 function func_wp_vue(){
   return "Hello Shortcode";
}
add_shortcode( 'wpvue', 'func_wp_vue' );

Breaking it down:
The function func_wp_vue() returns the text Hello Shortcode.

add_shortcode( Make this shortcode available.
'wpvue', The code the user uses in the content
[ 'wpvue'].
'func_wp_vue' The function returning the string to display.
); End of shortcode.

Your file should look like:

<?php
/**
 * Plugin Name: WordPress Vue Tutorial
 * Description: A demo on how to use Vue in WordPress.
 */

//Add shortscode
 function func_wp_vue(){
 return "Hello Shortcode";
}
add_shortcode( 'wpvue', 'func_wp_vue' );
?>

Test it.
Edit, or add a new WordPress page. In the content area, type in [wpvue].
Publish the page and have a look at it. You should see Hello Shortcode.

The short code can return more than a word, we can put in an element in the too.

Let's change the code to:

//Add shortscode
function func_wp_vue(){
  $str= "<div id='divWpVue'>"
    ."Message from Vue: "
    ."</div>";
  return $str;
} // end function

Test it again.

Set Up Vue

To use Vue, you need three things:

  1. Vue js files need to be loaded on the page so you can use the framework.
  2. You need an element on your page Vue can work in. Usually a div.
  3. You need your Vue code in a .js file to do its work in the element.

Load Vue.js

Let's start with adding Vue. You can add Vue through a link to a CDN. There's 2 versions, one for development with debugging options and one for production that is faster and leaner.
We'll use the development one: https://cdn.jsdelivr.net/npm/vue/dist/vue.js

We need to load this into WordPress, which can be done through the magic of 'wp enqueue scripts' and 'wp_register_script'.
This hook automatically load the script into WordPress' head when needed.

It looks like:

function func_load_vuescripts() {
 wp_register_script( 'wpvue_vuejs', 'https://cdn.jsdelivr.net/npm/vue/dist/vue.js');
}
add_action('wp_enqueue_scripts', 'func_load_vuescripts');

Break down:

function func_load_vuescripts() { Define the function .
wp_register_script( Tell WP this script exists
(we'll load it later) .
'wpvue_vuejs', This is the WordPress reference to the script.
''https://cdn.jsdelivr.net/npm/vue/dist/vue.js'', Actual script file.
); Close the wp_register_script.
} Close the function.
add_action( WordPress, do this.
'wp_enqueue_scripts', Hook into 'wp_enqueue_scripts'.
'func_load_vuescripts' Do this function for 'wp_enqueue_scripts' .
); End action .

For more info:

We'll add the code to your plugin file, above the shortcode.
It should look like this:

<?php
/**
 * Plugin Name: WordPress Vue Tutorial
 * Description: A demo on how to use Vue in WordPress.
 */

//Register Scripts to use 
function func_load_vuescripts() {
 wp_register_script( 'wpvue_vuejs', 'https://cdn.jsdelivr.net/npm/vue/dist/vue.js');
}
add_action('wp_enqueue_scripts', 'func_load_vuescripts');

//Add shortscode
function func_wp_vue(){
  //Build String
  $str= "<div id='divWpVue'>"
  ."Vue code here: "
  ."</div>";

  //Return
  return $str;
} // end function

//Add shortcode to WordPress
add_shortcode( 'wpvue', 'func_wp_vue' );
?>

Right now, we told WordPress about Vue.js, but we haven't loaded it. You load it in the shortcode function so it fires when the shortcode is running. Makes sense?

Include the following in your shortcode function:

//Add Vue.js
  wp_enqueue_script('wpvue_vuejs');

Breakdown:

wp_enqueue_script( WordPress, load this script
'wpvue_vuejs' It's called 'wpvue_vuejs' defined when we registered the script.
); End enqueue script

It should look like:

function func_wp_vue(){
  //Add Vue.js
  wp_enqueue_script('wpvue_vuejs');
  
  //Build String
  $str= "<div id='divWpVue'>"
  ."Vue code here: "
  ."</div>";

  //Return
  return $str;
} // end function

Now if you run it, not much has changed, but if you were to look in the source code of the page you'd find :

<script type='text/javascript' src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js?ver=5.1.1'></script>

Vue is now loaded, we can use the div generated in the shortcode ('divWpVue'), all we need is some Vue code to run in that div.

Create Your Code File for Vue

Make a new file: vuecode.js your plugin directory:
wp-content/plugins/wp-vue-tutorial

You should have 2 files in there:

  • wp-vue-tutorial.php
  • vuecode.js

More Info:
https://vuejs.org/v2/guide/

This is a really simple demo, so include the following code:

var app = new Vue({
  el: '#divWpVue',
  data: {
    message: 'Hello Vue!'
  }
})

Breakdown:

var app = new Vue({ We're creating a var called 'app' that is actually a Vue instance.
el: '#divWpVue', This app runs in the element 'divWpVue'.
data: { This app has data with it.
message: 'Hello Vue!' One piece of data is called 'message' and has the value 'Hello Vue!'
}
})
Close the data section
Close the app section

For more info on Vue:

In order to display the message, you need to include the data message in the div. Change your shortcode function (func_wp_vue) to include {{message}}.

The double curly brackets is a way of including Vue stuff in your HTML file. If you add {{message}} inside the element you've assigned to your Vue instance, it will magically appear. This is the short non-technical answer of course.

Important! If you check your file, and everything is working, you will see 'Hello Vue!' where {{message}} was placed. If you see {{message}}, something's not working.

Let's add the {{message}} to the shortcode.

function func_wp_vue(){
  //Add Vue.js
  wp_enqueue_script('wpvue_vuejs');
  
  //Build String
  $str= "<div id='divWpVue'>"
  ."Message from Vue: {{ message }}"
  ."</div>";

  //Return
  return $str;
} // end function

If you test it now, it won't work, {{message}} will display, not 'Hello Vue!'

Why? Because we haven't loaded your vuecode.js file yet. We can load it the same was as the Vue CDN file, right?

Hmm... not exactly. You're on the right track, but there's a couple of Gotcha's to watch out for. Let's walk through it.

To register vuecode.js, you would expect something like the following would work:

 wp_register_script('my_vuecode', 'vuecode.js')

No, it won't because WordPress can't find vuecode.js.

Gotcha: Your file isn't loading

Remember the shortcode is displayed on a page with a different path than the plugin. It's a kind of logistical hall of mirrors, nothing is where it appears.

To fix it, you need to be more precise about where the file is located. That's handled by adding a WordPress function that returns the path of the plugin file you're using. plugin_dir_url will do the trick.

wp_register_script('my_vuecode', plugin_dir_url( __FILE__ ).'vuecode.js') 

More info on plugin urls: https://codex.wordpress.org/Function_Reference/plugins_url

Gotcha: Vue.js needs to load first

Your vuecode.js runs on Vue, so make sure those files are loaded before you start running your Vue code.

To ensure that, tell WordPress it depends on Vuejs by adding the dependency to the register_script function. Use the handle from the Vue.js script register.

wp_register_script('my_vuecode', plugin_dir_url( __FILE__ ).'vuecode.js', 'wpvue_vuejs' );

GOTCHA: Your file with Vue code has to come AFTER the DOM loads

Why? It loads scripts in the head, but your vuecode.js controlling a div in the body and it hasn't been loaded yet. It can't find it and throws an error.

So, load Vue.js first, then the body with the DOM, then the controlling script ( vuecode.js ). You can do that through the register script by adding a 'true' value to the parameters. 'true' tells WordPress to load the script in the footer.

It should look like this:

wp_register_script('my_vuecode', plugin_dir_url( __FILE__ ).'vuecode.js', 'wpvue_vuejs', true );

Breaking it down:

wp_register_script( WordPress, register this script.
'my_vuecode', Its handle (reference) .
plugin_dir_url( __FILE __).'vuecode.js', The file is located in the same directory as this plugin. This is the link to it.
'wpvue_vuejs', Make sure this is loaded first. It's a reference to another registered script.
true Load the script in the footer.
); Close the function.

Your function to load the scripts should look like:

//Register scripts to use
function func_load_vuescripts() {
    wp_register_script('wpvue_vuejs', 'https://cdn.jsdelivr.net/npm/vue/dist/vue.js');
    wp_register_script('my_vuecode', plugin_dir_url( __FILE__ ).'vuecode.js', 'wpvue_vuejs', true );
}
add_action('wp_enqueue_scripts', 'func_load_vuescripts');

More Info on Registering Scripts:

That should do it. But, you still won't see anything, why not?
After all that, you've only registered the script. WordPress knows all about it, but you haven't told it to fire it.

Gotcha: Don't forget to enqueue /load the script

Ok, this one is just like enqueing the Vue.js script. Add the following to the shortcode function -- 'func_wp_vue'.

wp_enqueue_script('my_vuecode');

'func_wp_vue ' should look like:

//Return string for shortcode
function func_wp_vue(){
  //Add Vue.js
  wp_enqueue_script('wpvue_vuejs');
  //Add my code to it
  wp_enqueue_script('my_vuecode');

  //Build String
  $str= "<div id='divWpVue'>"
    ."Message from Vue: {{ message }}"
    ."</div>";

  //Return to display
  return $str;
} // end function

//Add shortcode to WordPress
add_shortcode( 'wpvue', 'func_wp_vue' );

Putting It All Together:

You should have your plugin called 'wp-vue-tutorial'. In the plugin directory, there should be 2 files:

  • vuecode.js (that has the Vue code in it)
  • wp-vue-tutorial.php (with the WordPress code)

vuecode.js

Your Javascript file that runs off of Vue.js should look like the following:

var app = new Vue({
  el: '#divWpVue',
  data: {
    message: 'Hello Vue!'
  }
})

wp-vue-tutorial.php

Your WordPress file that defines the plugin and has the shortcode code in it should look like:

<?php
/**
 * Plugin Name: WordPress Vue Tutorial
 * Description: A demo on how to use Vue in WordPress.
 */

//Register scripts to use
function func_load_vuescripts() {
    wp_register_script('wpvue_vuejs', 'https://cdn.jsdelivr.net/npm/vue/dist/vue.js');
    wp_register_script('my_vuecode', plugin_dir_url( __FILE__ ).'vuecode.js', 'wpvue_vuejs', true );
}
//Tell WordPress to register the scripts 
add_action('wp_enqueue_scripts', 'func_load_vuescripts');


//Return string for shortcode
function func_wp_vue(){
  //Add Vue.js
  wp_enqueue_script('wpvue_vuejs');
  //Add my code to it
  wp_enqueue_script('my_vuecode');

  //Build String
  $str= "<div id='divWpVue'>"
    ."Message from Vue: {{ message }}"
    ."</div>";

  //Return to display
  return $str;
} // end function

//Add shortcode to WordPress
add_shortcode( 'wpvue', 'func_wp_vue' );
?>

Note: You can include the shortcode in a theme's functions.php file, it will still work the same as long as the the theme is being used, or its functions.php file isn't over-written with an upgrade.
I prefer the plugin because it separates out the style from the content and usually my Vue code is about content anyway.

More Info:
https://codex.wordpress.org/Functions_File_Explained

If you use the theme functions file, you need to call the file path in relation to your theme, not the plugin.

More Info:
https://codex.wordpress.org/Function_Reference/get_template_directory

Download Files

You can download the files at:
https://github.com/workingwebsites/wp-vue-tutorial

Conclusion

Ok, so 'Hello Vue' doesn't exactly set the world on fire. However, once you've got that in there, you can see how you can add a lot of functionality to your WordPress site through Vue.

By using Vue in your WordPress site, accessing API's and making your site more interactive gets easier. Likewise, if you have some crafty code written with Vue, dropping it into a WordPress site (and there's lots of them) is not a big trick.

Like peanut butter and jam, combining two good things make it better!

💖 💪 🙅 🚩
workingwebsites
Lisa Armstrong

Posted on April 3, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Using Vue in WordPress
vue Using Vue in WordPress

April 3, 2019