WordPress: How to reverse the comment order with paged comments

In the settings panel of the WordPress Dashboard you will find the Discussion Settings. These settings enable you to create paged comments. You can set the top level comments per page, if you want to show the first or last page by default and if the oldest or newest comments should be shown on the to of each page.

In the situation that you want to show the newest comments first, you can set that the last page should showed be default with the newest comments on the top of each page. These setting are also shown in the following figure:

discussion settings expected

When using these settings i found some unexpected behavior. If you set the number of comments per page to 10 and for instance the total number of comments on your page is 44, you will get the following situation: The default page contains the 4 newest comments, and the next 4 pages contain 10 older comments each. In this situation i expected the newest 10 comments on the first page and the oldest 4 comments on the fifth and last page.

This strange behaviour will be due to the setting which shows the latest page first. If you inspect the comment queries in wp-includes/comments-template.php you will find that all comments are loaded with the oldest first (ORDER BY comment_date_gmt). Pages are created with an instance of the Walker_Comment class. This walker_Comment class create each page the same way. Order per page can be set with the reverse_top_level Boolean option (if not set the option when calling wp_list_comments() in your template the oldest/newest setting from the Discussion Settings will be used). The reverse_top_level option will only set the order per page and not the order for all comments.

Solution: reverse the comments array

The order issue with paged comments can be solved by using a filters which reverse the comments array returned by the database query in wp-includes/comments-template.php. After querying the database to get the list of comments the code applies the comments_array filter. You use this filter to reverse the comments array by adding the following code to your function.php file:

add_filter('comments_array',function($comments){return array_reverse($comments);});

When using the filter described above the Discussion Settings can be set to show the first page by default and the oldest comments first as be shown in the following figure:

discussion settings improved

Notice that the setting to show the oldest comments first seems to be strange if you want to show the newest comments first. When this setting is set to “oldest” the reverse_top_level Boolean option will be set to false, which means that the results per page are not reversed. That’s right because of the order of the comments is already set from new to old after reversing the comments array by the filter.

After applying the comments_array function you should also have to change the text or images used by the previous_comments_link() and next_comments_link() functions. The next link should refer to older comments instead of newer comments and so the previous links should refer to the older comments.

Further improvements

It seems interesting to take a look at the HTML link elements rel="next" and rel="prev" to indicate the relation between the pages with the comments. Some SEO plugins set these links, it’s unclear how reversing the comments influence that for now.
Finally you will find that the content of the page will be repeated on every comment page now. Paging your comment with jQuery which doesn’t reload the page can be an intresting strategy to investigate too. The jPages pagination for WordPress plugin integrates pagination for comments.

WordPress and PageSpeed Insights

Today i tested my WordPress Theme on Google’s PageSpeed Insights. The result was a little disapointing. The Desktop version scored above 90 out of 100, but the mobile version scored around 80.

Based on Google’s suggestions i searched for some solutions.

Eliminate render-blocking JavaScript and CSS in above-the-fold content

Due to my own plugin Wp Defer loading there was no render-blocking JavaScript.

@import CSS child theme

My test site runs a child theme of JBST. The stylesheet of this child theme imports the stylesheet of it’s parent by @import url(../jamedo-bootstrap-start-theme/style.css);. Although appropriate course of action following Child Themes this is a performance issue.

In the case of JBST the parent style.css contains only some empty default styles. So for child themes (of JBST) remove this @import. Also notice for the parent style.css can’t be excluded by removing wp_enqueue_style( 'jbst-style', get_stylesheet_uri() ); cause some child themes will relay on it’s own style.css instead of only LESS code (custom.less).

Plugin’s stylesheets

My test site use a Question and Asnswer plugin, i found it’s style sheet included on my front page as well. On the front page this style sheet shouldn’t load.

Prevent Plugin’s from loading

By default WordPress loads all plugins (including CSS and JavaScript) on all pages. This also mean performance can be improved at two different stages: prevent the plugin from loading at all and prevent the style sheets and JavaScript from loading.

First a found the “Selective Loading Plugin”. This plugin looks promising, but seems not to work well or at least as expected. After add the class initialization which was missing, the settings interface won’t work too. I found some plugins not loading but overall the result was not good.

