[wp-trac] [WordPress Trac] #58197: $blog_id always returns 0 in get_site_icon_url hook
WordPress Trac
noreply at wordpress.org
Thu Apr 27 20:50:16 UTC 2023
#58197: $blog_id always returns 0 in get_site_icon_url hook
--------------------------+------------------------
Reporter: revxx14 | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: 6.3
Component: Customize | Version: 4.4
Severity: normal | Resolution:
Keywords: needs-patch | Focuses: multisite
--------------------------+------------------------
Comment (by petitphp):
@revxx14
Yes it's confusing. Here `$blog_id` is not supposed to be equal to the
current blog id, but to the blog id we are retrieving the site icon for.
Here are some example with the different cases :
{{{#!php
<?php
// Assuming the examples are run on the blog `7`
// Case 1 : Retrieving the site icon url for the current site.
// Here the value of `$blog_id` will be `0` and the value of
`get_current_blog_id` will be `7`
$icon_url = get_site_icon_url();
// Case 2 : Retrieving the site icon url for the blog `12`.
// Here the value of `$blog_id` will be `12`and the value of
`get_current_blog_id` will be `7`
$icon_url = get_site_icon_url( 512, '', 12 );
// Case 3 : Retrieving the site icon url for the blog `7` (even if already
on the blog `7`).
// Here the value of `$blog_id` will be `7`and the value of
`get_current_blog_id` will be `7`
$icon_url = get_site_icon_url( 512, '', 7 );
}}}
The first case is the one used in WordPress core. That why you always have
the value `0` for `$blog_id` when hooking the filter. If you see `0` it
means we are getting the site icon url for the current site.
Here one possible way this could be handled
{{{#!php
<?php
function mytheme_set_site_icon_url(string $url, int $size, int $blog_id):
string {
// If `$blog_id` is `0`, fill it with the current blog id
if ( empty( $blog_id ) ) {
$blog_id = get_current_blog_id();
}
// Load the desired icon url
switch ( $blog_id ) {
case 7:
$url =
"https://example.com/7/my/custom/icon-{$size}x{$size}.svg"
break;
//...
}
return $url;
}
add_filter("get_site_icon_url", "mytheme_set_site_icon_url", 10, 3);
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/58197#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list