A big opportunity to use the JavaScript interface with the WordPress rest API is to improve the WordPress management and setup screens. Recently, torque wrote a post on how to use the rest API and jQuery to build the setup screen. This is a good introduction, but I usually use vuejs. In the future, I will describe more specifically how to use vuejs to do this, but I want to discuss two important issues: ES6 and template compilation. create The
For various reasons, I am a loyal fan of vuejs. It’s easy to start without using fancy tools or ES6. That is to say, ES6 is a major improvement of the JavaScript language, so it would be better to use vuejs or virtually all JavaScript if written in ES6. When you apply the knowledge of ES6 to the WordPress environment, you find that a thorny problem is that everything you learn about the tool setting method assumes the traditional html+javascript project. This is ideal for tasks that are completely separated from WordPress, but I want to perform this task in the WordPress administrator now. The
After some experiments, we used Babel to adjust the gulp workflow of WordPress plug-in for minimizing JavaScript files to compile ES6 into browser safe Es5. Hipster JS will not be approved but will work. I want to discuss several other issues from WP admin to vuejs, including how to manipulate and create HTML. ES6 compiling ES6 is really great, and I like working, but not all browsers can use it safely. Babel is by far the most stable tool for creating secure Es5 code in browsers. This may change soon, but considering that Internet Explorer still exists today, this is a worrying issue. The
I have been using gulp to collapse and merge JavaScript files, so I have always wanted to use gulp, but I know that Babel can be deleted soon, so I am also using Babel. Using Babel with gulp is simple. The first is packaging. Add gulp Babel to JSON. NPM install gulp Babel – save dev if you are interested in the devdependencies completed by all gudu gudu libraries required by the example, please do the following: \
\
\
\
\
\
\
\
\
\
}This allows you to add Babel compilation to the standard JavaScript build process. The
Const Gudong = requirement (\
Const watch = require (‘gulp watch’);
Constminify= requirement (\
Const source map =require (\
Const Babel = require (‘gulp babel’);
Const concat = require (‘gulp concat’);
Gulp Task (\
Gudu
SRC (‘.\/assets\/js\/admin\/*.js’)
Pipe (sourcemaps.i
A sample function based on Gutenberg code, which puts the IPT file in the queue and places the data in the saved post in the JavaScript object window_ Exampleeditor:
Male post_type);
If (current_user_can (‘edit_post’, $post->id)){
$request= new wp\u rest\u request (\
$request->set_param (‘context’,’edit’);
$response = rest_do_request ($request);
If ($response->is\u error()){
Returns
}The
Wpuenqueue_script (\
Wp_add_inline_script (\
}The
}You can set up vuejs components to use the above templates. The
New view ({
50: \
Data: functions (){
Returns
Post: window_ Exampleeditor
}The
}The
}); Some PHP can sometimes be very helpful in improving javascript based interfaces. Yes, so far, we have implemented several plug-ins using vuejs on the administrator screen. The client plug-in for caldera forms Pro is the only plug-in that uses ES6. It is recommended to check the source in GitHub. Examples using realt must read Kaam White’s WP notebook plug-in or Gutenberg. The
Now it’s your turn. Find some plug-ins, topics, or site administrators that can be improved with vuej or realt and use them as a learning method. Then, use these technologies on the front end to gradually improve the site.