Can I create a WordPress plugin with JavaScript only?

Viewed 7950

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!

3 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.

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.

The following article should give you insight about this.

Your WordPress Plugin must have at least one PHP file; it could also contain JavaScript files, CSS files, image files and language files. If there are multiple files, pick a unique name for a directory and a name of your choice for the main PHP file. (This file name is often, but not essentially, the same as the directory name.) Example directory and file names could be mycompanyname-fabulous-functionality and mycompanyname-fabulous-functionality.php respectively. Put all your Plugin's files into the directory you've created and tell your Plugin users to upload the whole directory to wp-content/plugins/.

Hope this helps. But no, you can't create a Wordpress plugin with only Javascript.

Related