-
-
Save ControlledChaos/ff1608f7885b3d8859f21d389b333ff3 to your computer and use it in GitHub Desktop.
ACF Form Shortcode Plugin for the Front End
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: ACF Front End Forms | |
Plugin URI: https://navarrojr.com | |
Description: Create forms on the front end of the site from ACF field groups. | |
Version: 0.0.1 | |
Author: Dave Navarro, Jr. | |
Author URI: https://navarrojr.com | |
*/ | |
//* Add required acf_form_head() function to head of page | |
function am_do_acf_form_head() { | |
if ( !is_admin() ) | |
acf_form_head(); | |
} | |
add_action( 'get_header', 'am_do_acf_form_head', 1 ); | |
//* Add custom body class to the head | |
function am_acf_form_class( $classes ) { | |
$classes[] = 'acf-form-front'; | |
return $classes; | |
} | |
add_filter( 'body_class', 'am_acf_form_class' ); | |
//* ACF Form Short Code | |
function am_sc_acf_form( $attr ) { | |
$cpt = $attr['cpt']; | |
$post_id = $_GET['postid']; | |
$title = ( $attr['title'] == 1 ) || ( $attr['title'] == 'y' ) || ( $attr['title'] == 'yes' ); | |
$content = ( $attr['content'] == 1 ) || ( $attr['content'] == 'y' ) || ( $attr['content'] == 'yes' ); | |
$submit = 'submit'; | |
if ( empty( $post_id ) ) { | |
$post_id = 'new_post'; | |
} else { | |
$submit = 'update'; | |
} | |
$options = array( 'id' => 'acf_form', | |
'post_id' => $post_id, | |
'label_placement' => 'top', | |
'instruction_placement' => 'field', | |
'new_post' => array( 'post_type' => $cpt, 'post_status' => 'publish'), | |
'submit_value' => $submit, | |
'updated_message' => __( 'Client Updated', 'acf' ), | |
'post_title' => $title, | |
'post_content' => $content, | |
'return' => '%post_url%', | |
); | |
ob_start(); | |
acf_form( $options ); | |
$ret = ob_get_contents(); | |
ob_end_clean(); | |
return $ret; | |
} | |
add_shortcode( 'do_acf_form', 'am_sc_acf_form' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment