StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
790 stars 61 forks source link

ACF Front End Form using ACF Builder #87

Open developerpaddy opened 5 years ago

developerpaddy commented 5 years ago

Hello StuoutLogic,

Great Efforts,

I loved the way you sorted out the complexity in ACF Form development using PHP.

I am very sorry if I have missed this from avavilable doc, Can we use ACF builder to get form at frontend? and How we can use it ( if possible code snippet)

Thanks for the great efforts.

stevep commented 5 years ago

Thanks for the kind words @developerpaddy!

I don't have a lot of familiarity with front end forms via ACF, but after looking into it, is the difficulty you are having related to setting the fields parameter in the acf_register_form function? Or is there something else?

developerpaddy commented 5 years ago

Hey, thanks for the reply,

Eventually, I found the solution, someone might will get benefit out of it or will find a better solution,

require plugin_dir_path( __FILE__ ) . '/vendor/autoload.php';

use StoutLogic\AcfBuilder\FieldsBuilder;

$banner = new FieldsBuilder('banner');
$banner
    ->addText('title')
    ->addWysiwyg('content')
    ->addImage('background_image');

$bannerNew = new FieldsBuilder('banner_new');
$bannerNew
    ->addText('title_new')
    ->addWysiwyg('content_new')
    ->addImage('background_image_new');

$bannerMerged = new FieldsBuilder('Banner Merged',[
        'key' => 'group_banner_merged', 
        'style' => 'seamless'
    ]);

$bannerMerged
    ->addTab('Banner')
        ->addFields($banner)
    ->addTab('Banner New')
        ->addFields($bannerNew)
    ->setLocation('post_type', '==', 'cp_event');

add_action('acf/init', function() use ($bannerMerged, $bannerNew, $banner) {

        acf_add_local_field_group($banner->build());  
        acf_add_local_field_group($bannerNew->build());  
        acf_add_local_field_group($bannerMerged->build());  

});

After that one can use this group fields added into the local cache in theme's

<?php acf_form(); ?>

Like below,

<?php

             acf_form([
                    'id' => 'cp-event-form',
                    'field_groups' => array('group_banner_merged'), //or group_banner or group_banner_new
                    'post_id'   => 'new_post',
                    'new_post'  => array(
                        'post_type'     => 'cp_event',
                        'post_status'   => 'publish'
                    ),
                    'post_title'=> true,
                    'post_content'=> true,
              ]);

?>

one can find the following links helpful,

https://www.advancedcustomfields.com/resources/create-a-front-end-form/ https://www.advancedcustomfields.com/resources/register-fields-via-php/

Please provide code snippet, blog link or any other convenient way, if anyone finds a better way to implement it.

Thanking you, Have A nice Day