After this i investigate where plugins load in WordPress’s code. Plugins load in wp-settings.php, there are no hooks for this action defined. Also the old school (and deprecated!) my-hacks.php can not overwrite the loaded plugins.

The only way to prevent plugin from loading, i found, is edit wp-setting.php itself. Plugins are load by including through iterating an array with plugin paths:

// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin )
	include_once( $plugin );
unset( $plugin );

So removing paths from this array will prevent them from loading. For my test site i found the plugin path has q-and-a in its name and the plugin should only load if the request uri contains /knowledge-base/. Based on this data i could change the preceding code into:

// Load active plugins.
$plugins = wp_get_active_and_valid_plugins();
foreach($plugins as $key=>$value)
{
if(strstr($value,'q-and-a') &&  !strstr($_SERVER['REQUEST_URI'],'/knowledge-base/')){unset($plugins[$key]);}
}
foreach ( $plugins as $plugin )
        include_once( $plugin );
unset( $plugin );

The above works well. If you know what to code, you can code it effective. You also have to remember that wp-settings.php will be overwritten when upgrading WordPress.

I also found i can’t remove every plugin this way. For example removing WooCommerce cause problems with the shopping button which should be load on every page and requires WooCommerce (global $woocommerce). Maybe a reason to load all by default.

Prevent CSS and Javascript from loading

On How can I reduce the amount of files loaded/included per plugin? i found: “Current versions of WordPress allow for enqueuing later in the flow so you you can, for example, enqueue on your shortcode init/parse.” I will test this soon, cause it seems very useful for my own plugins too.

For now i start conditional dequeue CSS an Javascript i don’t need. For example for WooCommerce this can been done effective by adding some lines to functions.php:

if(function_exists('is_shop') && !is_shop())wp_dequeue_style( 'woocommerce_frontend_styles');

Overall i found some small code changes will have a huge performance profit.

Images

Pagespeed also mentioned: “Losslessly compressing” for some PNG files used. I found OptiPNG can resolve this easy.

Zou je leven er dan anders uitzien? #weekzondergoogle

Spruitjes zijn viesEerder deze week schreef ik al over de machtige positie van Google. In Europa gebruikt meer dan 95% van de mensen Google om te zoeken op internet. Google bepaalt dus ook welke antwoorden er beschikbaar zijn voor jouw dagelijkse vragen. Zoek jij en recept en vindt Google spruitjes vies, dan eten jij en je kinderen nooit meer spruitjes. Zou jouw (dagelijkse) leven er dus anders uit kunnen zien als jij je zoekgedrag aanpast?

Sommige Nederlandse websites ontvangen dus relatief veel bezoek vanuit Bing / Yahoo. Deze combinatie van laag zoekvolume met hoge conversie, lijkt een unieke kans voor seo-specialisten. Nu richt ik mij tot de consument, jij dus.

Zoek eens op Google en daarna met dezelfde zoekvraag op Bing. Zoals je ziet vaak totaal andere resultaten. Zou jij het aan durven, als voornemen voor 2013; zoek een week lang eens niet met Google. Thuis niet, onderweg niet en op je werk niet.

Google is toch de beste?

Niet altijd wordt de beste technologie de standaard, klassiek voorbeelden zijn de PC, VHS en het qwerty-toetsenbord. Via het “effect van positieve terugkoppeling” kunnen sommige producten toch de markt veroveren.

Maar google wil toch zelf ook de beste zoekresultaten?

Zou de zoekmachinemarkt volledig door vraag en aanbod bepaalt worden (klassieke theorie). Dan zou de consument inderdaad kiezen voor de zoekmachine met de beste zoekresultaten. Consumenten blijken veel minder kritisch en blijken ook in dit geval erg merktrouw.

Advertenties

Google verdient geld met advertenties. Deze advertenties worden gekoppeld aan de zoekopdrachten. Slechte zoekresultaten en relevante advertenties zullen Google op korte termijn dus meer opleveren. Zolang consumenten hier genoegen mee nemen en blijven terugkomen, is het belang van goede zoekresultaten voor Google steeds ondergeschikter.

