View difference between Paste ID: 66hv36WC and
SHOW: | | - or go back to the newest paste.
1-
1+
function add_query_arg() {
2
        $ret = '';
3
4
        $homeurl = str_replace(( is_ssl() ? 'https://' : 'http://' ), '', get_bloginfo('url'));
5
6
        if ( is_array( func_get_arg(0) ) ) {
7
                if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
8
                        if ( get_bloginfo('url') != '' )
9
                                $uri = get_bloginfo('url') . $_SERVER['REQUEST_URI'];
10
                        else
11
                                $uri = $_SERVER['REQUEST_URI'];
12
                else
13
                        $uri = @func_get_arg( 1 );
14
        } else {
15
                if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
16
                        if ( get_bloginfo('url') != '' )
17
                                $uri = get_bloginfo('url') . $_SERVER['REQUEST_URI'];
18
                        else
19
                                $uri = $_SERVER['REQUEST_URI'];
20
                else
21
                        $uri = @func_get_arg( 2 );
22
        }
23
24
        if ( $frag = strstr( $uri, '#' ) )
25
                $uri = substr( $uri, 0, -strlen( $frag ) );
26
        else
27
                $frag = '';
28
29
        if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
30
                $protocol = $matches[0];
31
                $uri = substr( $uri, strlen( $protocol ) );
32
        } else {
33
                $protocol = '';
34
        }
35
36
        if ( strpos( $uri, '?' ) !== false ) {
37
                $parts = explode( '?', $uri, 2 );
38
                if ( 1 == count( $parts ) ) {
39
                        $base = '?';
40
                        $query = $parts[0];
41
                } else {
42
                        $base = $parts[0] . '?';
43
                        $query = $parts[1];
44
                }
45
        } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
46
                $base = $uri . '?';
47
                $query = '';
48
        } else {
49
                $base = '';
50
                $query = $uri;
51
        }
52
53
        wp_parse_str( $query, $qs );
54
        $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
55
        if ( is_array( func_get_arg( 0 ) ) ) {
56
                $kayvees = func_get_arg( 0 );
57
                $qs = array_merge( $qs, $kayvees );
58
        } else {
59
                $qs[func_get_arg( 0 )] = func_get_arg( 1 );
60
        }
61
62
        foreach ( (array) $qs as $k => $v ) {
63
                if ( $v === false )
64
                        unset( $qs[$k] );
65
        }
66
67
        $ret = build_query( $qs );
68
        $ret = trim( $ret, '?' );
69
        $ret = preg_replace( '#=(&|$)#', '$1', $ret );
70
        $ret = $protocol . $base . $ret . $frag;
71
        $ret = rtrim( $ret, '?' );
72
        $ret = str_replace($_SERVER['HTTP_HOST'], $homeurl, $ret);
73
        return $ret;
74
}