Can
Can

Reputation: 4726

Custom post type breaks wordpress itself

I'm trying to add a custom post type to Wordpress. I can register the custom post type with the following code so that it is visible on the menu bar on the left.

add_action( 'init', 'add_member');

function add_member() {

$args = array(
    'label' => __('Members'),
    'singular_label' => __('Member'),
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarhical' => false,
    'rewrite' => true,
    'supports' => array('title', 'editor', 'thumbnail')
);

register_post_type( 'member', $args );

}

But the problem is that when I try to add a custom meta box, it breaks the site.

add_action("admin_init", "admin_init");

function admin_init() {

add_meta_box("memberInfo-meta", "Member Options", "meta_options", "member", "side", "low");

}

add_action('save_post', 'save_member');

function meta_options() {

global $post;
$custom = get_post_custom($post->ID);
$member = $custom["member"][0];

}

<label>Member:</label><input name="member" value="<?php echo $member; ?>" />

function save_member() {

global $post;
update_post_meta( $post->ID, "member", $_POST["member"] );

}

What am I doing wrong?

Thanks.

Upvotes: 1

Views: 269

Answers (3)

sirBlond
sirBlond

Reputation: 335

You should hook your function to "add_meta_boxes" action.

instead of:

add_action("admin_init", "admin_init");

use:

add_action("add_meta_boxes", "admin_init");

also I would suggest you to use prefixes for your functions: instead of calling your function admin_init you could call it myplugin_admin_init. This will help you to avoid errors because of functions naming.

Upvotes: 0

ninja
ninja

Reputation: 2263

Just glancing through your code, but. You're trying to hook your function called "admin_init" to the hook admin_init. This obviously won't work. Try changing your function "admin_init" to something else.

You could activate DEBUG in your wp_config (or check your error_log) to actually find out what's causing it to break.

Upvotes: 1

Bud Damyanov
Bud Damyanov

Reputation: 31839

Not sure how/what you need to accomplish, but there is a really nice, small and neat plug-in, called "custom-post-type-ui" with great capabilities. Long time ago I was in struggle as you, but this plug-in made my life easier. See here.

Upvotes: 1

Related Questions