Moraal

Google vindt spruitjes vies schreef ik al. Ook over andere zaken kan Google een mening hebben; ‘Naaktfoto’s moeilijker te vinden via Google’. Google zou deze ‘braafste jongetje van klas’-strategie bijvoorbeeld kunnen gebruiken om een positiever beeld te scheppen bij de mededingingsautoriteiten.

Alternatieven

In tegenstelling tot bijvoorbeeld Rusland (Yandex) en China (Baidu) ontbreekt in Europa sowieso aan een serieuze concurrent. Europese zoekmachines of lokale zoekmachines ( waar is Ilse gebleven ? ) zouden mogelijk een betere zoekervaring kunnen geven.

Bing / Yahoo is in de VS de een serieuze concurrentie voor Google. Ook in Europa zal meer concurrentie leiden tot betere zoekresultaten. Jij als consument bent daarin dus bepalend. Ga de uitdaging aan “een week zonder google” #weekzondergoogle.

Mag ik Bing wel vergeten?

David en Goliath door MichelangeloDeze week kwam google niet alleen met Google+ communities. Microsoft maakt haar social netwerk so.cl ook voor iedereen beschikbaar. Ik heb even rondgekeken op so.cl. In eerste instantie vond ik het veel op Pintrest lijken. Ook van Pintrest vraag ik mij ook nog steeds af wat ik daar zakelijk nou precies mee kan / moet. Er zijn wel mensen die daar ideeën over hebben. Ergens kan ik mij inderdaad wel voorstellen dat een restaurant bijvoorbeeld een foto van een lekker gerecht kan delen. Zelf lever ik meer diensten, die zou ik dan moeten (laten) uittekenen?

Andere vraagstelling dan: En wat wil Microsoft dan met so.cl? De zoekresultaten van Bing meer ‘author-led’ maken, een tegenhanger van Google Authorship dus.

Hoe belangrijk is dat? Om die vraag de beantwoorden zou je eerst moeten weten hoe groot Bing nu eigenlijk is. Ik heb gezocht naar cijfers over het marktaandeel van zoekmachines over 2012. Eigenlijk is er maar weinig te vinden. Marktaandeel zoekmachines in Nederland geeft de cijfers van 2011. Wel is duidelijk dat Google eigenlijk in heel Europa, inclusief Nederland oppermachtig is: Google nog steeds stevig in zadel in Europa. In de Verenigde Staten is het beeld al jaren anders: Bing wint marktaandeel in de Verenigde Staten.

In Nederland zou je Bing en Yahoo dus misschien maar liever vergeten. Anderzijds is het misschien juist een kans. Door juist ook aandacht te geven aan Bing en Yahoo, lukt het misschien uit dat kleine marktaandeel een hogere conversie te halen. Een site die in Google niet te vinden is heeft misschien wel een hoge ranking in Bing. Yahoo maakt gebruik van de technologie van Bing. Dus qua optimalisatie kan dezelfde aanpak gebruikt worden.

Bij gebrek aan cijfers heb ik naar een aantal van mijn eigen websites gekeken. Via Google analytics heb ik gekeken naar het percentage bezoekers dat binnenkomt via Bing:

linkotheek.nl

Google: 93.1
Bing: 3.4
Yahoo: 0.4

babykeur.nl
Google: 98.1
Bing: 0.6
Yahoo: 0.2

webvrouw.nl
Google: 90.0
Bing: 6.3
Yahoo: 1.3

Opvallend zijn de grote verschillen per website en het relatief grote aandeel Bing/Yahoo voor een website als webvrouw.nl. Een conclusie trekken zonder het marktaandeel Bing/Yahoo te kennen is moeilijk. In het geval van webvrouw zou je kunnen zeggen, de site doet het ‘slecht’ in Google, waardoor het aandeel Bing/Yahoo relatief groter wordt. Of precies andersom webvrouw.nl scoort goed voor Bing/Yahoo dat zou voor de andere websites ook haalbaar moeten zijn.

