Skip to content

Instantly share code, notes, and snippets.

@guillaumeduhan
Created February 7, 2021 15:56
Show Gist options
  • Select an option

  • Save guillaumeduhan/5302237e4cfd65fdce3926766ec1d4f5 to your computer and use it in GitHub Desktop.

Select an option

Save guillaumeduhan/5302237e4cfd65fdce3926766ec1d4f5 to your computer and use it in GitHub Desktop.
Wordpress Plugin Development with Vue.js - Multiples instances
function createNewInstance(myInstanceId) {
Vue.component('button-counter', {
data: function () {
return {
count: 0
}
},
template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>'
})
new Vue({ el: '#instance-' + myInstanceId })
}
<?php
/*
* Plugin Name: Test-Plugin
* Author: Guillaume Duhan
* Version: 1.0.0
* Description: This plugin aims to display a GDPR bar to allow/disallow cookies usage.
*
*/
add_shortcode('my-plugin', 'pluginFunction');
wp_enqueue_script('vue', 'https://cdn.jsdelivr.net/npm/vue/dist/vue.js');
wp_enqueue_script('app', plugin_dir_url(__FILE__) . 'app.js');
function pluginFunction() {
$randomId = uniqid();
echo "<div id='instance-{$randomId}'><button-counter></button-counter></div>";
echo "<script type='text/javascript'>createNewInstance(" . json_encode($randomId) . ");</script>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment