-
Notifications
You must be signed in to change notification settings - Fork 50
Integrating Babble with Fieldmanager
Fieldmanager is a toolkit for developers to create complex administration screens in WordPress.
We'll use the basic textfield example:
//http://fieldmanager.org/docs/fields/textfield/
add_action( 'fm_post_post', function() {
$fm = new Fieldmanager_TextField( array(
'name' => 'demo-field',
) );
$fm->add_meta_box( 'TextField Demo', array( 'post' ) );
} );
Since we'll want to make this field translatable, we have to register a new UI for the translation screen.
Let's start with a working example:
//Babble translation UI registration
add_filter( 'bbl_translated_meta_fields', function( array $fields, WP_Post $post ) {
$fields[ 'demo-field' ] = new Babble_Meta_Field_Text(
$post, // WP_Post object - is provided by the filter as it's second argument
'demo-field', //meta_key
__( 'Fieldmanager Demo field', 'bbl_fm' ), //title
array() //arguments
);
return $fields;
}, 10, 2 );
//Synchronisation exclusion
add_filter( 'bbl_sync_meta_key', function( $synchronize, $meta_key ) {
if ( 'demo-field' === $meta_key ) {
$synchronize = false;
}
return $synchronize;
}, 10, 2 );
Babble provides bbl_translated_meta_fields
filter for adding new meta fields. It provides the callback function with two parameters, an array of $fields
and WP_Post object. Both are necessary in order to add a translation UI and hooking the logic working on behind the scenes for our demo-field
.
### Babble_Meta_Field_Text Class
Babble comes with some ready-to-use meta field translation UI classes for the most common meta box fields:
- Babble_Meta_Field_Text
- Babble_Meta_Field_Textarea
- Babble_Meta_Field_Editor
All are subclasses of abstract Babble_Meta_Field class and you can easily extend that class in your own plugin or theme in case those basic ones are not enough for you.
When initialising a new Babble_Meta_Field object, you need to provide following parameters:
WP_Post $post
-
(string) $meta_key
- is used for reading and saving the meta value -
(string) $meta_title
- is used for displaying the label next to input elements -
(array) $args
(optional, defaults to an emptyarray()
) - additional arguments you may need inside your own class
### bbl_sync_meta_key Filter
All post metas are being synchronised across all translation on update. No matter whether the post meta get changed in the original post or in it's translation. This way Babble secures that all translations and original post are synchronised.
You may want to exclude your meta which is supposed to be translated and thus not synchronised - else any attempt for translating the meta would result in overriding the value across all translations and original post in case you translated that.
//Register Fieldmanager fields
add_action( 'fm_post_post', function() {
//http://fieldmanager.org/docs/fields/textarea/
$fm = new Fieldmanager_TextArea( array(
'name' => 'demo-textarea',
) );
$fm->add_meta_box( 'TextArea Demo', array( 'post' ) );
//http://fieldmanager.org/docs/fields/richtextarea/
$fm = new Fieldmanager_RichTextArea( array(
'name' => 'demo-richtext',
) );
$fm->add_meta_box( 'RichTextArea Demo', array( 'post' ) );
} );
//Babble translation UI registration
add_filter( 'bbl_translated_meta_fields', function( array $fields, WP_Post $post ) {
//Textarea translation UI
$fields[ 'demo-textarea' ] = new Babble_Meta_Field_Textarea( $post, 'demo-textarea', __( 'Fieldmanager Demo field', 'bbl_fm' ), array() );
//RichText/Visual Editor translation UI
$fields[ 'demo-richtext' ] = new Babble_Meta_Field_Editor( $post, 'demo-richtext', __( 'Fieldmanager Demo field', 'bbl_fm' ), array() );
return $fields;
}, 10, 2 );
//Synchronisation exclusion
add_filter( 'bbl_sync_meta_key', function( $synchronize, $meta_key ) {
//exclude more than one meta_key at once
$do_not_sync = array(
'demo-textarea',
'demo-richtext',
);
//use in_array() with $strict whenever possible
if ( true === in_array( $meta_key, $do_not_sync, true ) ) {
$synchronize = false;
}
return $synchronize;
}, 10, 2 );