Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Following this tutorial, I am trying to create a custom Meta Box on my theme by adding following codes to the function.php file:

enter code here
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'normal', 'high' );
add_action( 'add_meta_boxes', 'cd_meta_box_add' );

function cd_meta_box_add()
{
  add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'normal', 'high' );
}

function cd_meta_box_cb()  
{  
  echo 'What you put here, show\'s up in the meta box';     
}  

but I am getting following error :

fatal error

( ! ) Fatal error: Call to undefined function add_meta_box() in C:\wamp\www\TMPress\wp-content\themes\twentytwelve\cpt.php on line 94
Call Stack
#   Time    Memory  Function    Location
1   0.0012  865440  {main}( )   ..\edit.php:0
2   0.0018  982824  require_once( 'C:\wamp\www\TMPress\wp-admin\admin.php' )    ..\edit.php:10
3   0.0021  1008744 require_once( 'C:\wamp\www\TMPress\wp-load.php' )   ..\admin.php:30
4   0.0023  1026872 require_once( 'C:\wamp\www\TMPress\wp-config.php' ) ..\wp-load.php:29
5   0.0029  1158248 require_once( 'C:\wamp\www\TMPress\wp-settings.php' )   ..\wp-config.php:90
6   0.3251  32951888    include( 'C:\wamp\www\TMPress\wp-content\themes\twentytwelve\functions.php' )   ..\wp-settings.php:291
7   0.3255  32979176    include_once( 'C:\wamp\www\TMPress\wp-content\themes\twentytwelve\cpt.php' )    ..\functions.php:2

Can you please let me know why this is happening? is there any better way to add a meta box containing a field and dropdown select box? I know there are some plugins out there for doing this but I really like to learn it on my own.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

Try this

add_action( 'add_meta_boxes', 'cd_meta_box_add' );

function cd_meta_box_add()
{
  add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'normal', 'high' );
}

function cd_meta_box_cb()  
{  
  echo 'What you put here, show\'s up in the meta box';     
}  

ps: you have some extra problematic lines, at the top of your code, that I removed, namely:

enter code here
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'normal', 'high' );

Update:

To add the metabox to your custom post type edit page, replace 'post' with 'your-custom-post-type' in this line:

add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'your-custom-post-type', 'normal', 'high' );
share|improve this answer
    
Hi Bitgire, thanks I am nit getting any error now but there is not any meta box on the page! I mean in Posts admin page! –  Behseini May 15 '13 at 16:55
    
check your post edit page. –  birgire May 15 '13 at 16:56
    
sorry man I find it at is added to Post! can you please let me know how to customize it to only added to my Custom post Type? –  Behseini May 15 '13 at 16:57
1  
Or use the action add_meta_boxes_your-post-type. –  toscho May 15 '13 at 17:02
1  
I think the enter code here line is an editing "error" here in the Stack interface. I noticed recently that if we click {} in an empty space, it prints exactly that in the editing box. –  brasofilo May 15 '13 at 17:10
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.