When building WordPress plugins and themes for thousands of websites, be sure to handle data entering and leaving WordPress carefully. This tutorial will explore native functions for protecting, cleaning, and checking WordPress data, which is crucial in creating settings pages, HTML forms, manipulating shortcodes, and more.
What is data purification?
In short, data purification is to clean up user input. It removes text, characters, or code that are not allowed in the input.
Example: Gmail removes tags and contents from HTML messages before displaying them to prevent CSS from overwriting Gmail styles. WordPress widget titles do not allow HTML tags, and if any, will be automatically removed before saving the title.
WordPress provides multiple functions to purify different types of data:
sanitize_email()
: Removes characters that are not allowed in the email address. For example: sanitize_email("narayan prusty@sitepoint.com")
Output "narayanprusty@sitepoint.com"
. sanitize_file_name()
: Removes characters from the file name that may cause problems with the command line reference file. WordPress Media Uploader uses this function to purify media file names. For example: sanitize_file_name("_profile pic--1_.png")
Output "profile-pic-1_.png"
. sanitize_key()
: Options, metadata, and transient keys can only contain lowercase alphanumeric characters, dashes, and underscores. This function is used to purify the keys. For example: sanitize_key("http://SitePoint.com")
Output "httpsitepointcom"
. sanitize_text_field()
: Removes invalid UTF-8 characters, converts HTML-specific characters to entities, removes all tags, and removes line breaks, tabs, and extra spaces. WordPress uses this function to purify widget titles. For example: sanitize_text_field("<b>Bold</b>")
Output "Bold"
. sanitize_title()
: Removes PHP and HTML tags, as well as accents from strings. Convert space characters to dash. This function is used to generate slugs of articles/pages based on the article/page title, rather than purifying the title (purifying the title requires sanitize_text_field
). For example: sanitize_title("Sanítizing, Escaping and Validating Data in WordPress")
Output "sanitizing-escaping-and-validating-data-in-wordpress"
. What is data escape?
In short, data escape is to protect the output. This is done to prevent XSS attacks and ensure that the data is displayed as expected.
Data escape converts special HTML characters into HTML entities for display rather than execution.
Example: Facebook escapes chat messages when they display to ensure that users do not run code on each other's computers.
WordPress provides some functions to escape different types of data:
esc_html()
: Escape HTML specific characters. esc_textarea()
: When displaying text in the text area, use esc_textarea()
instead of esc_html()
because esc_textarea()
can double-encode entities. esc_attr()
: Encode ,
, &
, "
, and '
characters. It never double-encodes entities. This function is used to escape the value of HTML tag attributes. esc_url()
: The URL may also contain JavaScript code. Therefore, if you want to display a URL or a full <a></a>
tag, the href
attribute should be escaped, otherwise it may result in an XSS attack. esc_url_raw()
: Use this function if you want to store the URL in a database or for URL redirection. The difference between esc_url
and esc_url_raw
is that esc_url_raw
does not replace the versus and single quotes. antispambot()
: This function converts email address characters into HTML entities to block spam bots. What is data verification?
In short, data verification is about checking user input. This is to check whether the user has entered a valid value.
If the data is invalid, it will not be processed or stored. The system will ask the user to re-enter the value.
Example: When you create an account on a website, you will be asked to enter your password twice. The system will verify that the two passwords are the same.
HTML5 verification should not be relied on, as it is easily bypassed. Server-side verification is required before specific data is processed or stored.
WordPress provides some functions to verify certain types of data. Developers usually define their own functions for data validation.
is_email()
: Check whether the given string is an email address. is_serialized()
: Check whether the passed data is a string. Conclusion
We understand the concepts of data purification, verification and escaping and their importance. Be sure to include these functions when developing WordPress themes or plugins. Many plugins are not well developed and have no escaped output, which makes the website vulnerable to potential XSS attacks.
FAQ (FAQ)
This section contains frequently asked questions about data purification, escaping, and validation in WordPress, covering its importance, how to work, best practices, and how to use WordPress functions to implement these security measures.
The above is the detailed content of Sanitizing, Escaping and Validating Data in WordPress. For more information, please follow other related articles on the PHP Chinese website!