Code Versus Data in Customized WordPress Models

Surprised, at first, that Codex says to (re)register custom post types (CPT) at “init” — an event (WP calls them “hooks”) triggered early in the processing of every request. Custom taxonomies, too.

Why repeat this messy setup (WP even registers its own built-ins that way) — it’s just configuration data, no logic — regardless of whether it’s at all needed in that request? Isn’t this configuration persisted to the DB? No.

Curious tradeoff between code and data?

  1. A lot of this configuration is labels for menus in the admin screens, which need to be translated, thus all the gettext tagging.
    $labels = array(
    	'name' => _x('Books', 'post type general name', 'your_text_domain'),
    	'singular_name' => _x('Book', 'post type singular name', 'your_text_domain'),
    	'add_new' => _x('Add New', 'book', 'your_text_domain'),
    	'add_new_item' => __('Add New Book', 'your_text_domain'),
    	'edit_item' => __('Edit Book', 'your_text_domain'),
    	'new_item' => __('New Book', 'your_text_domain'),
    	'all_items' => __('All Books', 'your_text_domain'),
    	'view_item' => __('View Book', 'your_text_domain'),
    	'search_items' => __('Search Books', 'your_text_domain'),
    	'not_found' =>  __('No books found', 'your_text_domain'),
    	'not_found_in_trash' => __('No books found in Trash', 'your_text_domain'), 
    	'parent_item_colon' => '',
    	'menu_name' => __('Books', 'your_text_domain')
    );
  2. But, gettext uses the “source” to prepare translations — the code doesn’t need to be executed when it’s not being rendered. Why not use a callback?
  3. WP does such “registrations” a lot, instantiating crufty data structures, storing them in globals (nu, PHP). I wonder…



Comments are closed.