public
Last active

  • Download Gist
custom_post_types.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
<?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();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.