Cross-site scripting, or XSS, lets an attacker run their JavaScript in your visitors' browsers through your website. Because the code runs on your trusted domain, it can act as the logged-in visitor, read what is on the page, capture keystrokes, redirect users or deface pages. It's a close cousin of SQL injection: both come from trusting user input.
How cross-site scripting works
A page is vulnerable when it takes data from outside (a comment, a search term, a URL parameter, a product review) and puts it into the HTML without encoding it for the place it lands. The browser can't tell the attacker's markup from yours, so a <script> tag or an event handler such as onerror= becomes part of the page and runs with your site's full trust.
The browser's same-origin policy normally stops one site's scripts from reading another site's data. XSS gets around it completely, because the script really does come from your site.
The three types of XSS
- Reflected XSS: the malicious script is in a link or request and is reflected straight back in the response. The victim has to click a crafted link, often sent by email or chat.
- Stored XSS: the script is saved on your site (in a comment, a profile, a review) and runs for every visitor who sees that content. This is the most dangerous kind, because nobody has to click anything.
- DOM-based XSS: the vulnerability is in the site's own JavaScript, which reads something like the URL fragment and writes it into the page with
innerHTMLordocument.write. The server may never see the payload.
Why XSS is so dangerous on WordPress
The attacker's code runs with the same trust as your own. If an administrator opens a page with stored XSS, the script runs in the administrator's session and can send the same requests the admin screens send, for example to add a new administrator account or install a plugin. That turns one unescaped field into full control of the site.
On an e-commerce site, injected JavaScript on the checkout page can copy card details as customers type them. This is how card-skimming attacks on online shops work.
WordPress marks its login cookies HttpOnly, so a script can't simply read them. That doesn't make XSS harmless: the script doesn't need the cookie when it can act inside the logged-in browser.
A realistic example: a stored XSS in a review form
A plugin adds a "Your review" box to product pages and prints each saved review with echo $review->text;. An attacker submits a review that contains an image tag with a broken source and an onerror handler holding their code. Nothing looks wrong on the page, because the image just fails to load. But every visitor's browser runs the handler, and when a shop manager checks the new reviews from the dashboard, the script runs in the manager's session too.
The fix is one function: printing the review with esc_html( $review->text ) turns the attacker's markup into harmless text.
How to prevent cross-site scripting
- Escape output. Whenever you print data, escape it for the context. In WordPress, use
esc_html()for text,esc_attr()for attribute values andesc_url()for links. Usewp_kses_post()when some HTML is allowed, andwp_json_encode()for data you pass to JavaScript. - Sanitize input. Clean data before you store it, with functions such as
sanitize_text_field()orsanitize_textarea_field(). Sanitizing reduces risk but never replaces escaping on output. - Use a Content Security Policy (CSP). This header limits which scripts the browser will run. See how to roll out CSP and other security headers.
- Keep plugins updated. As with most attacks, plugins are the usual entry point, and XSS is one of the flaws most often fixed in plugin updates.
- Run a WAF. A web application firewall blocks common XSS patterns in requests.
- Limit who can post HTML. In WordPress only administrators and editors have the
unfiltered_htmlcapability on a single site. Don't give those roles to accounts that don't need them.
Common mistakes
- Escaping on input instead of output. Data escaped for HTML when it is saved can still be dangerous when it is later used in an attribute, a URL or a script. Escape at the moment you print, for that exact place.
- Using
strip_tags()as the defence. Removing tags doesn't help inside attributes or JavaScript, and it mangles legitimate text. - Trusting data from logged-in users. Subscriber and customer accounts are easy to create. Their input needs the same escaping as anyone else's.
- Relying on a CSP alone. A policy that allows inline scripts or
unsafe-eval, which many themes need, stops very little. CSP is a second layer, not the fix. - Forgetting the admin screens. A value shown only in the dashboard is still a stored XSS, and it runs for your most powerful users.
What a firewall and scanners can't do
A firewall recognises known attack patterns in requests. It can't remove the vulnerable code, and an attacker can sometimes find an encoding that a rule doesn't expect. DOM-based XSS that lives in the URL fragment never reaches the server at all. Scanners find injected JavaScript after it has been stored, not the missing escaping that let it in. Treat these tools as a safety net, and fix the code or update the plugin.
How Silent Threads helps
Silent Threads' firewall blocks reflected XSS attempts on every plan, its malware and database scanners look for injected JavaScript, and the JS & Card-Skimmer Scanner (Pro) looks specifically for card-skimmer code in your pages.
The short version
XSS happens when untrusted data reaches a page without being escaped for where it lands. Escape every output, sanitize input, keep plugins updated, add a CSP once you know what your site loads, and use a firewall and regular scans to catch what slips through.




