Created
October 15, 2010 21:45
-
-
Save pietromalerba/629010 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Simple WordPress custom post type example for WP3.0+ | |
// More info at http://curtishenson.com/wordpress-custom-post-types-and-meta-boxes-example | |
//These two lines initiate everything | |
add_action( 'init', 'CHLinksInit' ); | |
function CHLinksInit() { global $ch_links; $ch_links = new CHLinks(); } | |
class CHLinks { | |
function CHLinks() { | |
register_post_type( 'link', | |
array( | |
'label' => __( 'Link Post' ), | |
'singular_label' => __( 'Link Post' ), | |
'public' => true, | |
'menu_position' => 5, | |
'query_var' => true, | |
'supports' => array('title', 'editor'), | |
'rewrite' => array('slug'=>'link'), | |
'taxonomies' => array('post_tag') | |
) | |
); | |
add_action("admin_init", array(&$this, "admin_init")); | |
add_action('save_post', array(&$this, 'save_post_data')); | |
// Add custom post navigation columns | |
add_filter("manage_edit-link_columns", array(&$this, "nav_columns")); | |
add_action("manage_posts_custom_column", array(&$this, "custom_nav_columns")); | |
// If you want to use a custom template name | |
add_action("template_redirect", array(&$this, 'template_redirect')); | |
} | |
function admin_init(){ | |
add_meta_box("link-url-meta", "Link URL", array(&$this, "link_url_meta_box"), "link", "normal", "high"); | |
} | |
function link_url_meta_box() { | |
global $post; | |
$meta = get_post_meta($post->ID, 'ch_link_url', true); | |
// Verify | |
echo'<input type="hidden" name="ch_link_url_noncename" id="ch_link_url_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />'; | |
// Fields for data entry | |
echo '<label for="ch_link_url" style="margin-right: 10px;">Enter Link URL:</label>'; | |
echo '<input type="text" name="ch_link_url" value="'.$meta.'" size="50" />'; | |
} | |
function save_post_data( $post_id ) { | |
global $post; | |
// Verify | |
if ( !wp_verify_nonce( $_POST["ch_link_url_noncename"], plugin_basename(__FILE__) )) { | |
return $post_id; | |
} | |
if ( 'page' == $_POST['post_type'] ) { | |
if ( !current_user_can( 'edit_page', $post_id )) | |
return $post_id; | |
} else { | |
if ( !current_user_can( 'edit_post', $post_id )) | |
return $post_id; | |
} | |
$data = $_POST['ch_link_url']; | |
// New, Update, and Delete | |
if(get_post_meta($post_id, 'ch_link_url') == "") | |
add_post_meta($post_id, 'ch_link_url', $data, true); | |
elseif($data != get_post_meta($post_id, 'ch_link_url', true)) | |
update_post_meta($post_id, 'ch_link_url', $data); | |
elseif($data == "") | |
delete_post_meta($post_id, 'ch_link_url', get_post_meta($post_id, 'ch_link_url', true)); | |
} | |
function nav_columns($columns) { | |
$columns = array( | |
"cb" => "<input type=\"checkbox\" />", | |
"title" => "Link Title", | |
"link_description" => "Description", | |
"link_url" => "Link URL", | |
); | |
return $columns; | |
} | |
function custom_nav_columns($column) { | |
global $post; | |
switch ($column) { | |
case "link_description": | |
the_excerpt(); | |
break; | |
case "link_url": | |
$meta = get_post_custom(); | |
echo $meta["ch_link_url"][0]; | |
break; | |
} | |
} | |
// If you want to use a custom template name you can use template_redirect() | |
// WP defaults to single-"custom-post-name".php | |
function template_redirect() { | |
global $wp; | |
if ($wp->query_vars["post_type"] == "link") { | |
include(TEMPLATEPATH . "/single-link.php"); | |
die(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment