Validation of variables in WordPress
Please disable ad blockerRead more
Validation of variables in WordPress is more than necessary to ensure maximum protection for your site. According to statistics, WordPress is one of the most, if not most, hacked CMS system. Of course, the main reason is that WordPress is the most popular CMS and there are basically more sites on it than on others. But also the developer is often neglected to protect their websites and their customers’ websites. Today we will talk about the four most necessary functions for variable validation.
One of the most common functions for variable validation is esc_attr (). All the variables that you display in the templates must be cleared with this function:
$text = ‘some text’;
echo esc_attr( $text );
To validate url use esc_url ():
$url = ‘https://an2-studio.xyz’;
echo esc_url( $url );
To safely work with databases, you need to validate the variable with esc_sql ():
$option = esc_sql( $key );
$wpdb-&amp;gt;get_var( "SELECT meta_value FROM table WHERE meta_key = ‘$option’";
esc_html () replaces html tags and returns formatted text, often used to output text in a template:
echo esc_html(‘Some text’, ‘text_domain’);
Do not forget to use the above functions to make your site as secure as possible.