Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#35287 closed defect (bug) (invalid)

get_site_url does not accept path parameter

Reported by: webdevdlo's profile WebDevDLo Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

I've tried this several times and am convinced this is a bug. site_url (which automatically echoes) works, but the sister function get_site_url (which I am using to create a javascript variable for use in AJAX) does not work. It is very easy to replicate the issue.

Here are a few lines to show you the problem I've run into. These are lines that I can get to work:

$.ajax({

url: <?php echo json_encode(get_site_url() . '/ajax-random-ad/'); ?>,

That is a work around. The proper way to write this is the following, which does not work:

$.ajax({

url: <?php echo json_encode(get_site_url('ajax-random-ad/')); ?>,

This is the first time I've submitted a bug so I'm not an expert on exactly what you would prefer for troubleshooting, but I am hoping this is enough for you to do a simple test of your own to replicate the problem.

I have searched the forums and I do not see this but reported yet.

Thank you.

Change History (4)

#1 @swissspidy
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version 4.4 deleted

Please have a look at the documentation for get_site_url(). The function takes $blog_id as the first parameter, and $path as the second.

So your code should probably look like get_site_url( null, '/some-path' ); — or just site_url( '/some-path' );. site_url() is just a wrapper for get_site_url(), always passing $blog_id = null

The same goes for get_home_url() and home_url().

#2 @WebDevDLo
9 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Hi @swissspidy -

Thank you for the prompt reply! That isn't quite the answer. The documentation for get_site_url() says the following about the $blog_id parameter. It is optional and defaults to null (current site), see below.

Parameters

$blog_id
(int) (Optional) Blog ID. Default null (current site).
Default value: null

So, the issue is that first parameter is not defaulted as specified. That's the bug. Does that make sense?

#3 @dd32
9 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

So, the issue is that first parameter is not defaulted as specified. That's the bug. Does that make sense?

When you call get_site_url('ajax-random-ad/') you are specifying the first parameter, so the defaults do not kick in. In order to specify the 2nd parameter, you need to specify the first, so get_site_url( null, '/ajax-random-ad/' ); or as pointed out, use site_url( 'ajax-random-ad/' ) directly instead.

#4 @WebDevDLo
9 years ago

Ah, it's the order of parameters. I get it now. What was really throwing me off was how site_url() worked without anything specified, but I see now in @swissspidy's response that blog_id is automatically passed with null in that wrapper function.

Thank you @dd32 and @swissspidy for the detailed answer!

Note: See TracTickets for help on using tickets.