ap_sanitize_unslash( string|array $str, boolean|string $from = false, mixed $default_val = '' )

Description #

Sanitize and unslash string or array or post/get value at the same time.

Parameters #

  • $str
    string | array (Required) String or array to sanitize. Or post/get key name.
  • $from
    boolean | string (Optional) Get value from $_REQUEST or query_var. Valid values: request, query_var. Default value: false
  • $default_val
    mixed (Optional) Default value if variable not found. Default value: ''

Changelog #

VersionDescription
3.0.0Introduced.

Source #

File: includes/functions.php

function ap_sanitize_unslash( $str, $from = false, $default_val = '' ) {
	// If not false then get from $_REQUEST or query_var.
	if ( false !== $from ) {
		if ( in_array( strtolower( $from ), array( 'request', 'post', 'get', 'p', 'g', 'r' ), true ) ) {
			$str = ap_isset_post_value( $str, $default_val );
		} elseif ( 'query_var' === $from ) {
			$str = get_query_var( $str );
		}
	}

	if ( empty( $str ) ) {
		return $default_val;
	}

	if ( is_array( $str ) ) {
		$str = wp_unslash( $str );
		return array_map( 'sanitize_text_field', $str );
	}

	return sanitize_text_field( wp_unslash( $str ) );
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment