3

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

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.

    1 Answer 1

    8

    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:

    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' ); 
    7
    • 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
      CommentedMay 15, 2013 at 16:55
    • check your post edit page.
      – birgire
      CommentedMay 15, 2013 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
      CommentedMay 15, 2013 at 16:57
    • 1
      Or use the action add_meta_boxes_your-post-type.
      – fuxia
      CommentedMay 15, 2013 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
      CommentedMay 15, 2013 at 17:10

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.