使用“All in One SEO Pack”插件挂钩生成动态网站地图

时间:2014-04-02 作者:Pietro

除了帖子、页面和类别的URL之外,我还需要我的站点地图包含各种页面的URL,这些页面显示来自WP数据库(Rails API)以外的外部源的数据。

是否有一个挂钩,我可以添加一个过滤器,以便在动态生成站点地图后,我可以将这些其他URL附加到站点地图。

附言:我有一个建议aiosp_sitemap_post_filter, 但没有关于它如何工作的文件。

谢谢

UPDATE

具有aiosp_sitemap_post_filter 您只能将其他帖子添加到网站地图,而不能添加自定义内容。

3 个回复
最合适的回答,由SO网友:Pietro 整理而成

这是solution 来自Peter Baylies(插件作者)

add_filter( \'option_aioseop_options\', \'itdge_sitemap_addl_pages\' );

function itdge_sitemap_addl_pages( $option ) {
    if (   !empty( $option )
        && !empty( $option[\'modules\'] )
        && !empty( $option[\'modules\'][\'aiosp_sitemap_options\'])
      ) {
        $my_sitemap_entries = Array(
        \'/test1\' => Array( \'prio\' => \'0.1\', \'freq\' => \'daily\', \'mod\' => \'2014-04-01\' ),
        \'/test2\' => Array( \'prio\' => \'0.3\', \'freq\' => \'weekly\', \'mod\' => \'2014-03-02\' )
        );
        if ( empty( $option[\'modules\'][\'aiosp_sitemap_options\'][\'aiosp_sitemap_addl_pages\'] ) ) {
            $option[\'modules\'][\'aiosp_sitemap_options\'][\'aiosp_sitemap_addl_pages\'] = Array();
        }
        foreach( $my_sitemap_entries as $k => $v ) {
            $option[\'modules\'][\'aiosp_sitemap_options\'][\'aiosp_sitemap_addl_pages\'][$k] = $v;
        }
    }
    return $option;
}

SO网友:Pietro

这不是我问题的解决方案,但它帮助我在Wordpress中解决我的问题,我想与大家分享。

我使用XMLWriter创建了一个单独的站点地图。这是代码。

Wordpress挂钩:

add_action(\'parse_request\', \'my_custom_url_handler\');

function my_custom_url_handler() {
    $c = new EventsSitemapBuilder();
    if( $_SERVER["REQUEST_URI"] == \'/website/events-sitemap.xml\') {
        header("Content-type: text/xml");
        $c = new EventsSitemapBuilder();
        $c->build();
    }
    exit;
}
EventsSitemapBuilder():

class EventsSitemapBuilder {

        private $api_client;
        private $sitemap_data;
        private $sm_xml_writer;


        public function __construct() {
            $this->api_client = new ALApiClient();
            $this->sitemap_data = $this->api_client->sitemap_data();
            $this->sm_xml_writer = new XMLWriter();
        }


        public function build() {
            //events.id, it_texts.slug AS it_slug, en_texts.slug AS en_slug, events.updated_at
            //$this->sm_xml_writer->openUri( home_url( "events-sitemap" ) );
            $this->sm_xml_writer->openUri( "php://output" );
            $this->sm_xml_writer->startDocument(\'1.0\', \'UTF-8\');
            $this->sm_xml_writer->startElement(\'urlset\');
            $this->sm_xml_writer->writeAttribute(\'xmlns\', "http://www.sitemaps.org/schemas/sitemap/0.9");

            array_map( array( $this, \'sitemap_event_entry\' ), $this->sitemap_data );

            $this->sm_xml_writer->endElement();
            $this->sm_xml_writer->endDocument();
            $this->sm_xml_writer->flush();
        }


        private function sitemap_event_entry( $data ) {
            $this->sm_xml_writer->startElement(\'url\');
            $this->sm_xml_writer->writeElement(\'lastmod\', $data[\'date\']);
            $this->sm_xml_writer->writeElement(\'changefreq\', \'weekly\');
            $this->sm_xml_writer->writeElement(\'priority\', 0.6);
            $event_url = home_url( \'events/\' . $data[\'id\']  );
            $this->sm_xml_writer->writeElement(\'loc\', $data[\'url\'] );
            $this->sm_xml_writer->endElement();
        }
    }

SO网友:Vishnja

而且事实证明option_aioseop_options 钩子在任何页面加载时触发(甚至多次)。我在Pietro的解决方案中添加了几行代码来检查sitemap是否正确。请求xml:

add_filter( \'option_aioseop_options\', \'itdge_sitemap_addl_pages\' );

function itdge_sitemap_addl_pages( $option ) {
    $uri = $_SERVER[\'REQUEST_URI\'];
    $uriSegments = explode(\'/\', $uri);
    if ($uriSegments[ count($uriSegments) - 1 ] != \'sitemap.xml\') return $option;


    if (   !empty( $option )
        && !empty( $option[\'modules\'] )
        && !empty( $option[\'modules\'][\'aiosp_sitemap_options\'])
    ) {
    ...
}

结束

相关推荐

为什么不调用/触发“Plugins_Load”?

我正在打电话load_plugin_textdomain 然而,一旦加载了插件,就不会发生这种情况。我确实激活了一个插件,所以这不应该触发吗?add_action(\"plugins_loaded\", \"test_override\"); function init_localization() { echo \"init_localization<br>\"; load_plugin_textdomain (&#x