php - Using $this with an anonymous function in filters

admin2025-06-05  0

Okay so I have a method within a class which looks like

$this->addToSitemap('page', $urls);

This is used to append extra pages to the Yoast Sitemap. Here 'page' is the type of sitemap within yoast and $urls are the new urls. This method looks like this

public function addToSitemap($type = '', $links = array())
{
    $CustomSitemaps = $this;
    add_filter( "wpseo_sitemap_{type}_content", function() use 
    (&$CustomSitemaps) { return $CustomSitemaps->parseLinks($links); } );
}

Now here I am using an anonymous function because I need to pass the $links variable to the callback.

I am using $CustomSitemaps = $this; because an anonymous function does not allow $this. But for some reason his is still not working. If i do this

public function addToSitemap($type = '', $links = array())
{
    $CustomSitemaps = $this;
    add_filter( "wpseo_sitemap_{type}_content", function() use 
    (&$CustomSitemaps) { var_dump($CustomSitemaps->parseLinks($links)); 
    die(); return $CustomSitemaps->parseLinks($links); } );
}

I see no nothin dumping on the screen. But if I do below $CustomSitemaps = $this; I do see it

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749056397a315982.html

最新回复(0)