Optimize Syndication Frequenzy

WordPress-Christmas-20Today a standard becomes 9 years old. But hardly ever noticed: The Syndication-Module 1.4.1 of RDF Site Summary 1.0.

What is it good for?

Apparently some of you publishing posts less than 10 times per day. Shame on you ... not!
WordPress, however, assumes that you can not even stop, so write it in the RSS feed:

<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>

In plain English: Every feed reader should check once an hour, if you finally wrote something again.

Anyone who wants to relieve his server from such a thing, can change that. In the functions.php you write this two lines:

add_filter( 'rss_update_period',    create_function( '', 'return "daily";' ) );
add_filter( 'rss_update_frequency', create_function( '', 'return 4;' ) );

A few feed reader keep it by that but other don't. But even if there is only one: You've saved a few resources that you can use somewhere else.

Guest Post

This post is written by Thomas Scholz - toscho.de and is a post in our Advent Calendar on WP Engineer about WordPress.
Thank you very much from my part to Thomas.
If you also like to have your interesting post published on our website, please let us know on our contact page. Of course we will appreciate your contribution!

Display Private Posts to Logged in Users

WordPress-Christmas-15Sometimes I use the function of private posting in WordPress. These contributions can only see the user who has created the post, or the administrator. To show all these article to users who are logged into the system, you can go different ways. But I'd like to highlight just two different ways.

With custom fields

With the help of custom fields you create a private field; if you can see private posts, this field has true in the field. This field, we then query in the loop on output in the frontend.

if ( have_posts() ) :
	while ( have_posts() ) : the_post();
	// the loop
	$private = get_post_custom_values('private'); // read custom field
	if ( isset($private[0]) && $private == 'true' ) {
		if ( is_user_logged_in() ) {
			// display private post, only logged users
		}
	} else {
		// display public post, for every visitors
	}

	endwhile;
endif;

With capabilities

Another method is, to assign via function capabilities to the appropriate role. You can do this with the help of a Plugin, for example Members, or just with a code snippet in functions.php of the theme. You have to call the snippet just once, after that you can delete it, since the capability is saved in the data base.

Ad capabilities to read private posts read_private_posts to the role of the author.

function fb_add_cap2role() {
	global $wp_roles;
	
	$wp_roles->add_cap('author', 'read_private_posts');
}
add_action( 'init', 'fb_add_cap2role' );

Also you can also remove the rights again. This is possible with the following solution:

function fb_remove_cap2role() {
	global $wp_roles;
	
	$wp_roles->remove_cap('author', 'read_private_posts');
}
add_action( 'init', 'fb_remove_cap2role' );

Use WordPress Cron

WordPress-Christmas-14WordPress has its own cron to automatically and scheduled run certain themes. Therefore WordPress provides several functions to use the cron.

In our first example we send every hour a mail with the help of the WordPress function wp_mail(). FYI, this is just a possibility, please don't do it on your system!

As default, WordPress can handle 3 time keys, which you can call with the function wp_schedule_event.

// send automatic scheduled email
if ( ! wp_next_scheduled('my_task_hook') ) {
	wp_schedule_event( time(), 'hourly', 'my_task_hook' ); // hourly, daily and twicedaily
}

add_action( 'my_task_hook', 'my_task_function' );
function my_task_function() {
	
	wp_mail( 
		'example@yoursite.com', 
		'Automatic mail', 
		'Hello, this is an automatically scheduled email from WordPress.'
	);
}

If you use the cron in a Plugin or theme, then don't forget to deactivate the cron if you don't need it anymore.

delete_action( 'my_task_hook', 'my_task_deactivate' );
// clean the scheduler
function my_task_deactivate() {
	
	wp_clear_scheduled_hook( 'my_task_hook' );
}

But not always are 3 time values enough. Luckily you can expand the control via a filter.

add_filter( 'cron_schedules', 'filter_cron_schedules' );
// add custom time to cron
function filter_cron_schedules( $schedules ) {
	
	$schedules['once_half_hour'] = array( 
		'interval' => 1800, // seconds
		'display'  => __( 'Once Half an Hour' ) 
	);
	
	return $schedules;
}