First Impressions of Custom Post Type

One of the new very interesting things in WordPress 3.0 are individual post-types you can implement with little effort. Back then, you had to expand the database and write your own interface for it, now you just have to add a few lines of code - of course this is just the current state, which can be change until the final release.

After Justin had been playing with these types, we check out the possibilities of types for "Movies".

Simple Solution

function post_type_movies() {
	register_post_type( 'movies',
                array( 'label' => __('Movies'), 'public' => true, 'show_ui' => true ) );
	register_taxonomy_for_object_type('post_tag', 'movies');
}
add_action('init', 'post_type_movies');

default custom post type

More parameters for meta-boxes

Of course there are a number of parameters for this function and so the behavior and appearance of the corresponding edit page can be controlled quite easily, a small sample with additional meta boxes:

function post_type_movies() {
	register_post_type(
                     'movies', 
                     array('label' => __('Movies'), 
                             'public' => true, 
                             'show_ui' => true,
                             'supports' => array(
                                        'post-thumbnails',
                                        'excerpts',
                                        'trackbacks',
                                        'custom-fields',
                                        'comments',
                                        'revisions')
                                ) 
                      );
	register_taxonomy_for_object_type('post_tag', 'movies');
}
add_action('init', 'post_type_movies');

The default arguments

// Args prefixed with an underscore are reserved for internal use.
$defaults = array(
    'label' => false,
    'publicly_queryable' => null,
    'exclude_from_search' => null,
    '_builtin' => false,
    '_edit_link' => 'post.php?post=%d',
    'capability_type' => 'post',
    'hierarchical' => false,
    'public' => false,
    'rewrite' => true,
    'query_var' => true,
    'supports' => array(),
    'register_meta_box_cb' => null,
    'taxonomies' => array(),
    'show_ui' => null
);
  • label - A descriptive name for the post type marked for translation. Defaults to $post_type
  • public - Whether posts of this type should be shown in the admin UI. Defaults to false
  • exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public
  • publicly_queryable - Whether post_type queries can be performed from the front page. Defaults to whatever public is set as
  • show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public
  • inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none
  • capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post"
  • edit_cap - The capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post)
  • edit_type_cap - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts)
  • edit_others_cap - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts)
  • edit_others_cap - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts)
  • read_cap - The capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post)
  • delete_cap - The capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post)
  • hierarchical - Whether the post type is hierarchical. Defaults to false
  • supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none
  • register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback
  • taxonomies - An array of taxonomy identifiers that will be registered for the post type. Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type()

Including Custom Taxonomies

In the following example we include in our Post-Type a Taxonomy with two possibilities; own Tags and categories for Post-Type Movies, the classical tag, without hierarchy and one as category, tag with hierarchies.

function post_type_movies() {
	register_post_type(
                'movies', 
                array(
                        'label' => __('Movies'),
                        'public' => true,
                        'show_ui' => true,
                        'supports' => array(
                                     'post-thumbnails',
                                     'excerpts',
                                     'trackbacks',
                                     'custom-fields',
                                     'comments',
                                     'revisions')
                )
        );
	
	register_taxonomy( 'actor', 'movies', array( 'hierarchical' => true, 'label' => __('Actor') ) ); 
	
        register_taxonomy( 'production', 'movies',
		array(
                         'hierarchical' => false,
			 'label' => __('Production'),
			 'query_var' => 'production',
			 'rewrite' => array('slug' => 'production' )
		)
	);
}
add_action('init', 'post_type_movies');

Definitely a very interesting and useful feature which provides many possibilities to play around with.

Disable HTML Editor In WordPress

In contrast to the visual editor in WordPress, you can not disable the HTML editor. WordPress does not provide a user setting or a global option. Also, there is no hook, to disable the HTML tab above the editor when writing posts or pages.

But there are users who do not need this tab and like to deactivate it globally. Therefore, I have a solution, how you can disable it in your editor.
Alternatively, you can use the Plugin Adminimize, which offers such a possibility per role, just put it in the settings.
Continue reading …

Contactable – Contact Form Easy with WordPress

The integration of a contact form in WordPress usually begins with a search of a Plugin and often ends with a great effort too. Since scripts are loaded on all pages and posts - only to be integrated on one side of a form. The blog is only as good as his technician and I would like to show a very simple way how to integrate a contact form into your own blog without much knowledge.
Continue reading …

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;
}