0

I am trying to add another field (subtitle) to my already created and existing custom post_type: team (code from functions.php in my *child*theme)

<?php
/**
 * Custom Post type register framework
 */
include(get_stylesheet_directory(). '/inc/acpt/init.php');
add_action('init', 'makethem');
function makethem() {
    $args = array(
        'supports' => array( 'title', 'editor', 'page-attributes', 'thumbnail', 'excerpt'  ),
        'hierarchical' => true,
    );

    $team = new post_type('team','team', false,  $args );

}

/**
 * Initialize the metabox class
 */
add_action( 'init', 'be_initialize_team_meta_boxes', 9999 );
function be_initialize_team_meta_boxes() {
    if ( !class_exists( 'team_Meta_Box' ) ) {
        require_once( get_stylesheet_directory(). '/inc/metaboxes/fisio-metaboxes.php' );
        require_once( get_stylesheet_directory(). '/inc/metaboxes/init.php' );

    }
}
?>

And this is the code in fisio-metaboxes.php

    <?php
/**
 * Include and setup custom metaboxes and fields.
 *
 * @category YourThemeOrPlugin
 * @package  Metaboxes
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
 * @link     https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
 */

/**
 * Define the metabox and field configurations.
 *
 * @param  array $meta_boxes
 * @return array
 */
 add_filter( 'team_Meta_Box', 'team_sample_metaboxes' );
function team_sample_metaboxes( array $meta_boxes ) {
    $prefix = '_team_';
    $meta_boxes[] = array(
            'id'         => 'team_details',
            'title'      => 'Team details',
            'post_type'      => 'team', // Post type    
            'context'    => 'side',
            'priority'   => 'high',
            'show_names' => true, // Show field names on the left
            'fields'     => array(
                array(
                    'name' => 'Subtitle',
                    //'desc' => '',
                    'id' => $prefix . 'subtitle',
                    'type' => 'text'
                ),
            ),
        );
    return $meta_boxes;
}

add_action( 'init', 'team_initialize_team_meta_boxes', 9999 );
/**
 * Initialize the metabox class.
 */
function team_initialize_team_meta_boxes() {

    if ( ! class_exists( 'team_Meta_Box' ) )
        require_once get_stylesheet_directory(). '/inc/metaboxes/init.php';

}

The problem is that when in the wp-admin i whant to add a new team's post_type post, the post_type doesn't have the subtitle field

What am I missing?

Edit: In case you Didn't notice I've never done this before. I am following this steps with no succeed..

-EDIT-

I am also trying like this:

$meta_boxes[] = array(
            'id'         => 'team_details',
            'title'      => 'Team details',
            'pages'      => 'team', // Post type    
            'context'    => 'normal',
            'priority'   => 'high',
            'show_names' => true, // Show field names on the left
            'fields'     => array(
                array(
                    'name' => 'Subtitle',
                    'desc' => 'This is the subtitle',
                    'id' => $prefix . 'subtitle',
                    'type' => 'text'
                ),
            ),
        );

-EDIT-

Also tried passing the post_type as an array

'pages'      => array('team'), // Post type 
4
  • What is team_sample_metaboxes returning to. Where is the team_meta_box filter being used? Commented Mar 1, 2013 at 14:38
  • Hi @MannyFleurmond thanks for your comment. I realised about the add_filter was missing and I updated the question with current code. Can you see anything wrong with it? Thanks again Commented Mar 1, 2013 at 16:14
  • What I meant was where is the coresponding do_filter? What code us using that meta box array? Commented Mar 1, 2013 at 19:44
  • Hi there, actually never Did this before and I am a bit lost... can you show me a bit the light? Commented Mar 1, 2013 at 21:30

4 Answers 4

1
+50

In your code, you are using:

add_filter( 'team_Meta_Box', 'team_sample_metaboxes' );

On the site you linked to, they are using:

add_filter( 'cmb_meta_boxes', 'wpb_sample_metaboxes' );

So my question is: did you change all instances of "cmb" to "team" in the init.php file?

If you did, then the correct filter to use should be:

add_filter( 'team_meta_boxes', 'team_sample_metaboxes' );

Edit: If you did NOT change init.php file at all (and I don't really see why you would change it), then you shouldn't be changing "cmb" to "team" in your code in the following instances:

1)

add_filter( 'cmb_meta_boxes', 'team_sample_metaboxes' );

2)

if ( ! class_exists( 'cmb_Meta_Box' ) )
1
  • Many thanks! i thought i had to rename the cmb by the custom post_type! (i was re-using a already existing class) thanks a lot again (i did not modify init.php) Commented Mar 5, 2013 at 15:50
2

I recently released a plugin called SuperCPT which makes this process very easy for a developer. That said, it sounds like you're not really a developer, and you might benefit from having a UI. In that case, you might check out the plugin More Fields which allows you to create meta boxes and fields without writing code.

If you end up using SuperCPT, you can replace all your code (custom post type declaration and meta box code) with:

function wpse_88959_add_meta_boxes() {
    if ( ! class_exists( 'Super_Custom_Post_Type' ) )
        return;

    $teams = new Super_Custom_Post_Type( 'team', 'Team', 'Teams', array( 'hierarchical' => true ) );
    $teams->add_meta_box( array(
        'id' => 'team_details',
        'context' => 'normal',
        'priority' => 'high',
        'fields' => array(
            'subtitle' => array()
        )
    ) );
}
add_action( 'after_setup_theme', 'wpse_88959_add_meta_boxes' );
2
  • Hi there! thanks for your answer and time. I'm not a wordpress developer, that match is true! I am actually a web developer that its not familiar with this framework though. I've been given this project i need to tweak a bit (mostly front) but i need this extra field.. and it's a shame (i will consider for own projects though!) that i can not use extra plugins here.. any thoughs about the metaboxes I posted? Commented Mar 4, 2013 at 18:38
  • 2
    No, sorry. Best of luck! Commented Mar 4, 2013 at 18:42
1

Try changing the pages to an array like so:

'pages' => array('page'), // post type
1
  • Hi there, thanks for your reply. I Did try.. same result... Is there any step that follows this coding? Commented Mar 2, 2013 at 19:59
0

I would recommend you use this great tool to add meta fields. It has many features and was build by respectable WP coders. Everything works right out the box.

Just read the instructions and also copy the example functions to your functions file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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