urlencode_deep( mixed $value )

Navigates through an array, object, or scalar, and encodes the values to be used in a URL.


Parameters Parameters

$value

(mixed) (Required) The array or string to be encoded.


Top ↑

Return Return

(mixed) The encoded value.


Top ↑

Source Source

File: wp-includes/formatting.php

function urlencode_deep( $value ) {
	return map_deep( $value, 'urlencode' );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Razon Komar Pal

    Here is an example for string:

    echo urlencode_deep( 'foo bar' );
    
    // Output will be: foo+bar
    

    Here is an example for array:

    $data = urlencode_deep( array( 'foo' => 'bar long text', 'key1' => 'value1' ) );
    
    // Output will be for $data: array( 'foo' => 'bar+long+text', 'key1' => 'value1' )
    

    Here is a use case for multi word string as url parameter:

    echo add_query_arg( 'key', urlencode_deep( 'long value' ), 'http://example.com/' );
    
    // Output will be: http://example.com/?key=long+value
    

    Here is a use case for an array multi word string as url parameter:

    $params = array( 
        'key1' => 'value1 long text',
        'key2' => 'value2',
    );
    
    echo add_query_arg(
        urlencode_deep( $params ),
        'http://example.com/?'
    );
    
    // Output will be: http://example.com/?key1=value1+long+text&key2=value2
    

    It’s making spaces between word, if string has space:

    echo add_query_arg( 'key', 'long value', 'http://example.com/' );
    
    // Output will be: http://example.com/?key=long value
    

You must log in before being able to contribute a note or feedback.