Creating a WordPress Plugin With Cursor

#
By Mike Davey, Senior Editor

AI-powered coding tools have been around for some time, but Cursor has been getting a lot of press recently. While other tools often focus on code completion, Cursor allows natural language inputs and can guide you in exactly what to do with the code once it’s written.

One of its more attractive features is the ability to set rules, such as “Follow WordPress coding standards.” In this article, we’ll set up Cursor for WordPress development, and then use it to create a simple plugin.

Setting Up Cursor

Getting started with Cursor is relatively straightforward. Just visit the downloads page, download the file for your OS, and install following the on-screen instructions. There are builds available for Mac, Windows, and Linux. We’ll be using the Windows distribution throughout this article, but it shouldn’t make much difference.

The installation screen allows you to choose Vim, Emacs, Atom, Sublime, Jetbrains, or VS Code keybindings, with VS Code as the default. You can also specify the language you wish to use (defaults to English), and disable Cursor’s codebase indexing if you choose. There are also a couple of options to launch Cursor from the command line. Once you’ve made your choices, click Continue to proceed with the installation.

The Cursor install screen as described in the text.

Next, the installer will also if you’d like to import existing VS Code extensions, settings, and keybindings. This is a handy feature if you have a complicated setup, but I’m going to just click Start from Scratch. Next, you can choose your data preferences. “Help Improve Cursor” allows them to collect usage data, while “Privacy Mode” disables this function.

Next, you’ll be prompted to log in. You can create a fresh ID for this, or use an existing Google or GitHub account.

Configuring Cursor for WordPress Development

At this point, you should see three options in Cursor: “Open project”, “Clone repo”, and “Connect via SSH”. Those are all fairly self-explanatory, but we’re going to ignore them for now anyway. Instead, I’m going to put in some time configuring Cursor for WordPress development.

Click on the gear icon in the top right corner to access Cursor’s settings. On the “General” tab, find the “Rules for AI” section, and add the following global rule:

When working with WordPress projects, follow WordPress Coding Standards and best practices.

Adding a rule in this section sets it as a global rule that will be used for all projects. You can also set project-specific rules that only apply to that project.

Cursor’s Rules for AI documentation goes into more detail on this.

Building a Simple WordPress Plugin with Cursor

The goal here is to create a plugin called “Hello Homer” that displays random Simpsons quotes in the WordPress admin area, similar to the “Hello Dolly” plugin. We’ll use Cursor to assist us in developing this plugin, showcasing how AI can streamline the WordPress plugin development process, even when the plugin only achieves something pointless that no reasonable person would ever want.

Cursor is similar to other code editors you may be familiar with. Once you’ve opened a project, you’ll see the files and folders in the left-hand pane. You can click into any of these and modify them as you normally would, creating new files, editing code, etc. However, the real power of Cursor is its AI editing. To access this, click the Toggle AI Pane button in the top right, next to the Settings icon. When opened, the pane will show three tabs: “Chat”, “Composer”, and “Bug Finder”.

  • Chat: This tab provides an interactive AI assistant that you can converse with about your code. You can ask questions, get explanations, or request help with specific coding tasks. The chat feature has built-in context awareness, allowing it to understand your codebase and provide relevant answers.

  • Composer: This is an advanced multi-file editing feature that can help you build entire applications or make large-scale changes across your project. With Composer, you can describe what you want to create, and the AI will generate code across multiple files to implement your request. It’s particularly useful for quickly prototyping ideas or setting up boilerplate code.

  • Bug Finder: This tab offers automated code analysis to detect and highlight potential bugs or issues in your codebase. Before you get too excited, it’s worth mentioning that it’s an experimental feature, and it’s very expensive because it has to run through every single change in your codebase. You might be able to get the price down by stashing Git changes you don’t want scanned.

A screenshot of Cursor with three panes open. The left hand pane shows the file structure of a basic WordPress site. The center pane shows the rules we've given to the AI. The third pane shows the Chat, Composer, and Bug Finder as described in the text.

First, I’ll try the “Chat” feature and see how far that gets us. When it comes to LLMs, the old principle of “garbage in, garbage out” still applies. The more instructions you give it, and the more precise those instructions are, the better your results will be.