De genoemde websites zijn alle in het Nederlands en gericht op Nederland (België). Toch kan het zo zijn dat een site relatief veel buitenlandse bezoekers aantrekt via de zoekmachines door een hoge ranking op een Engels zoekwoord. Dit zou dan terug te zien moeten zijn in de kwaliteit van het verkeer. Er zijn wel kleine verschillen in de kwaliteit (pagina’s per bezoek, bezoekduur, bounce-rate) in het geval van webvrouw:

kwaliteit van het verkeer op webvrouw.nl

Het verkeer van Bing/Yahoo heeft een vergelijkbare kwaliteit. Deze bezoekers komen dus waarschijnlijk gewoon uit Nederland of zijn op zijn minst Nederlandstalig.

Andere verschillen tussen de website die mogelijk een rol spelen:

Doelgroep

De bezoekers van webvrouw.nl zullen (hopelijk) meer vrouwen dan mannen zijn. Dat zou echter in zekere mate ook op moeten gaan voor babykeur.nl

Technologie

Zowel babykeur.nl als webvrouw.nl zijn gebouwd met wordpress. Dat lijkt het verschil niet te zijn. Wat wel een duidelijke rol kan spelen. Babykeur maakt gebruik van de All in One SEO Pack plugin, terwijl op webvrouw.nl gebruik gemaakt wordt van de WordPress SEO plugin.
Tevens is op webvrouw.nl de afgelopen maanden via de WordPress SEO plugin veel aandacht besteed in het juist instellen van o.a. de links voor Google Authorship en bijvoorbeeld Twitter cards.

History

Er is een verschil het aantal jaren dat de website bestaat. De linkotheek bestaat sinds 1995, via startpunt.vuurwerk.nl en startpunt.cc sinds 2001 op de domeinnaam linkotheek.nl. Webvrouw.nl bestaat sinds 2003. Babykeur is pas sinds 2008 actief.

Bovenstaande vragen en cijfers zijn voor mij wel aanleiding komende weken nog eens verder naar Bing/Yahoo te kijken. Hoe staat het bijvoorbeeld met het aanbod van tools die vergelijkbaar zijn met de Google Webmaster Tools. Spelen sitemaps nog een rol? En in hoeverre maakt Bing/Yahoo reeds gebruik van bijvoorbeeld de rel=”author” metatags en bijvoorbeeld twitter cards?

Wat kunnen we uit Rusland halen?

Laatst las ik in de Nieuwsbrief van ICT-Kring Delft dat er binnenkort een delegatie van Russische en Wit-Russische ICT-bedrijven naar Nederland komt. Op 23 november zal er in Den Haag een matchmaking event plaats vinden met Nederlandse ICT-bedrijven. Meer informatie is te vinden op http://www.handelsbevordering.nl/activiteiten/4/2/758. Interessant vond ik dat Yandex ook aanwezig is. Zo veel we horen over Google, zo weinig horen we over Yandex. Toch behoort Yandex tot de vijf grootste zoekmachines ter wereld. Omdat Yandex zich vooral richt op Rusland, Oekraïne, Kazachstan, Wit-Rusland en Turkije, is er zelfs geen Nederlandstalige pagina over Yandex te vinden op Wikipedia.

Yandex heeft vast andere technieken en kan daarom leerzaam zijn in het uitdenken van content- en SEO-strategieën. Zelf ben ik er ook gewoon te vinden http://www.yandex.com/yandsearch?text=bass%20jobsen, waarbij bijvoorbeeld opvalt dat er een hoge score is voor comments en andere zaken waar mijn naam aan verbonden is. Duidelijk andere resultaten dan een zelfde zoekopdracht bij Google geeft.

Misschien moet ik eens een pagina in het Russisch maken, zoals ik eerder al eens deed in het Chinees.

Verder biedt Yandex ook nog andere diensten. Ook dat kan interessant zijn. @JellevanderBij ontdekte er vast al één: Yandex Metrica. Yandex Metrica is een websitestatistiekendienst. Yandex Metrica biedt ook gratis de mogelijkheid een on-page-analyse uit te voeren. Lees meer hierover op: On Page Analyse met de gratis tool: Yandex Metrica. Voor Yandex Metrica is ook een (beperkte) WordPress plugin beschikbaar.