Using Active Extra fields feature from other plugins
Monday, 23 August 2010 11:38The Active Extra Fields wordpress plugin works a whole ,but a plugin designer can make use of it features to create custom write panels.For reusability sake this plugin supports,class functions which can be used to create,edit and delete Meta boxes and Fields.Also Filters and Action hooks are supported an allow to control actions excecuted by this plugin and the filtering of data collected.This is a short tutorial on how to make use of some of the Active Extra Fields features.
Creating a Meta Box
Creatin a meta box is very simple.All you have to do is to call the save_metabox method of the ameta_box class with the following parameters
- Box title :string
- Post Types:mixed (an array or a list of posted seperated by a comma)
- Position:string (side,normal)
- Priority:string (high,low)
Calling this method would return the id of the meta box created ,which can be use to create fields to display in that box.Here is an example below.
$box_id=ameta_box::save_metabox('Address and Contact Information'
,'business_listing'
,'normal','high');
Creating a field
Using the id of a box ,a field can be created by calling the save_field function of the axfield class.The function takes the following parameters:
- Box ID:integer
- Field key: string (The key of the field)
- Field Label:string
- Input Type:string (text,date,email,checkbox,checkboxlist,textarea,wusiwug)
- Display order:int
- Options:string (option sued by multiple value types such as select and checkboxlist.Values should be seperated by a comma)
- Validations:array optional
- Slug:string optional
- Id:int optional
This method would save a new field to database.See an example use of this function bellow
axfield::save_field($box_id
,'email'
,'E-mail'
,'email'
,0
,''
,array(array('required','Please enter an e-mail')));
Filter Hooks
The axf_action filter
This filter is used to process some of the plugin actions such as meta boxe deletion.The Active Extra fields plugin has admin page to manage Meta Boxes and Fields.Before an axf_action is is performed an action variable is first created and a filter applyed to this variable.This variable is an array with the following valuesble
- proceed (boolean): Speciies if the action is allowed. Setting this action to false would prevent the action from excecuting.
- action (string): The action name
- element_id (int):If an element is being performed an action on then the ID of the item is supplied in this variable.
The example bellow show the action variable before a deletion of a field.
$action=array('proceed'=>true,'action'=>'delete_field','element_id'=>$id);
When applying a filter to this request variable ,if a action is cancelled a cancel url is required.This url specifies where the browser should send the user once the action is cancelled.So at the end on the filter we can have a filterer variable with as bellow
array('proceed'=>false,
'action'=>'delete_field',
'element_id'=>$id,
'redirect_url'=>get_option('home').'/wp-admin/options-general.php?page=meta-boxes&tab=field&field='.$element_id.'&msg='.urlencode('You cannot delete this field because it is used by the plugin "Your Plugin Name"')
Tips:When you create a plugin you might want to prevent display,deletion and editing of boxes and/or fields by the Active Extra Field plugin.A way to achieve that is to create boxes and fields when your plugin is activated and save IDs of boxes and fields created to storage.Depending on the action name and element id the action variable you can choose to cancel an action.
The axf_get_boxes filter
This is used to filter the list of metaboxes displayed in the admin on the boxes management page.this hook is usefull in sutiuation when a plugin metaboxes must not be visible or editable by admin on the meteboxes listing page.
Hope this help.Happy programming
1 Comment
-
Comment Link
Saturday, 27 November 2010 07:05
posted by Emma Smith
Very nice Plugin
Leave a comment
Make sure you enter the (*) required information where indicated.
Basic HTML code is allowed.