AI coding assistants have significantly sped up the day-to-day work of WordPress developers. Registering a custom post type, writing a WooCommerce hook, creating an admin settings page or defining a REST API endpoint now takes minutes. But this speed can come at a price: code generated by AI may look like it works while containing serious security vulnerabilities.
This article is for my colleagues who do AI-assisted WordPress development and for technically minded site owners who add custom code to their own sites. I will share, with examples, the security and quality checkpoints I review in WordPress code written with AI.
Why Is Extra Care Needed?
AI models learn from millions of code examples on the internet. A significant portion of those examples are outdated, lacking in security, or written only to “work”. As a result, the model sometimes repeats the same habits: using user input without cleaning it, not making output safe with escaping, skipping capability checks, or writing unprepared SQL queries.
Most of the vulnerabilities reported in the WordPress plugin ecosystem stem from exactly this kind of mistake: cross-site scripting (XSS), cross-site request forgery (CSRF), SQL injection and missing authorization.
Security Checklist
1. Input Sanitization
Every piece of data coming from a user, whether from a form, a URL parameter or a REST request, must be sanitized before use. WordPress offers type-specific functions for this: sanitize_text_field() for text fields, sanitize_email() for emails, absint() or intval() for integers, esc_url_raw() for URLs, sanitize_textarea_field() for multi-line text. If you see $_POST or $_GET values used directly in AI-written code, that is a red flag. Also, slash escapes must be removed with wp_unslash() before accessing superglobal variables.
2. Output Escaping
Every variable printed to the screen must be escaped appropriately for its context. esc_html() for HTML content, esc_attr() for HTML attributes, esc_url() for URLs, wp_json_encode() or esc_js() for data used inside JavaScript, and wp_kses() or wp_kses_post() if limited HTML is to be allowed. The general rule in WordPress is: “escape late”, meaning escape as close as possible to the point where the output is written. This is one of the steps most often skipped in AI-generated code.
3. Nonce Verification
Every action that changes state, meaning a form submission, saving settings, a delete action or an AJAX request, must be protected with a nonce. A nonce should be added to forms with wp_nonce_field() and verified on the processing side with wp_verify_nonce() or check_admin_referer(). For AJAX actions, check_ajax_referer() is used. An action without a nonce check is vulnerable to CSRF attacks.
4. Capability Checks
A nonce verifies that the request really came from your form, but it does not verify whether the user making the request is authorized to perform that action. In every sensitive action, the appropriate capability must be checked with current_user_can(). For example, an action that saves settings should require the manage_options capability. In REST API endpoints, permission_callback must always be defined; one of the mistakes AI makes often is to get past this field with “__return_true” or to skip it entirely.
5. Database Queries
If you need to write a custom SQL query, every query containing variables must be prepared with $wpdb->prepare(). Inserting user input directly into the query text creates an SQL injection vulnerability. Wherever possible, prefer WordPress’s built-in functions such as WP_Query, get_posts() and get_option(); they handle security for you.
6. Direct Access Protection
At the very beginning of plugin and theme files, there should be an ABSPATH check that prevents the file from being called directly. It is a small detail, but AI often skips it.
7. File Upload Handling
Code that involves file uploads is the riskiest code. The file type and extension must be verified, WordPress functions such as wp_handle_upload() should be used, and you must make sure uploaded files cannot be executed. If you had AI write this kind of code, check it twice.
Quality Checklist
Another topic as important as security is the quality and maintainability of the code.
Using a Prefix
Use a unique prefix in the names of functions, classes, constants, options and meta keys. For example, I use a prefix like “bd_” in my projects. Generic names generated by AI, such as “save_settings()”, can conflict with other plugins.
WordPress Coding Standards
Writing code that follows WordPress’s PHP coding standards (WPCS) improves readability and ease of maintenance. By adding the WPCS rules to your project with PHP_CodeSniffer, you can automatically check the code generated by AI. This tool also catches, to a large extent, output where escaping was forgotten and input that was not sanitized.
Preparing for Translation
All text shown to users should be wrapped in translation functions such as __() and esc_html__(), with the correct text domain. Especially on multilingual sites, this saves a lot of work later on.
Correct Hook Usage
Scripts and styles should be loaded with wp_enqueue_script() and wp_enqueue_style(), on the appropriate hooks and only on the pages where they are needed. I sometimes see AI writing a script tag directly into the header or loading unnecessary files on all pages.
Using Current APIs
AI models may suggest deprecated functions. Check from official developer resources that the suggested functions are still valid in the current WordPress version. Deprecation notices that appear when you run the code with WP_DEBUG enabled also help you with this.
Ways to Get Better Code from AI
The quality of the assistant’s output depends largely on the quality of the instruction you give. In my own experience, the following approaches have made a clear difference:
- Provide context: State the target WordPress and PHP version, the theme you use, the relevant plugins and the project’s prefix.
- Ask for standards explicitly: Give clear instructions such as “write code that follows the WordPress coding standards, with sanitized inputs, escaped outputs, and nonce and capability checks”.
- Work in small pieces: Instead of having the whole plugin written at once, ask for features one by one and review each of them.
- Ask for explanations: Ask it to explain what each part of the code does. Inconsistencies between the explanation and the code reveal bugs.
- Have it audit its own code: Have the code it produced reviewed again in a separate request by saying “audit this code for WordPress security”. This second look often catches problems missed the first time.
Example: Insecure vs. Secure Code
To see the difference concretely, let us consider a simple settings-saving action. In the insecure version AI often produces, when the form is submitted the value in $_POST is passed directly to the update_option() function and then printed to the screen with echo. This code has no nonce check, no capability check, the input is not sanitized and the output is printed without escaping. In other words, four separate security problems come together in a single small function.
In the secure version, the process goes in this order: first the nonce is verified with check_admin_referer(), then the user’s capability is checked with current_user_can( ‘manage_options’ ), the incoming value is cleaned with wp_unslash() and sanitize_text_field(), saved with update_option(), and escaped with esc_html() when printed to the screen. The code only grows by a few lines, but the level of security changes completely. Keeping this difference in mind when having AI write code, and checking every output against these four steps, prevents almost all of the most common vulnerabilities.
Testing and Release Process
I never add any AI-written code directly to a live site. My process goes like this: the code is first tested in a local development environment with WP_DEBUG enabled, then goes through a standards check with PHP_CodeSniffer, is then tried on a staging environment under conditions close to real data, and finally is moved to the live site after a full backup has been taken. Using Git for version control lets me roll back quickly when a problem arises.
Frequently Asked Questions
Can I submit a plugin I wrote with AI to WordPress.org?
You can, but the plugin review team carries out a rigorous review in terms of security, prefix usage, input sanitization, output escaping and coding standards. Code written with AI often receives requests for corrections in this review, so do a thorough audit before submitting.
Is it safe to add code given by AI to functions.php?
Yes, if you understand the code, have run it through security checks and have tested it first. Otherwise you may make the site unusable or create a vulnerability. Using a child theme or a code snippets plugin prevents the code from being lost when the theme is updated.
For which tasks should I not trust AI?
In critical areas such as payment processing, user authentication, file uploads and personal data handling, you must have code produced by AI reviewed by an experienced developer.
Conclusion
AI coding assistants have created a real productivity revolution in WordPress development. But speed should never be an excuse for compromising on security. Input sanitization, output escaping, nonce and capability checks, prepared queries and coding standards are the basic points to check in every line written with AI. For secure, standards-compliant WordPress development support tailored to your site, you can reach me through my contact page.