IT Consultant – Java, J2EE, IBM WebSphere Portal, Lotus Notes/Domino
RSS icon Home icon
  • Making the DD Sitemap Generator Plug-in work with multilingual blogs

    (2 votes) 1 Star2 Stars3 Stars4 Stars5 Stars
    Loading...
    Posted on 1 May 2008 Sebastian Thomschke*/?> 3 comments

    To create a bilingual blog with WordPress I am using the Language Switcher Plug-in. This plug-in allows you to put content in more than one language into the post title and content fields by surrounding the localized texts with special tags indicating their language (e.g. “[ lang_en]Hello World[ /lang_en][ lang_de]Hallo Welt[ /lang_de]”). When the posts are viewed in the browser the plug-in will determine the active language and strip off the texts specified in all other languages on the fly.

    I am using the great DD Sitemap Generator Plug-in for WordPress to automatically render a site map based on the posts, pages and categories created in my blog. The problem however is that the generated site map is not language aware and displays the page/post titles and category names including the tags and the texts of all languages at once.

    To solve this I started digging a bit into the code of WordPress and the Language Switcher Plug-in. The Language Switcher Plug-in achieves the on-the-fly processing of multilingual texts by hooking itself into the so called Filters API which is something like an event-based callback mechanism. Plug-ins can signal their interest in a certain text filter event by calling the add_filter(the_event_id, the_name_of_a_function_to_callback) function provided by the WordPress API to register one if it’s functions that will be invoked by WordPress when the filter event occurs. Besides registering for filter events, plug-ins can also fire such events by calling the apply_filters(the_event_id, the_text_to_be_filtered). WordPress will then apply all registered filters to the text passed over and the apply_filters will return a “filtered” version of the passed text. When rendering the title of a post or page WordPress fires the filter event “the_title” allowing plug-ins to process/modify the title before it is actually displayed. Therefore the Language Switcher Plug-in registers itself to this event and a lot of others to be able to strip off the language tags and the texts in languages other than the currently active one.

    Unfortunately the DD Sitemap Generator Plug-in is currently not applying the respective filters when rendering the site map. Therefore the Language Switcher Plug-in is not aware of it and as a result the site map shows all translations of a category name or page/post title at the same time no matter which language is currently active.

    To solve this problem I analyzed the source code of the DD Sitemap Generator Plug-in and added the missing apply_filters calls where appropriate. You can follow these steps in case you are having the same issue:

    1. Open the file <wordpress_root>/wp-content/plugins/sitemap-generator/sitemap-generator.php (or <wordpress_root>/wp-content/plugins/dd-sitemap-gen/dd-sitemap-gen.php) in an editor
    2. Locate the text
         $tmp_array[‘title’] = $pages[$k]->post_title;
      and replace it with
         $tmp_array[‘title’] = apply_filters(‘the_title’, $pages[$k]->post_title);
    3. Locate the text
         $tmp_array[‘title’] = $cat_data[$c][‘cat_name’];
      and replace it with
         $tmp_array[‘title’] = apply_filters(‘the_category’, $cat_data[$c][‘cat_name’]);
    4. Locate the text
         $tmp_array[‘title’] = $posts[$k]->post_title;
      and replace it with
         $tmp_array[‘title’] = apply_filters(‘the_title’, $posts[$k]->post_title);
    5. Save the file and you are done.

    If you are encountering similar problems with other plug-in you can use the same approach to make them play nicely with the Language Switcher Plug-in.

     

    3 responses to “Making the DD Sitemap Generator Plug-in work with multilingual blogs” RSS icon

    Leave a reply