<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: DGMANN</title>
    <description>The latest articles on Forem by DGMANN (@dgmann).</description>
    <link>https://forem.com/dgmann</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F280275%2Fefade672-2af3-424d-91b7-12fb15409326.png</url>
      <title>Forem: DGMANN</title>
      <link>https://forem.com/dgmann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dgmann"/>
    <language>en</language>
    <item>
      <title>Vue Application as a Wordpress Plugin</title>
      <dc:creator>DGMANN</dc:creator>
      <pubDate>Wed, 27 Nov 2019 07:51:02 +0000</pubDate>
      <link>https://forem.com/dgmann/vue-application-as-a-wordpress-plugin-9nm</link>
      <guid>https://forem.com/dgmann/vue-application-as-a-wordpress-plugin-9nm</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fmichaelsoriano.com%2Fwp-content%2Fuploads%2F2018%2F01%2Fwp-vue.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fmichaelsoriano.com%2Fwp-content%2Fuploads%2F2018%2F01%2Fwp-vue.jpg" alt="Vue and Wordpress"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Intro
&lt;/h4&gt;

&lt;p&gt;I read &lt;a href="https://dev.to/workingwebsites/using-vue-in-wordpress-1b9l"&gt;this article&lt;/a&gt; of Lisa Armstrong to use VueJS to create a plugin for Wordpress. My post will extend Lisa's way and show a solution with a little bit more VueJS.&lt;br&gt;
This is just a really short and quick article. I will write more about this next time. &lt;/p&gt;
&lt;h4&gt;
  
  
  Step 1
&lt;/h4&gt;

&lt;p&gt;First we have to create a Vue application with &lt;code&gt;vue create wp-vue&lt;/code&gt;. Select the default configuration and run &lt;code&gt;npm run build&lt;/code&gt;. &lt;/p&gt;
&lt;h4&gt;
  
  
  Step 2
&lt;/h4&gt;

&lt;p&gt;Now we can follow the steps of &lt;a href="https://dev.to/workingwebsites/using-vue-in-wordpress-1b9l"&gt;Lisas' article&lt;/a&gt;. &lt;/p&gt;
&lt;h5&gt;
  
  
  The shorthand-version
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;We create a file wp-vue.php in /wp-content/plugin/wp-vue/&lt;/li&gt;
&lt;li&gt;with the following code:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
 /**
 * Plugin Name: WordPress Vue
 * Description: Vue-App in WordPress.
 */

 function func_load_vuescripts() {
     wp_register_script('wpvue_vuejs', plugin_dir_url( __FILE__ ).'dist/js/app.c8d5a15f.js', true);
     wp_register_script('wpvue_vuejs1', plugin_dir_url( __FILE__ ).'dist/js/chunk-vendors.5e0c61d5.js', true);
 }

 add_action('wp_enqueue_scripts', 'func_load_vuescripts');

 //Add shortscode
 function func_wp_vue(){
     wp_enqueue_script('wpvue_vuejs');
     wp_enqueue_script('wpvue_vuejs1');

     $str= "&amp;lt;div id='app'&amp;gt;"
           ."Message from Vue: "
           ."&amp;lt;/div&amp;gt;";
     return $str;
 } // end function

  add_shortcode( 'wpvue', 'func_wp_vue' );
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;one very important change is the id attribute of the div element. For the connection to our Vue-App&lt;/li&gt;
&lt;li&gt;a second important change is that we won't load the VueJS-Script with CDN. We load the chunk-vendors.js and the app.js from our Vue-App&lt;/li&gt;
&lt;li&gt;now we have to activate the plugIn in Wordpress &lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Step 3
&lt;/h4&gt;

&lt;p&gt;To get the js files we have to upload the dist folder which we created in Step 1 to the plugin folder &lt;code&gt;wp-vue&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 4
&lt;/h4&gt;

&lt;p&gt;If you already added the shortcode &lt;code&gt;[wpvue]&lt;/code&gt; in one of your pages the whole Vue-App shows up. We only have to reduce the code in HelloWorld.vue a little bit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
      &amp;lt;div class="hello"&amp;gt;

      &amp;lt;/div&amp;gt;
    &amp;lt;/template&amp;gt;

    &amp;lt;script&amp;gt;
    export default {
      name: 'HelloWorld',
      props: {
        msg: String
      }
    }
    &amp;lt;/script&amp;gt;

    &amp;lt;!-- Add "scoped" attribute to limit CSS to this component only --&amp;gt;
    &amp;lt;style scoped&amp;gt;
    &amp;lt;/style&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...and run the &lt;code&gt;npm run build&lt;/code&gt; task again. After that we can upload the necessary files and delete the old ones. We also have to change the scripts in our wp-vue.php. Now we get a perfect start for a new Vue-App plugin.&lt;br&gt;
The hash behind &lt;code&gt;app.&lt;/code&gt; and &lt;code&gt;chunk-vendors.&lt;/code&gt;should be delete to avoid editing the wp-vue.php script. We can solve this by adding a file &lt;code&gt;vue.config.js&lt;/code&gt; next to our package.json with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  filenameHashing: false,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We just have to remove the hashs out of our &lt;code&gt;wp-vue.php&lt;/code&gt; and it will work. But you have to be careful with that because the caching should be handled at any point. &lt;a href="https://dev.to/jonyhayama/comment/1bnna"&gt;Look at Jony's suggestion&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Development
&lt;/h4&gt;

&lt;p&gt;To develope in the development mode (so that we are able to use all advantages of VueJS) we can run &lt;code&gt;npm run serve&lt;/code&gt; and create a local Vue-App to show our plugin.&lt;br&gt;
A huge advantage of this way is that we also can realize an atomic design workflow and are also able to scale this up by using the Vue-App for more than one plugin.&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>vue</category>
      <category>atomic</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
