Make WordPress Core

Opened 4 years ago

Last modified 4 years ago

#51318 new enhancement

WP_Sitemaps_Provider protected $name property

Reported by: tkama's profile Tkama Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version: 5.5.1
Component: Sitemaps Keywords:
Focuses: Cc:



Why We need $name property to be protected? It is just name. This is inconvenient and can cause bugs. For example I create my Sitemaps Provider like this:

add_filter( 'init', 'wpkama_register_sitemap_providers' )
function wpkama_register_sitemap_providers(){

        require_once __DIR__ .'/class-Similar_Posts_Sitemaps_Provider.php';

        $provider = new Similar_Posts_Sitemaps_Provider();

        wp_register_sitemap_provider( 'similarposts', $provider );

And I can't get the value of $name property like this:


        $provider = new Similar_Posts_Sitemaps_Provider();

        wp_register_sitemap_provider( $provider->name, $provider );

But if first argument of wp_register_sitemap_provider() function and the $name prop are different the Sitemap Provider won't run at all!

So all I can to do is copy $name prop value and past it as parameter. And if I will change the name I need to remember to copy because they must be the same (and code itself doesn't tell me this dependency).

Change History (2)

#1 @dd32
4 years ago

While I'm not sure why it's marked protected, I'm just noting that if you want to make the $name property public, you can do so in the subclassed provider as PHP allows you to change the visibility, for example:

You could also call wp_register_sitemap_provider( $this->name, $this ) within the class through something like $provider = new Similar_Posts_Sitemaps_Provider(); $provider->register_provider(); I believe.

#2 @Tkama
4 years ago

Thanks, good solution! But maybe it's better to change it to "public" in main class.

Note: See TracTickets for help on using tickets.