Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a WordPress plugin with JavaScript only?

I am scared to ask this question, because StackOverflow is already warning me about my question being likely to be downvoted. But I am very curious about this.

I have read that you must have at least one php file in your WordPress Plugin. Which sounds to me like you only need something like an index/entry php file.

However, I wasn't able to find much about JavaScript in WordPress Plugins by using google. Which seemed weird to me, because we're talking here about the web of 2017.

An answer would be very appreciated!

like image 887
Jonas Bergner Avatar asked Nov 16 '17 20:11

Jonas Bergner


2 Answers

The answer is no, you cannot, as you have referenced in your question. By definition, a plugin is at the very least a "PHP file with a WordPress plugin header comment" (Wordpress Docs).

However, there is no reason a Wordpress plugin can't be primarily Javascript. You can follow this guide for some different approaches to include it in your plugin.

like image 67
davidchappy Avatar answered Oct 23 '22 14:10

davidchappy


Since WordPress is (basically) purely php, then all its plugins will also be php. This is they way it has been set up.

In order to have a 'WordPress plugin', yes you will need at least one php file to register to WordPress and enqueue your javascript/css/what-have-you..

This means if you want something like React/Ember on your WP installation, they aren't going to be 'WordPress' plugins, they will belong to the React/Ember ecosystem. You will just have the minimum to enqueue the javascript libraries you want to use.

You can then use your javascript frontend and talk to WP's REST API, and now WP has basically become your server 'framework'.

So, no, there's no such thing as a 'JavaScript WordPress Plugin'. But you can have a plugin that includes as much fancy JS as you may want.

like image 3
Kallmanation Avatar answered Oct 23 '22 13:10

Kallmanation