BuddyDev

Search

[Resolved] Custom field in BP Simple Front End Post

  • Participant
    Level: Initiated
    Posts: 8
    maelga on #382

    Hi Brajesh,

    How can one add custom fields in BP Simple Front End Post?

    I see the render_custom_fields() function in class-edit-form.php so it seems that custom fields are supported.

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24697
    Brajesh Singh on #383

    Hi Maelga,
    Thank you for asking.

    Yes, It supports custom fields. Her is an example of form

    
    $settings = array(
    	'post_type'				=> 'post_type',
    	'post_status'			=> 'draft',//post status to be saved, draft|publish|pending etc
    	'comment_status'		=> 'open', //open/closed
    	'show_comment_option'	=> 1,//allow person posting the form to control whether there will be comment or not
    	'custom_field_title'	=> '',//Section title for custom fields on edit form
    	'custom_fields'			=> array(
    	
    		'key1'	=> array(
    			'type'	=> 'textbox', //textbox, textarea, radio, checkbox, select, date, hidden
    			'label'	=> 'Label for this field ',
    			'default' => 'default value for this field'	
    		),
    		'key2'	=> array(
    			'type'	=> 'checkbox', //
    			'label'	=> 'Label for this field ',
    			'default' => 'default value for this field',
    			'options'	=> array(
    				array(
    					'label' => 'Some label',
    					'value'	=> 'value'
    				),
    				array(
    					'label' => 'Some Other label',
    					'value'	=> 'value 2'
    				),
    				
    			)
    		)
    	),      
    
    	//'upload_count'			=> 0,
    	'has_post_thumbnail'	=> 1,
    	'current_user_can_post' => true,//true|false attach some callback here to do it at run time
    	'update_callback'		=> '',//register your callback function here, It will be called after saving post, provided the post id
    );
    bp_new_simple_blog_post_form( 'your-unique-form-name', $settings );
    

    As you see ‘custom_fields’ is a multi dimensional array.

    I will just explain that section.

    The custom_fields is an associative array of arrays where ‘key’ is used as the post meta key and the value array is used for displaying the custom field.

    For example:-

    
    $custom_fields = array(
    		//key1 is stored as post meta key
    		'key1'	=> array(
    			'type'	=> 'textbox', //textbox, textarea, radio, checkbox, select, date, hidden
    			'label'	=> 'Label for this field ',
    			'default' => 'default value for this field'	
    		),
    		
    	);      
    

    In the above snippet, we are registering a single custom field of type ‘text’. The value provided here will be stored in the post meta with meta key =’key1′.

    For the textbox, textarea, date, hidden fields you can register those as other entries as shown above.

    It gets complicated when we need to deal with ‘selectbox’, ‘checkbox’ and ‘radio’ fields. Fo these fields, we need to provide another field in the value array with key ‘options’.

    This ‘options’ field is an array of arrays and contains each of the radio/checkbox/selectbox item as key, label array.

    Hope that helps.

  • Participant
    Level: Initiated
    Posts: 8
    maelga on #394

    Thanks Brajesh.
    It’s very useful.

The topic ‘ [Resolved] Custom field in BP Simple Front End Post’ is closed to new replies.

This topic is: resolved