Turn pages or posts back to draft with a custom cron

WordPress runs its own cron events by default. We can hook into this to run our own cron scripts, a useful one of which is to schedule some posts to revert to draft status. 

/**
         * Add a new time to the Cron Scheduler
         */
        add_filter('cron_schedules', 'cron_add_schedules');
        
        function cron_add_schedules($schedules) {
                # Adds once weekly to the existing schedules.
                $schedules['weekly'] = array(
                        'interval' => 604800,
                        'display' => __('Once Weekly')
                );
                
                # Adds 5minutes to the existing schedules
                $schedules['every_five_minutes'] = array(
                        'interval' => 300,
                        'display' => __('Every 5 Minutes')
                );
                
                # Add every minute schedule
                $schedules['every_minute'] = array(
                        'interval' => 60,
                        'display' => __('Every Minute')
                );
                
                return $schedules;
        }
        
        /**
         * Add a scheduled event
         */
        if(!wp_next_scheduled('draft_old_posts')) {
                wp_schedule_event(time(), 'every_minute', 'draft_old_posts');
        }
        
        /**
         * This function takes published posts and changes
         * them to drafts
         */
        function draft_old_posts_function() {
                $query = new WP_Query(array(
                        'post_type' => 'post',
                        'post_status' => 'any'
                ));
                
                if($query->have_posts()) {
                        while($query->have_posts()) {
                                $query->the_post();
                                
                                $current_post = get_post($post->ID, 'ARRAY_A');
                                $current_post['post_status'] = 'draft';
                                wp_update_post($current_post);
                        }
                }
        }
        
        add_action('draft_old_posts', 'draft_old_posts_function');
        
        /**
         * Unschedule an Event
         */
        if(false !== ($time = wp_next_scheduled('draft_old_posts'))) {
                wp_unschedule_event($time, 'draft_old_posts');
        }

 

 

Adding in a custom link to the WP Admin bar

This is a lovely little snippit that allows a user to add in a custom link to the WP admin bar. This is handy if you want to put a link in to, say, support, or a help directory. Simply plonk this snippit into your functions.php file…

 

function custom_adminbar_menu($meta = TRUE) {
global $wp_admin_bar;

if(!is_user_logged_in()) {
return;
}

if(!is_admin_bar_showing()) {
return;
}

$wp_admin_bar->add_menu(array(
'id' => 'YOUR ID',
'title' => __('YOUR TITLE'),
'href' => 'YOUR URL',
'meta' => array('target' => '_blank')
));

$wp_admin_bar->add_menu( array(
'parent' => 'YOUR PARENT',
'id' => 'YOUR ID',
'title' => __('YOUR TITLE'),
'href' => 'mailto:YOUR LINK?subject=' . get_bloginfo('name'),
'meta' => array('target' => '_blank')
));
}

/**
* The priority of the add_action determines the position on the menu
*
* 10 = Before the WP Logo
* 15 = Between the logo and My Sites
* 25 = After the My Sites menu
* 100 = End of menu
*/
add_action('admin_bar_menu', 'custom_adminbar_menu', 15);

Add a class to the first and last child in wp_nav_menu

This is a nice little function that allows you to add classes to the first and last menu item. This removes the need of using pseudo CSS classes that may not be supported by some browsers.

	 * Add classes for the first and last menu items
	 *
	 * Removes need for :first-child and :last-child which some browsers may not understand
	 */
	function add_first_and_last($output) {
		$output = preg_replace('/class="menu-item/', 'class="first-menu-item menu-item', $output, 1);
		$output = substr_replace($output, 'class="last-menu-item menu-item', strripos($output, 'class="menu-item'), strlen('class="menu-item'));
		return $output;
	}
	add_filter('wp_nav_menu', 'add_first_and_last');

Show a featured image in your WP admin table

This is a nice little function to show a featured image in a native WordPress admin table.

// SHOW FEATURED IMAGE IN LISTINGS
// Add the posts and pages columns filter. They can both use the same function.
add_filter('manage_posts_columns', 'tcb_add_post_thumbnail_column', 5);
add_filter('manage_pages_columns', 'tcb_add_post_thumbnail_column', 5);

// Add the column
function tcb_add_post_thumbnail_column($cols){
$cols['tcb_post_thumb'] = __('Featured Image');
return $cols;
}

// Hook into the posts an pages column managing. Sharing function callback again.
add_action('manage_posts_custom_column', 'tcb_display_post_thumbnail_column', 5, 2);
add_action('manage_pages_custom_column', 'tcb_display_post_thumbnail_column', 5, 2);

// Grab featured-thumbnail size post thumbnail and display it.
function tcb_display_post_thumbnail_column($col, $id){
switch($col){
case 'tcb_post_thumb':
if( function_exists('the_post_thumbnail') )
echo the_post_thumbnail('admin_thumb');
else
echo 'Not supported in theme';
break;
}
}

Adding menu classes to menu links of custom post types

When you’re using a custom menu that you’ve built in WordPress, you have to add Custom Post Type archive pages as custom links.

This causes a problem in that WordPress doesn’t then add the current_page_parent class to this link as the user browses this strand of the site.

Fortunately JavaScript comes to the rescue with the help of the WP body_class tag.

$(function() {

    if ($("body").hasClass("single-product")) {   
// this is the class of the post type as written onto the body tag by body_class 
        $("li#menu-item-41").addClass("current_page_parent");   
// Adds the class current_page_parent to the correctly specified li
    }
});