This means I should give Cursor very precise and detailed instructions for exactly what I want it to do. That’s not much of a test, though. Instead, we’re going to give it very brief instructions and see what it can whip up based on those and the single rule I set, instructing it to follow the WordPress coding standards and best practices.

The Cursor chat window, with a prompt instructing Cursor to create a plugin called Hello Homer, with similar functionality to the existing plugin Hello Dolly.

The initial results look pretty good, complete with notes on how to implement it, and a warning that the server has to have the right permissions to make external API calls to Frinkiac.com.

The code window has a button marked “Apply” at the top. In this case, clicking this button will create a new file, hello-homer.php, with the provided code, placing it in the root directory of the project. The plugin won’t be functional until it’s moved to the plugins directory and activated through the WordPress admin. Moving the file is simply a matter of clicking on the file in Cursor, and dragging it to the right directory. Once that’s done, we can activate the plugin in the usual way.

Opening the backend of the site, we can see that the plugin definitely exists, complete with a description.

The Hello Homer plugin in the WordPress admin.

Now it’s time for the real test, seeing if it actually works as intended:

A screenshot of the WordPress admin, with a quote from the Simpsons shown: "Smithers, I can still sell them snake oil."

Success! We now have a Simpsons quote in our WordPress admin. Since that worked so well, I’ll ask Cursor to “improve” the plugin. I’m using scare quotes as none of what we’ll ask it to do will make it more useful, because zero plus zero still equals zero. However, it will make it more complicated, allowing us to test Cursor’s abilities further.

Here’s the list of what I’ll ask it to do:

  • Add a fallback quote if we can’t connect to the API. The quote will simply be “D’oh! We couldn’t connect to the API!”
  • Include the title and season of the episode with each quote
  • Include the corresponding screenshot from Frinkiac
  • Add a settings page that allows users to customize the display
  • Add caching to reduce API calls

In a few seconds, Cursor had made the needed revisions to the plugin’s code. After clicking Apply again, we refreshed the site again. On first examination, it worked like a charm:

A screenshot of part of the WordPress admin, showing a screengrab from the Simpsons, an accompanying quote, and the episode's title.

We’ve got a quote, the episode’s title and season, and a screenshot. Cursor also managed to create a settings page:

The settings page for the Hello Homer plugin.

Long story short, it did everything I asked it to do. Expanding on that a bit, it also did a couple of things I didn’t specifically ask it to do: adding sanitization and escaping to protect against cross-site scripting, and using i18n internationalization to make it translatable to other languages. It went ahead and added these because they’re part of WordPress coding standards and best practices, set in the “Rules for AI” settings.

You can see the code for the complete plugin here.

Wrapping Up

This was a quick test, and the plugin we created was fairly simple, but Cursor definitely fulfilled the objectives. Despite that, I’d be very cautious about deploying any of its code in a real-world situation without having a highly experienced dev go over it first. Off the top of my head, there are quite a few situations where a human touch wouldn’t just be helpful, but practically required:

  • Complex Logic: When dealing with intricate business logic or custom algorithms, human developers often have a better grasp of the nuances and edge cases that AI might overlook.

  • Performance-Critical Code: Manually crafted code is often more efficient than AI-generated solutions. There’s usually more than one way to do something. The code the AI spits out at you may work, but there’s no guarantee that it’s the best way to do it.

  • Security-Sensitive Areas: When working with user authentication, data encryption, or other security-critical features, it’s crucial to have a human expert review and implement the code.

In every case, it’s essential that a living, breathing human being take responsibility. That way at least you have someone to blame. This may sound like a joke, but I’m 100% serious. You can have a robot do your work for you, but you’re still on the hook for making sure that work is up to scratch. The key is to view AI as a powerful assistant that enhances your development workflow rather than a replacement for your expertise and judgment.

Have you used Cursor or other AI coding tools? What was your experience? Let me know in the comments.

About the Author

Mike Davey Senior Editor

Mike is an editor and writer based in Hamilton, Ontario, with an extensive background in business-to-business communications and marketing. His hobbies include reading, writing, and wrangling his four children.