Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

|
//

WordPress WPML Kommentar Filter Plugin

28.6.2010 | 2 Minuten Lesezeit

Dieser Beitrag ist etwas „off-topic“. Kürzlich ist unser Blog von qTransate auf WPML für das zweisprachige Bloggen migriert worden. Hauptgrund waren die erheblich besseren Updates und eine sauberere Trennung von den verschiedenen Sprachen.
Ein Feature hat uns dabei aber gefehlt: Unter beiden Postings in Englisch und Deutsch wollen wir alle Kommentare anzeigen, nicht nur die Kommentare in der jeweiligen Sprache. WPML erlaubt dies von sich aus leider nicht, jedoch konnten wir dies erstaunlich einfach selbst programmieren.

Da wir an Open Source glauben hier der Quelltext unseres Kommentar-Merge Plugins. Wenn man wollte könnte man die Sprachen noch parametrisieren, aber für unsere Zwecke war dies absolut ausreichend.

WPML comment merging Plugin auf wordpress.org

1<?php
2/*
3Plugin Name: WPML comment merging
4Plugin URI: http://wordpress.org/extend/plugins/wpml-comment-merging/
5Description: This plugin merges comments from all translations of the posts and pages, so that they all are displayed on each other. Comments are internally still attached to the post or page they were made on.
6Version: 1.3
7Author: Fabian Lange
8Author URI: http://blog.codecentric.de/en/2010/06/wordpress-wpml-comments-filter-plugin/
9License: MIT
10*/
11 
12function sort_merged_comments($a, $b) { 
13    return $a->comment_ID - $b->comment_ID;
14}
15 
16function merge_comments($comments, $post_ID) {
17    // get all the languages for which this post exists
18    $languages = icl_get_languages('skip_missing=1');
19    $type = is_page($post_ID) ? 'page' : 'post';
20    foreach($languages as $l) {
21        // in $comments are already the comments from the current language
22        if(!$l['active']) {
23            $otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
24            $othercomments = get_comments( array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC') );
25            $comments = array_merge($comments, $othercomments);
26        }
27    }
28    if ($languages) {
29        // if we merged some comments in we need to reestablish an order
30        usort($comments, 'sort_merged_comments');
31    }
32    return $comments;
33}
34 
35function merge_comment_count($count, $post_ID) {
36    // get all the languages for which this post exists
37    $languages = icl_get_languages('skip_missing=1');
38    $type = is_page($post_ID) ? 'page' : 'post';
39 
40    foreach($languages as $l) {
41        // in $count is already the count from the current language
42        if(!$l['active']) {
43            $otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
44            if($otherID) {
45                // cannot use call_user_func due to php regressions
46                if ($type == 'page') {
47                    $otherpost = get_page($otherID);
48                } else {
49                    $otherpost = get_post($otherID);
50                }
51                if ($otherpost) {
52                    // increment comment count using translation post comment count.
53                    $count = $count + $otherpost->comment_count;
54                }
55            }
56        }
57    }
58    return $count;
59}
60 
61add_filter('comments_array', 'merge_comments', 100, 2);
62add_filter('get_comments_number', 'merge_comment_count', 100, 2);
|

Beitrag teilen

Gefällt mir

0

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.