Europese Unie en hun WordPress blogs

De Europese Unie is blijkbaar niet erg up to date.. Ze gebruiken voor hun blogs een verouderde versie van WordPress, namelijk versie 2.9.1, terwijl op dit moment (van schrijven) versie 3.5.2 de nieuwste versie is.

Blijkbaar is up to date zijn niet zo belangrijk..

OldWordPressVersionEuropa

Protect wp-login.php with nginx for botnet that scans WordPress websites

With the recent news of a big botnet that attacks WordPress websites via wp-login.php was it time to prepare myself for the attack. wp.login.php is an uncached page that uses PHP resources which can slow down your website or cause errors.

My resolution: restrict access to wp-login.php with nginx. I’m only allowing the IP addresses that I can trust and anyone else will receive a 403 Forbidden error!

location ~* /wp-login.php$ {
allow INSERTYOURIPADDRESSHERE;
deny all;
# These lines are here to make sure that the page is loaded correctly after your IP has been verified. Change them to your own settings.
fastcgi_keep_conn on;
include fastcgi_params;
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_intercept_errors on;
# By all means use a different server for the fcgi processes if you need to
proxy_set_header Proxy-Connection "";
fastcgi_pass php5-fpm;
}

How to change WordPress default Email From Name and From Address

WordPress sends by default an email from wordpress@domainname.com with WordPress as sender name.

This can be adjusted easily via a filter.

Change the From Address

The filter will change the From address from wordpress@ to the value you specify.

function new_mail_from( $from_email ) {
$from_email = 'myownemail@domainname.com';
return $from_email;
}
add_filter( 'wp_mail_from', 'new_mail_from' );

Change the From Name

To change the From name:

function new_mail_from_name( $from_name) {
$from_name = 'my own sender name';
return $from_name;
}
add_filter( 'wp_mail_from_name', 'new_mail_from_name' );

You can put this code inside your functions.php file.

WordPress MP6 registers Google fonts apart from my theme

My theme (customized theme Esplanade on www.maartenvandekamp.nl) uses Google fonts. These fonts are registered and enqueued the right way in functions.php.

The new (secret!) plugin MP6 also uses Google fonts, but does not check if a user is logged in, so these fonts are always enqueued when a post of page is viewed, even when I’m logged out. In my case, a visitor downloads the same font twice, taking extra bandwidth and loading time.

You can recognize the MP6 stylesheet by the next line:

<link rel='stylesheet' id='open-sans-css' href='//fonts.googleapis.com/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&#038;subset=latin-ext%2Clatin&#038;ver=0.1' type='text/css' media='all' />

This bug (for me) is easy to fix.

Fix

Open WordPress Admin and navigate to the plugin editor ( Plugins > Editor ), select the MP6 plugin (mp6/mp6.php) and scroll down until you reach the following function:

function mp6_load_open_sans()

Add the following line – if ( is_user_logged_in() ) - above the line:

wp_register_style( 'open-sans', '//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,300,400,600&subset=latin-ext,latin', false, 0.1 );

The new code will be:


// load Open Sans
add_action( 'init', 'mp6_load_open_sans' );
function mp6_load_open_sans() {
if ( is_user_logged_in() ) 
    wp_register_style( 'open-sans', '//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,300,400,600&subset=latin-ext,latin', false, 0.1 );
    wp_enqueue_style( 'open-sans' );
}

The MP6 plugin will work as expected when a user / admin is logged in and when this is not the case, the Google fonts won’t be enqueued and no extra bandwidth will be wasted

Improved code – Show related posts in WordPress without a plugin

A while ago I posted an article about showing related posts in WordPress without the use of a plugin. Recently I have found better code for this and I would like to share it with you

<?php
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
$args = array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 5,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false
);
$tag_query = new WP_Query($args);
if( $tag_query->have_posts() ) : ?>
<h3 class="widget-title">You might also like:</h3>
<ul>
<?php while ($tag_query->have_posts()) : $tag_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Open <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
<?php endif; ?>

The difference with the previous code is that this code only retrieves the ID’s from the related tags, where the previous code retrieved all the information from the tags, to use only the ID from the array.

The code is cleaner and does not use any if anymore

Verhuizing website IhavetheKnowledge.nl

Zojuist heb ik de laatste grote website verhuisd naar mijn VPS. De verhuizing komt de snelheid van de website zeer ten goede. Hiervoor was de VPS een reverse caching proxy, wat inhoudt dat de VPS een week een gecachde versie van de website vasthield.

