Skip to content

Instantly share code, notes, and snippets.

@coenjacobs
Created March 25, 2012 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coenjacobs/2199729 to your computer and use it in GitHub Desktop.
Save coenjacobs/2199729 to your computer and use it in GitHub Desktop.
Demonstration plugin that registers a post type for 'recipes'. This code is not fully tested, is not 100% written according to the WordPress Coding Standards and could use function prefixing to minimize the risk of having duplicate function names.
<?php get_header(); ?>
<div id="container">
<div id="content" role="main">
<h1 class="page-title">Recepten</h1>
<?php get_template_part( 'loop', 'recept' ); ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-above" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older work', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer work <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
</div><!-- #nav-above -->
<?php endif; ?>
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<div id="post-0" class="post error404 not-found">
<h1 class="entry-title"><?php _e( 'Not Found', 'twentyten' ); ?></h1>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyten' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div><!-- #post-0 -->
<?php endif; ?>
<?php
/* Start the Loop.
*
* In Twenty Ten we use the same loop in multiple contexts.
* It is broken into three main parts: when we're displaying
* posts that are in the gallery category, when we're displaying
* posts in the asides category, and finally all other posts.
*
* Additionally, we sometimes check for whether we are on an
* archive page, a search page, etc., allowing for small differences
* in the loop on each template without actually duplicating
* the rest of the loop that is shared.
*
* Without further ado, the loop:
*/ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php /* How to display posts of the Gallery format. The gallery category is the old way. */ ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title" style="padding-bottom: 10px;"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div style="float: left; margin-bottom: 20px;">
<?php if ( !is_archive() && !is_search() ) { ?>
<?php the_post_thumbnail('medium') ?>
<?php } else { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail') ?>
</a>
<?php }; ?>
</div>
<div class="entry-summary" style="clear: none;">
<div style="padding-left: 10px; float: left; width: 300px;">
<?php echo '<b>Bereidingstijd:</b> '.get_post_meta( $post->ID, '_recept_time', true).' minuten<br />'; ?>
<?php echo get_the_term_list( $post->ID, 'ingredient', '<b>Ingrediënten:</b> ', ', ', '<br />' ); ?>
<?php echo get_the_term_list( $post->ID, 'techniek', '<b>Techniek:</b> ', ', ', '<br />' ); ?>
<?php echo get_the_term_list( $post->ID, 'materiaal', '<b>Materiaal:</b> ', ', ', '<br />' ); ?>
</div>
<?php if ( !is_archive() && !is_search() ) : // Only display excerpts for archives and search. ?>
<div style="clear: both;">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
</div>
<?php endif; ?>
</div><!-- .entry-summary -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; // End the loop. Whew. ?>
<div style="clear: both;"></div>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation" style="margin-top: 0;">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older work', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer work <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
</div><!-- #nav-below -->
<?php endif; ?>
<?php
/*
Plugin Name: Recepten DEMO
Plugin URI:
Description:
Author: Coen Jacobs
Version:
Author URI: http://coenjacobs.me
*/
function register_cpt() {
$labels = array(
'name' => _x( 'Recepten' , 'post type general name' ),
'singular_name' => _x( 'Recept' , 'post type singular name' ),
'add_new' => _x( 'Voeg toe' , 'recept'),
'add_new_item' => __( 'Voeg nieuw recept toe' ),
'edit_item' => __( 'Bewerk recept' ),
'new_item' => __( 'Nieuw recept' ),
'view_item' => __( 'Bekijk recept' ),
'search_items' => __( 'Zoek recepten' ),
'not_found' => __( 'Geen recepten gevonden' ),
'not_found_in_trash' => __( 'Geen recepten gevonden in Trash' ),
'parent_item_colon' => '' ,
'menu_name' => 'Recepten' ,
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'recepten' ),
'capability_type' => 'post' ,
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'supports' => array( 'title' ,'editor', 'thumbnail' ),
'register_meta_box_cb' => 'register_recept_metaboxes' ,
);
register_post_type( 'recept' , $args );
}
add_action( 'init', 'register_cpt' );
function register_taxonomies() {
register_taxonomy(
'ingredient',
'recept',
array(
'hierarchical' => false,
'label' => __('Ingrediënten'),
'sort' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array('slug' => 'ingrediënt')
)
);
register_taxonomy(
'techniek',
'recept',
array(
'hierarchical' => false,
'label' => __('Kooktechnieken'),
'sort' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array('slug' => 'techniek')
)
);
register_taxonomy(
'materiaal',
'recept',
array(
'hierarchical' => false,
'label' => __('Materialen'),
'sort' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array('slug' => 'materiaal')
)
);
}
add_action( 'init', 'register_taxonomies' );
function register_recept_metaboxes()
{
add_meta_box('recept_time_metabox', 'Bereidingstijd', 'time_metabox', 'recept', 'side', 'default');
}
function page_add_meta() {
add_meta_box('recept_time_metabox', 'Bereidingstijd', 'time_metabox', 'page', 'side', 'default');
}
add_action( 'add_meta_boxes', 'page_add_meta' );
function time_metabox()
{
global $post;
echo '<input type="hidden" name="recepttime_noncename" id="recepttime_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$time = get_post_meta($post->ID, '_recept_time', true);
echo '<input type="text" name="_recept_time" value="' . $time . '" /> minuten';
}
function save_time_metabox( $post_id, $post )
{
if ( ! isset($_POST['recepttime_noncename'] ) ) {
return $post->ID;
}
if ( ! wp_verify_nonce( $_POST[ 'recepttime_noncename' ], plugin_basename( __FILE__ ) ) ) {
return $post->ID;
}
if ( ! current_user_can( 'edit_post', $post->ID ) )
return $post->ID;
$savedata['_recept_time'] = $_POST['_recept_time'];
foreach ( $savedata as $key => $value ) {
if ( $post->post_type == 'revision' )
return;
$value = implode(',', (array)$value);
if ( get_post_meta( $post->ID, $key, FALSE ) ) {
update_post_meta($post->ID, $key, $value);
} else {
add_post_meta($post->ID, $key, $value);
}
if ( ! $value )
delete_post_meta($post->ID, $key);
}
}
add_action('save_post', 'save_time_metabox', 1, 2);
function change_columns( $cols ) {
$cols = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Titel', 'trans' ),
'time' => __( 'Bereidingstijd', 'trans' ),
'techniek' => __( 'Kooktechniek', 'trans' ),
'materiaal' => __( 'Materiaal', 'trans' ),
'ingredients' => __( 'Ingrediënten', 'trans' ),
);
return $cols;
}
add_filter( 'manage_recept_posts_columns', 'change_columns' );
function content_columns( $column, $post_id ) {
switch ( $column ) {
case 'time':
$time = get_post_meta( $post_id, '_recept_time', true);
echo $time. ' minuten';
break;
case 'ingredients':
$ingredients = get_the_term_list( $post->ID, 'ingredient', '', ', ', '' );
echo $ingredients;
break;
case 'techniek':
$techniques = get_the_term_list( $post->ID, 'techniek', '', ', ', '' );
echo $techniques;
break;
case 'materiaal':
$materiaal = get_the_term_list( $post->ID, 'materiaal', '', ', ', '' );
echo $materiaal;
break;
}
}
add_action( "manage_posts_custom_column", "content_columns", 10, 2 );
function sortable_columns() {
return array(
'time' => 'time',
);
}
add_filter( "manage_edit-recept_sortable_columns", "sortable_columns" );
function time_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'time' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_recept_time',
'orderby' => 'meta_value_num'
) );
}
return $vars;
}
add_filter( 'request', 'time_column_orderby' );
?>
<?php get_header(); ?>
<div id="container">
<div id="content" role="main">
<h1 class="page-title">Recepten</h1>
<?php get_template_part( 'loop', 'recept' ); ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment