$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Using $this with an anonymous function in filters|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Using $this with an anonymous function in filters

matteradmin10PV0评论

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

Post a comment

comment list (0)

  1. No comments so far