2

I'm trying to concatenate my site_url and a string, but it doesn't work. This is what I'm doing:

$myurl = site_url();
var_dump($myurl); 

$url = "https" . $myurl . "/inbox/?fepaction=viewmessage&fep_id=" . $inserted_message->ID;

var_dump($url); die;

The output looks like this:

string(31) "//zgp.mydomain.be" string(78) "https:/inbox/?fepaction=viewmessage&fep_id=4813"

As you can see it isn't merged. How can this be?

1
  • 2
    What exactly are you trying to do? And why is there no http part in site_url()? It’s not default WP behavior... Commented Jul 31, 2018 at 14:22

3 Answers 3

2

Without knowing exactly what you are trying to do, it seems you want to append query variables to the URL. WordPress has methods for handling that properly, without manual string concatenation.

Look at the documentation for add_query_arg() for details: https://developer.wordpress.org/reference/functions/add_query_arg/

You can rebuild the URL and append query variables to the URL query by using this function. There are two ways to use this function; either a single key and value, or an associative array.

Using a single key and value:

add_query_arg( 'key', 'value', 'http://example.com' );

Would create http://example.com/?key=value

Using an associative array:

add_query_arg( array(
    'key1' => 'value1',
    'key2' => 'value2',
), 'http://example.com' );

This would create http://example.com/?key1=value1&key2=value2

1

Sometimes the easiest solutions are the best...

Take a look at site_url reference.

It takes two params:

$path (string) (optional) Path to be appended to the site url. Default: None

$scheme (string) (optional) Context for the protocol for the url returned. Setting $scheme will override the default context. Allowed values are 'http', 'https', 'login', 'login_post', 'admin', or 'relative'. Default: null

As you can see, you don't need all that code...

You can simply use:

$url = site_url('/inbox/?fepaction=viewmessage&fep_id=' . esc_url($inserted_message->ID), 'https');
0

What you are lookgin for is home_url().

This will return the address of the Site and not the path to your WP installation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.