Similarity plugin cooperating with qTranslate

This time the post will be very short. People using many plugins sometimes encounter a problem with compatibility. Unfortunately, require developers to take into other plugins when writing his own, is like require the developer to install Windows. Therefore, if we are having problems we are forced to create our own solutions.

Plugins making problems

On my blog I had some compatibility issues with plugins. qTranslate META was not compatible with All in One SEO Pack, and now the turn of the Similarity plugin, which has problem with the qTranslate plugin itself. It’s not easy to create multi-langual blog;) However, as You guess we will deal with this.

The main task of this entry is to preserve for being forgotten how to solve this problem. After one of the plugin will be updated I will not have to locate my patched files and overwrite the new – I will just reach this blog entry.

Similarity plugin issue

Using the qTranslate plugin entries titles are saved in a different way. For each language used special tags are added (based on HTML comments), and the whole title is written into one field.

<!–:pl–>PHP w trybie CGI – autentyfikacja HTTP – system logowania<!–:–><!–:en–>PHP in CGI version – HTTP Authentication – login system<!–:–>

Because of this record by getting the post title we get it in every used language. Unfortunately, a user who is visiting the Polish language site may have no desire to watch list of similar entries, where each entry is written in all the languages in which they occur. In the case of only two languages, yet it is not terrible problem, but misleading. What if, someone creates a larger site translated into dozens of languages?

Solution

In the case of creating such hooks the most ungrateful work is to rework someone else’s code and catch the relevant parts in it. In a case such as this, where the cause of incorrect operation of one of the plugins is known, the search for the solutions is easier. Basically, just find the code snippet for the Similarity plugin responsible for displaying links to related entries. In addition, you may be tempted to find a part of the qTranslate plugin which purifies link from unnecessary translations. Thanks to this combination will not need to write your own code, regular expressions, etc.. Bearing in mind that the need to use hooks for Similarity occurs only when using qTranslate we can safely attempt to re-use of its code.

In this particular case we have a facilitation, as the qTranslate plugin is run before the Similarity plugin, so the source of the first one are already loaded. So the creation of the solution is reduced to find and use the appropriate functions. The quest was hard, luckily editors help you keep track of the next function call (Eclipse F3 shortcut). Thanks to that just after a few hops I’ve discovered the right function from the qTranslate plugin.

The searched function is: qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($lang), interesting name 😉

The only thing left for us is to call this function in the right place in Similarity plugin. There are exactly two places (the original line of 200 and 281) in the code. Since the code can be executed from two places in code would be better to create a separate function, so, if necessary, changes can be replaced by anything else. We are interested in two lines 200 and 281 of file similarity/similarity.php. These are the lines that generate links to similar posts. The important variable in this code is $post->post_title, which is responsible for what user sees on the screen.

$impression = str_replace("{title}",$post->post_title,str_replace("{url}",get_permalink($list[$i]['post_id']),str_replace("{strength}",$list[$i]['strength'],str_replace("{link}","<a href=\"{url}\">{title}</a>",$output_template))));

Find the places for declaring $post variable (lines 143 and 215), add after those lines:

// line 143
$post = get_post($list[$i]['post_id']);
// added:
$post->post_title = qtranslate_post_title($post->post_title);
//(..)
// line 215
$post = get_post($list[$i]['post_id']);
// added:
$post->post_title = qtranslate_post_title($post->post_title);

Now we just call the appropriate function from the qTranslate plugin. This is done as follows:

1
2
3
4
5
6
7
8
9
10
11
/**
 * Hook for improve showing similar links
 * @author Vokiel | http://blog.vokiel.com/?p=598
 * @param string $title
 **/
function qtranslate_post_title($title){
	if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')){
		$title = wp_specialchars(qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($title));
	}
	return $title;
}

A simple condition checking whether a required function has been defined (loaded), if so, we improve the link, if not, the link is returned in the form it was. I hope someone will find this useful.

Voila!

 

Przeczytaj także

Komentarze: 1

Dodaj komentarz »

 
 
 

Dodaj komentarz

 
(nie będzie publikowany)
 
 
Komentarz
 
 

Dozwolone tagi XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

 
© 2009 - 2016 Vokiel.com
WordPress Theme by Arcsin modified by Vokiel