首页 > 解决方案 > 如何在 OOP 单例中将参数传递给 add_feed

问题描述

当 WPML 处于活动状态时,我很难在每种语言中创建单独的页面提要(URL 列表)。

计划是为每种语言设置不同的提要名称,例如feed_name_enfeed_name_fr

我编写了以下函数,该函数遍历站点上的每种可用语言,并应为每种语言输出一个提要,其中仅包含特定语言的页面。

我发现了其他示例,这些示例展示了如何在add_action使用闭包中传递参数。(例如。add_action('init', function() use($param) { some_out_of_scope_function($param) } )

但在我的函数中,我收到以下错误:

类闭包的对象无法转换为字符串

我怀疑这是因为我使用 OOP 将函数封装在一个数组add_feed( $feedname, array( $this, ... ) )中。但我不知道如何解决这个问题。

我该如何处理?

$feedname = 'feed_name'; 

private function createLanguageFeeds(){

    foreach($this->wpml_languages as $lang){

        // add the lang to the feed name. example: feed_name_en
        $feedname = $this->feedname . '_' . $lang;

        // making sure that the feed is created
        if ( ! in_array( $feedname, $wp_rewrite->feeds ) ) {
            $wp_rewrite->feeds[] = $feedname;
            flush_rewrite_rules( FALSE );
        }

        add_feed( $feedname, array( $this, function() use ($lang) {$this->create_page_feed($lang);} ) );
    }
}

private function create_page_feed($lang){
    // echo pages
}

标签: phpwordpresswpml

解决方案


如果您使用的是 PHP 5.4 或更新版本,您只需将带有$lang参数的闭包传递给add_feed()函数,$this并保留对对象的引用:

add_feed( $feedname, function() use ($lang) {$this->create_page_feed($lang);} );

请注意,您的create_page_feed()方法需要public这样 WordPress 才能访问它:

public function create_page_feed($lang){
    // echo pages
}

(有关详细信息,请参阅公共、私有和受保护之间的区别是什么? )

这是一个经过测试和工作的演示插件,它使用单例模式注册一个提要:

<?php
/**
 * Plugin Name:       Singleton RSS Feed
 * Plugin URI:        https://cabrerahector.com
 * Description:       A demo RSS feed built using the Singleton pattern.
 * Version:           1.0.0
 * Author:            Hector Cabrera
 * Author URI:        https://cabrerahector.com/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       singleton-rss-feed
 * Domain Path:       /languages
 */

class Singleton_RSS_Feed
{
    /**
     * The unique instance of the plugin.
     *
     * @var Singleton_RSS_Feed
     */
    private static $instance;

    /**
     * Gets an instance of our plugin.
     *
     * @return Singleton_RSS_Feed
     */
    public static function get_instance()
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * Constructor.
     */
    private function __construct()
    {
        // Actions
        add_action('init', array($this, 'createFeed'));
    }

    /**
     * Register the feed.
     */
    public function createFeed()
    {
        global $wp_rewrite;

        $feedname = 'some_rrs_feed';
        $lang = 'en';

        // Making sure that the feed is created
        if ( ! in_array( $feedname, $wp_rewrite->feeds ) ) {
            $wp_rewrite->feeds[] = $feedname;
            flush_rewrite_rules( FALSE );
        }

        add_feed( $feedname, function() use ($lang) {$this->renderFeed($lang);} );
    }

    /**
     * Renders the feed.
     *
     * @param string $lang
     */
    public function renderFeed($lang)
    {
        header( 'Content-Type: application/rss+xml' );
        ?>
        <xml version="1.0" encoding="UTF-8">
        <rss version="2.0">
            <channel><title>RSS Feed in <?php echo $lang; ?> language</title></channel>
        </rss>
        <?php
    }
}

$Singleton_RSS_Feed = Singleton_RSS_Feed::get_instance();

提要将在此处提供:https://www.example.com/feed/some_rrs_feed


推荐阅读