Het nadeel hiervan was dat de server na een week alle pagina’s opnieuw moest ophalen wat voor lange laadtijden zorgde. Nu is dat probleem opgelost.

Statistieken Maart 2013

Vorige maand kende een enorme groei in pageviews en ik wist dat deze groei niet constant zou blijven. In Maart is het inderdaad iets teruggelopen en dat vind ik helemaal niet erg. Het aantal bezoekers is wel goed gestegen en dat betekent dat de website een steeds breder publiek gaat aantrekken. Dat is ook groei!

Maand Bezoeken Pageviews Percentage bezoeken Percentage pageviews
Oktober 2012 2626 6734
November 2012 3268 8170 + 24.45% + 21.32%
December 2012 3864 10 332 + 17.5% + 26%
Januari 2013 4946 13 640 + 28% + 32%
Februari 2013 4962 16 139 + 0.14% + 18%
Maart 2013 5745 15 037 + 14.08% – 7.2%

Ik ben erg benieuwd wat April gaat brengen, of er op beide vlakken weer groei is, of dat de cijfers zich stabiliseren en een kleinere groei laten zien.

Problemen met email

Het lijkt erop dat de mailserver van KPN gemarkeerd staat als spammer op blacklists. Daardoor kunnen emailberichten niet goed worden verzonden waardoor er foutmeldingen teruggestuurd worden.

WordPress Archive Page with Transients

I recently discovered transients, a caching method that you can use to save queries in the database or object cache (if used). It can save a lot of time to gather the information for a page to load and it can descrease the amount of load on your database / PHP.

I use two files for this short tutorial. This code is used on the archive page for www.maartenvandekamp.nl/archief/. It’s generated once and only changed when a post is saved. This way, it’s only regenerated when needed.

<ul>
<?php
$posts = all_posts_archive_page();
if( $posts->have_posts() ) : while ($posts->have_posts() ) : $posts->the_post();
printf('<li>%1$s &mdash; <a href="%2$s" title="%3$s">%4$s</a></li>',
esc_html( get_the_date( get_option( 'date_format' ) ) ),
esc_url( get_permalink() ),
esc_attr( the_title_attribute( 'echo=0' ) ),
get_the_title()
);
endwhile; endif; wp_reset_query(); ?>
</ul>

I copied the template from an existing page and instead of showing the_content(). I pasted this code and saved the template. I called it template-archive.php.

You may have noticed, I fill the variable $posts with the function all_posts_archive_page();. This function is located in the file functions.php, which comes with every theme used by WordPress.

This is the code that retrieves the posts from the database and stores them in the database or object cache (if used):

function all_posts_archive_page() {
if ( false === ( $all_posts_for_archive = get_transient( 'all_posts_for_archive' ) ) ) {
$query = array( 'nopaging' => true );
$all_posts_for_archive = new WP_Query($query);
// transient set to last forever until another post is saved - all_posts_archive_page_transient_flusher takes care of the flush
set_transient( 'all_posts_for_archive', $all_posts_for_archive );
}
// do normal loop stuff
return $all_posts_for_archive;
}
/**
 * Flush out the transients used in all_posts_archive_page()
 *
 * 
 */
function all_posts_archive_page_transient_flusher() {
delete_transient( 'all_posts_for_archive' );
}
add_action( 'save_post', 'all_posts_archive_page_transient_flusher' );

The second function (all_posts_archive_page_transient_flusher()) deletes the transient when a new post is saved, so when a post is edited or published, the cache will become expired and will be rebuild when a visitor visits the archive page again.

Statistieken februari 2013

Er is weer een maand om en het was een korte maand deze keer, want het is februari.

De statistieken zijn wederom positief, behalve dan die van het aantal bezoeken. Omdat het maar 28 dagen zijn, is er groei van slechts 0.14% in het aantal bezoekers..

Het is merkbaar dat er 2 / 3 dagen ontbreken, anders had het aantal pageviews mogelijk 20% of hoger kunnen worden.

Maand Bezoeken Pageviews Percentage bezoeken Percentage pageviews
Oktober 2012 2626 6734
November 2012 3268 8170 + 24.45% + 21.32%
December 2012 3864 10 332 + 17.5% + 26%
Januari 2013 4946 13 640 + 28% + 32%
Februari 2013 4962 16 139 + 0.14% + 18%

Nieuwsbrief

Op de website kunnen mensen zich inschrijven voor de nieuwsbrief. Op dit moment zijn er 110 mensen die de nieuwsbrief ontvangen.