Shortcodes and HTML blocks make it easy to add buttons, forms, tables, and dynamic content to pages. However, any mechanism that accepts or outputs HTML can become an entry point for an XSS attack—especially when data comes from authors, editors, visitors, or third-party integrations.
The core rule for working securely with WordPress is this: sanitize data when it is received or saved, and escape it immediately before output. These are separate stages, and one does not replace the other. Below is a practical framework for checking shortcodes, HTML in Elementor, and user permissions.
Why a Shortcode Can Become an XSS Vulnerability
A shortcode is not inherently dangerous. The risk lies in its callback function—the code that processes attributes and generates HTML for the page. If the callback outputs attribute values without validation, an attacker may pass JavaScript or unsafe HTML attributes.
The problem is especially serious when users other than administrators can edit posts. If a user with elevated permissions inserts malicious code into a post, it may execute in the browsers of other administrators or visitors. This is known as stored XSS—a cross-site scripting attack in which the malicious content is stored.
The admin area should not be considered automatically safe. Data from settings fields, custom fields, imports, APIs, forms, and shortcodes can reach the front end, emails, or a JavaScript context.
- Do not output shortcode attributes without esc_attr().
- Do not output ordinary text without esc_html().
- Do not allow arbitrary HTML unless there is a clearly defined need for it.
- Check not only pages, but also widgets, archives, templates, and AJAX responses.
Separate Sanitization from Escaping
Sanitization cleans or normalizes data when it is received or saved. For example, sanitize_text_field() can be used for a short text field, esc_url_raw() for a URL, and absint() for a number. Sanitization reduces the risk of storing invalid data, but it does not eliminate the need for escaping.
Escaping is performed at the time of output and depends on the context. Text, an HTML attribute, a URL, and a JavaScript context require different functions. There is no single universal function that can be safely applied everywhere.
If a field must contain a limited subset of HTML, use wp_kses() or wp_kses_post() immediately before output. The set of allowed tags should reflect the actual requirement rather than being as broad as possible.
- Ordinary text: esc_html().
- HTML attribute value: esc_attr().
- URL in HTML: esc_url() during output and esc_url_raw() when saving.
- Limited HTML in post content: wp_kses_post() or wp_kses() with a custom list of tags.
- Numeric values: validate the type, for example with absint(), rather than simply escaping the string.
Example of a Secure Shortcode Callback
In the example below, the title is output as text, the URL as a link attribute, and the description as permitted HTML. It is also important not to trust shortcode attributes: they must be normalized and restricted to permitted values.
function webhva_button_shortcode( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'url' => '',
'title' => 'Перейти',
),
$atts,
'webhva_button'
);
$url = esc_url( $atts['url'] );
$title = esc_html( $atts['title'] );
$text = wp_kses_post( $content ?? '' );
if ( empty( $url ) ) {
return '';
}
return sprintf(
'<a class="button" href="%1$s" aria-label="%2$s">%3$s</a>',
$url,
$title,
$text
);
}
Work Safely with Shortcode Attributes
Shortcode attributes are user input, even when a field appears to be part of a template. Do not insert them into HTML through unprocessed concatenation. You should also not allow attributes to define arbitrary CSS classes, file names, URLs, or query parameters without an allowlist.
For values with a limited set of options, use an allowlist. For example, a button size might be limited to small, medium, or large. For URLs, check the scheme and do not allow unsafe protocols such as javascript:.
If a shortcode accepts a numeric post ID, convert it to an integer and check whether the current user is authorized to view the corresponding content. Escaping does not replace authorization checks.
- Set default values with shortcode_atts().
- Check the type and permitted range of every parameter.
- Use an allowlist for modes, styles, and statuses.
- Do not output unprocessed attributes in style, onmouseover, onclick, or other event attributes.
- Do not construct SQL queries, shell commands, or file names without separate validation.
Why Passing User Input Directly to do_shortcode() Is Dangerous
do_shortcode() is intended to process a string that already contains controlled shortcode content. If arbitrary user text is passed to it, that text may be interpreted as a shortcode. As a result, a user may gain access to functionality that the developer did not intend to expose in that context.
The risk depends on the shortcodes that are registered. One of them may make an external request, display private data, create a form, or change the state of the system. Even if this does not directly lead to XSS, it creates an unauthorized-functionality problem.
If shortcode processing is genuinely required, first restrict the data source and context. For ordinary text, use esc_html() rather than do_shortcode(). For controlled editorial content, use wp_kses_post() and allow shortcodes only for users with the appropriate permissions.
- Do not pass values from public forms to do_shortcode() without clear validation.
- Do not allow users to choose an arbitrary shortcode name.
- Do not use do_shortcode() as a way to “render any text.”
- Review third-party shortcodes that may be invoked through editorial content.
WordPress Roles and HTML in Elementor
HTML and JavaScript content should be available only to users you genuinely trust. In WordPress, the ability to insert unrestricted HTML is associated with the unfiltered_html capability. Do not grant it to authors and editors without a specific need: if such an account is compromised, an attacker may be able to save dangerous code.
In Elementor, the HTML widget and other elements that allow arbitrary code insertion should be restricted to administrators or a specifically designated group of trusted users. The exact method of control depends on the WordPress, Elementor, and additional role-management configuration, so check not only the editor settings but also the actual permissions of a test user.
It is important to understand that changing a role does not clean already stored content. After changing permissions, check old pages, templates, global widgets, popups, and other locations where HTML or JavaScript may remain.
- Apply the principle of least privilege: users should receive only the permissions required for their work.
- Check the roles of authors, editors, store managers, and users with custom capabilities.
- After changing roles, test the creation, editing, and publication of HTML content.
- Review active accounts periodically and remove unnecessary permissions.
- Do not insert third-party scripts into HTML widgets without checking their source and purpose.
How to Check Shortcodes and HTML in Practice
Testing should involve more than reading the code; perform it in a test environment as well. Create a separate account with the author or editor role and check whether it can insert, save, and publish content. Then open the page in another browser or private-browsing mode.
For each parameter, test an ordinary value, quotation marks, HTML tags, JavaScript events, URLs with unsafe protocols, and very long strings. Do not use malicious payloads on a live site: perform testing on a copy or in a staging environment.
Repeat the audit after plugin updates, role changes, or site migrations. Also check content created before sanitization was implemented: old data may remain in the database in an unsafe form.
- Find all shortcode registrations and review their callback functions.
- Check whether escaping is performed in the same place where the HTML is generated.
- Review settings fields, custom fields, imports, and AJAX handlers.
- Check Elementor content, including templates and global elements.
- Make sure input errors do not display unprocessed user text.
- After testing, delete demonstration posts and revoke temporary permissions.
Common Developer Mistakes
The most common mistake is sanitizing a value once during saving and assuming it will remain safe forever. Data may be changed by other code, imported from a database, or output in a different context. Escaping must therefore remain immediately before output.
Another mistake is using esc_html() for HTML that needs to be preserved. This function converts tags into text. Conversely, wp_kses_post() is not a replacement for esc_attr() when a value is inserted into an attribute.
- Using one escaping function for every context.
- Trusting data simply because it came from the admin area.
- Granting unfiltered_html to all editors “for convenience.”
- Dynamically constructing shortcodes from user input.
- Checking only new content without auditing older pages.
Frequently asked questions
Is using wp_kses_post() enough to protect all shortcodes?
No. wp_kses_post() is suitable for permitted HTML in text content, but it does not replace esc_attr() for attributes, esc_url() for URLs, access-control checks, or data-type validation.
Can editors be allowed to insert HTML in WordPress?
Only if this is justified by the workflow and the users can be trusted. It is better to provide limited capabilities and deny unfiltered_html to roles that do not need arbitrary HTML or JavaScript.
Is text containing shortcode syntax itself dangerous?
Text is not necessarily dangerous, but passing it to do_shortcode() may activate registered shortcodes. If the text needs to be displayed as text, output it with esc_html() rather than processing it as a shortcode.
Where should the Elementor HTML widget be restricted?
Restrictions should be applied through roles and the editor capabilities available to them, leaving arbitrary HTML only for administrators or a separate trusted group. Then verify access with a test user and review already stored templates.
Do data fields need to be escaped if they are available only in the admin area?
Yes. Admin-area data may reach the front end, emails, REST responses, or other contexts. Sanitization when saving and escaping when output must be performed independently, regardless of who fills in the field.
Conclusion
Secure shortcodes are not built around a single function; they depend on a proper access-control and data-processing model. Restrict roles, do not allow unrestricted HTML without a clear need, validate attributes, and use context-appropriate escaping.
For WordPress and Elementor, regular audits of existing content, control of HTML widgets, and avoiding arbitrary input in do_shortcode() are especially important. Sanitize data when saving, escape it when outputting, and test the site from the perspective of a user with minimal permissions.