#35287 closed defect (bug) (invalid)
get_site_url does not accept path parameter
| Reported by: | WebDevDLo | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
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)
#2
@
11 years ago
- Resolution invalid
- Status closed → 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
@
11 years ago
- Resolution → invalid
- Status reopened → 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
@
11 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!
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Please have a look at the documentation for get_site_url(). The function takes
$blog_idas the first parameter, and$pathas the second.So your code should probably look like
get_site_url( null, '/some-path' );— or justsite_url( '/some-path' );.site_url()is just a wrapper forget_site_url(), always passing$blog_id = nullThe same goes for
get_home_url()andhome_url().