Support

Account

Home Forums Front-end Issues Shortcode ACF frontend form

Solved

Shortcode ACF frontend form

  • I want to create a shortcode with attributes, which will display ACF frontend form. However, my code does not work. What am I doing wrong?

    //-----------------------------------------------------
    // ACF new post
    //-----------------------------------------------------
    
    add_shortcode( 'acfNew', 'acf_new' );
    function acf_new( $atts ) {
         ob_start();
       // Attributes
       extract( shortcode_atts(
           array(
              'acfPostID' => '',
              'acfPostTitle' => '',
              'acfPostContent' => '',
              'acfPostType' => '',
              'acfPostStatus' => '',
              'acfPostReturn' => '',
              'acfPostSubmit' => '',
           ), $atts )
       );
        // Start
    acf_form(array(
                     'post_id' => $acfPostID,
                                            'post_title' => $acfPostTitle,
                                            'post_content' => $acfPostContent,
                     'new_post'    => array(
                         'post_type' => $acfPostType,
                         'post_status' => $acfPostStatus
                     ),
           'return'     => '%post_url%',
           'submit_value'  => $acfPostSubmit
                  ));
        // End             
        $html = ob_get_contents(); 
        ob_end_clean();
        return $html;
    }
  • Hi @buylov

    I’ve just tested your code, and the form showed up as it should be. If it didn’t show up on your end, it’s possible because you set '' as the value. Please take a look at this page to learn more about acf_form() default value: http://www.advancedcustomfields.com/resources/acf_form/.

    I hope this helps.

  • I want to display a ACF frontend form using the shortcode like this:

    [acf-new acfpostidid=”new_post” acfposttitle=”true” acfpostcontent=”true” acfposttype=”post” acfpoststatus=”publish” acfpostreturn=”%post_url%” acfpostsubmit=”Public”]

    How do I modify the code to do this?

  • Hi @buylov

    Here is an example to do it:

    add_shortcode( 'acfNew', 'acf_new' );
    function acf_new( $atts ) {
        ob_start();
        // Attributes
        $atts = shortcode_atts( array(
    	'acfposttitle' => 'false',
    	'acfpostcontent' => 'false'
        ), $atts, 'acfNew' );
        // Start
        acf_form(array(
            'post_title' => $atts['acfposttitle'] === 'true'? true: false,
            'post_content' => $atts['acfpostcontent'] === 'true'? true: false,
            'return'     => '%post_url%',
            'submit_value'  => $acfPostSubmit
        ));
        // End             
        $html = ob_get_contents(); 
        ob_end_clean();
        return $html;
    }

    You can use it like this: [acfNew acfposttitle=true acfpostcontent=true].

    The $atts['acfposttitle'] === 'true'? true: false, is used to convert the string 'true' or 'false' into a boolean type.

    Please learn more the add_shortcode() fucntion here: https://codex.wordpress.org/Function_Reference/add_shortcode.

    Also, you can always hire a developer if you stuck with the issue.

    I hope this helps.

  • I understand the use of “true” and “false.” But how to use a dynamic value? For example, I have more than ten types of custom posts, and I just want to enter the name of the custom post type in the shortcode. For example, [acfNew acfposttype=”books”] or [acfNew acfposttype=”food”] or [acfNew acfposttype=”abc123″].

  • Hi @buylov

    Please learn the code carefully. Also, please learn more about creating a shortcode here: https://codex.wordpress.org/Shortcode_API. You also need to learn about PHP here: http://www.w3schools.com/php/. If you don’t want or don’t have time to learn them, please hire a developer to help you out with it. I’d recommend looking for one on https://studio.envato.com/ or https://www.upwork.com/.

    shortcode_atts() is used to add default values to the attributes. So you need to set the default values from acf_form() in this function. Something like this:

    $atts = shortcode_atts( array(
        'acfposttype' => 'post',
        ...

    After that, you can get the attributes using the key like this: $atts['acfposttype'], so it looks like this:

    acf_form(array(
        'post_title' => $atts['acfposttitle'] === 'true'? true: false,
        'new_post'		=> array(
            'post_type'	=> $atts['acfposttype'],
            'post_status'	=> 'publish',
        ),
        ...

    I hope this makes sense.

  • Thank you, my problem is solved!

  • Hi,

    This is almost exactly what I was after, however it’s wrapping the form in pre and code tags. How do I render the HTML without these?

    Many thanks!

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Shortcode ACF frontend form’ is closed to new replies.