Closed Dinamiko closed 10 years ago
Hi Emili,
I haven't actually documented that properly, but it's easy enough to do (once you know how). All you need to do is add a 'metabox' => 'metabox_id'
parameter to the custom field and the field will then appear in the metabox you specified. So your custom field array would look something like this:
array(
'id' => '_field_id',
'metabox' => 'metabox_id',
'label' => __( 'Field label', 'text-domain' ),
'description' => __( 'Field description', 'text-domain' ),
'type' => 'text',
'default' => '',
)
You can also pass an array to the metabox
parameter to make your field appear in more than one metabox.
At some point in the future I'll add a new 'example' plugin that shows how to use all of the APIs from this template correctly.
Thanks for your reply, but I don't know how create a custom field using your example, I will wait for a working example of this. Meanwhile I'm going to do it using the standard add_meta_box and creating the custom fields with add_post_meta / get_post_meta.
If you have a look at the meta_box_content
method in the admin API class, you will see that you can use this filter to add custom fields to any post type: add_filter( '{$post_type}_custom_fields', 'my_custom_fields', 10, 2 );
Than a function like this would create the custom fields:
function my_custom_fields ( $fields, $post_type ) {
$fields = array(
array(
'id' => '_field_1_id',
'metabox' => 'metabox_id',
'label' => __( 'Field 1 label', 'text-domain' ),
'description' => __( 'Field 1 description', 'text-domain' ),
'type' => 'text',
'default' => '',
),
array(
'id' => '_field_2_id',
'metabox' => 'metabox_id',
'label' => __( 'Field 2 label', 'text-domain' ),
'description' => __( 'Field 2 description', 'text-domain' ),
'type' => 'text',
'default' => '',
),
);
return $fields;
}
That will output the custom fields in the metabox using the admin API so that the fields are displayed and formatted nicely.
I really do need to write documentation for all of this, but I just haven't had the time to do that just yet. I'll get on that as soon as I can :)
Awesome! I tried it this way but I get an error in the metabox (sure I'm doing something wrong):
__construct (test with custom post type):
add_filter( 'custom-post-type-two_custom_fields', array( $this, 'my_custom_fields', 10, 2 ) );
my_custom_fields function same as your example changing 'metabox' id parameter
and I'm getting this error in the metabox: Warning: call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in ... wp-includes/plugin.php on line 214
Anyway, I'm going to continue trying :)
p.d. I'm aware of what it means get time to work on any free project, I just want you to know that this plugin is for me by far the best boilerplate plugin and I hope that will continue growing in the future :)
Your syntax is slightly wrong - you left out the closing bracket for your array
, so it should look like this:
add_filter( 'custom-post-type-two_custom_fields', array( $this, 'my_custom_fields' ), 10, 2 ) );
I'll definitely continue to grow this template - I use it for all of my own plugins, so as I build more I'll improve the template more :)
perfect! this works now, only say that works with deleting the last )
add_filter( 'custom-post-type-two_custom_fields', array( $this, 'my_custom_fields' ), 10, 2 );
Other thing I see (is a Notice, I'm using WP_DEBUG true): Notice: Undefined index: placeholder in ... includes/lib/class-myplugin-admin-api.php on line 79 this is the line 79:
$html .= '<input id="' . esc_attr( $field['id'] ) . '" type="text" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . esc_attr( $data ) . '" />' . "\n";
I know this is out of scope of my issue (my issue was completely resolved), but it's just so you know :)
Thanks for you time and patience with me ;)
Glad you got things sorted!
Thanks for bringing that up - that PHP notice won't be tough to sort out, so I'll get that sorted soon. Feel free to ask any further questions on here if you need to :)
The syntax for this:
function my_custom_fields ( $fields, $post_type ) {
$fields = array(
array(
'id' => '_field_1_id',
'metabox' => 'metabox_id',
'label' => __( 'Field 1 label', 'text-domain' ),
'description' => __( 'Field 1 description', 'text-domain' ),
'type' => 'text',
'default' => '',
),
array(
'id' => '_field_2_id',
'metabox' => 'metabox_id',
'label' => __( 'Field 2 label', 'text-domain' ),
'description' => __( 'Field 2 description', 'text-domain' ),
'type' => 'text',
'default' => '',
),
);
return $fields;
}
Throws a warning, doesn't it? Illegal offset type?
No - that error would only come up if an array/object was being specified as the key, which is not the case here.
Hi hlashbrooke,
I'm trying to use the new admin api for metaboxes, I've only achieved to show the metabox but not the custom fields inside:
in __construct:
and the function:
this shows the metabox in the required post types correctly, but what next? how can I put custom fields in the metabox?
thanks in advance :)