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

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
    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 = '' ) {
	// If not false then get from $_REQUEST or query_var.
	if ( false !== $from ) {
		if ( in_array( strtolower( $from ), [ 'request', 'post', 'get', 'p', 'g', 'r' ], true ) ) {
			$str = ap_isset_post_value( $str, $default );
		} elseif ( 'query_var' === $from ) {
			$str = get_query_var( $str );
		}
	}

	// Return default if empty.
	if ( empty( $str ) ) {
		return $default;
	}

	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. Required fields are marked *

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

Add your comment