How WordPress Scheduled Posts Work

There’s a bit of a misnomer in the WordPress community about scheduled posts that I hope to help clarify.

To start, let’s do a quick overview of the WordPress cron system. It’s a psuedo-cron, meaning that it doesn’t rely on a true linux crontab or the Windows version of scheduled task running. It depends on user activity on the site. If a user visits a page, it triggers a check of the scheduled tasks, and if it finds one that is in the past, it invokes the task callback. If WP_DISABLE_CRON is set to true, then the implicit behavior-based system is disabled and awaits for an explicit call to wp-cron.php. This is really the better option of the two as you can run a curl on the wp-cron.php URL from a true crontab and invoke the scheduler on a cyclical basis from the os task runner.
Continue…

Get file permissions, owner and group with PHP

Here are a few functions to help you when interacting with files in your php scripts:

Find the owner of a file:
An array of information about the owner is returned.

function foo_get_file_ownership($file){
	$stat = stat($file);
	if($stat){
		$group = posix_getgrgid($stat[5]);
		$user = posix_getpwuid($stat[4]);
		return compact('user', 'group');
	}
	else
		return false;
}

Get the four digit file permissions number:
A permissions string is returned. Example: 0755

function foo_get_file_perms($file){
	return substr(sprintf('%o', fileperms($file)), -4);
}

Continue…

Kint Debugger for WordPress

We just released a new UpThemes plugin called Kint Debugger for WordPress. It’s a simple wrapper for the awesome Kint debugger class. It allows you to view variable dumps in a styled and collapsible output. Kint also allows for easy back-tracing.

Examples

global $wp_query;
d($wp_query); //styled, collapsible output
s($wp_query); //un-styled output

I added a couple WordPress specific functions to aid in plugin and theme development.

dump_wp_query();
dump_wp();
dump_post();

View the entire documentation at the Kint site.

Check out the UpThemes plugin page.

Download Kint for WordPress

PHP – Passing data between classes

Static class properties are the most efficient way to pass information from one class to another. Typically, one would place a variable in the global scope for other functions and classes to use. However, this is not a good practice as setting GLOBALS equates to a downgrade in performance. Using static properties allows you to store/retrieve data without instantiating a class object.

class IntermediaryData{
    public static $global = null;

    public function set($data){	
        return self::$global = $data;
    }
	
    public function get(){
        return self::$global;
    }
}

Use this in your classes to set and retrieve data:

IntermediaryData::get();
IntermediaryData::set($data);

A good use case for this functionality is passing a rolling list of ids to exclude from queries from widget to widget within a dynamic WordPress sidebar.

Display hidden post types automatically in WP nav menus admin

One of the most annoying things about WP nav menu admin is when you create a custom post type, you first must go to Screen Options and check the custom post type checkbox to display it for use. Here’s a hack I created to get around this and force the custom post type to display for all users all the time.

function display_post_type_nav_box(){

    $hidden_nav_boxes = get_user_option( 'metaboxhidden_nav-menus' );

    $post_type = 'foobar'; //Can also be a taxonomy slug
    $post_type_nav_box = 'add-'.$post_type;

    if(in_array($post_type_nav_box, $hidden_nav_boxes)):
        foreach ($hidden_nav_boxes as $i => $nav_box):
            if($nav_box == $post_type_nav_box)
                unset($hidden_nav_boxes[$i]);
        endforeach;
        update_user_option(get_current_user_id(), 'metaboxhidden_nav-menus', $hidden_nav_boxes);
    endif;
}
add_action('admin_init', 'display_post_type_nav_box');