-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathacf-contact-shortcode.php
executable file
·98 lines (70 loc) · 2.64 KB
/
acf-contact-shortcode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
//add acf_form_head() before header if form shortcode is in post content.
function acf_contact_display_form_head() {
global $post;
if(has_shortcode( $post->post_content, 'acf_contact')) {
acf_form_head();
}
}
add_action( 'get_header', 'acf_contact_display_form_head' );
//[acf_contact] shortcode
function acf_contact_shortcode( $atts ) {
//This code uses acf_form() function which immediately outputs form html
//messing up the admin view of the page containing the shortcode
//For this reason I check if the current page "is_admin" page and return
//immediately to avoid shortcode rendering.
if ( is_admin() )
return false;
$url = acf_get_current_url();
// default shortcode attribute values
// refer to ACF documentation for acf_form() parameters http://www.advancedcustomfields.com/resources/acf_form/
$a = shortcode_atts( array(
'id' => "1",
'form_attributes' => array(),
'html_before_fields' => '',
'html_after_fields' => '',
'submit_value' => __("Submit", 'acf'),
'updated_message' => __("Thank you", 'acf'),
'label_placement' => 'top',
'instruction_placement' => 'label',
'field_el' => 'div',
'uploader' => 'basic',
'return' => add_query_arg( 'updated', 'true', $url ),
), $atts );
$form_id = $a['id'] - 1;
$forms = get_field('forms','option');
$f = $forms[$form_id];
$title = 'New Form Submission'; //This is a temporary page title, which will change when fields are saved.
$post = array(
'post_type' => $f['post_type'],
'post_status' => 'publish',
'post_title' => $title
);
$form = '<div id="acf_contact'.$f["group"].'">';
$form .= "</div>";
//Move form down to content
$form .= "<script>jQuery(function($) {
$('#acf_contactform" . $f["group"] . "').appendTo('#acf_contact" . $f["group"] . "');
});</script>";
echo '<div id="acf_contactform'.$f["group"] . '">';
acf_form(
array(
'post_id' => 'new_post',
'field_groups' => array($f["group"]),
'new_post' => $post,
'form_attributes' => $a['form_attributes'],
'html_before_fields' => $a['html_before_fields'],
'html_after_fields' => $a['html_after_fields'],
'submit_value' => $a['submit_value'],
'updated_message' => $a['updated_message'],
'label_placement' => $a['label_placement'],
'instruction_placement' => $a['instruction_placement'],
'field_el' => $a['field_el'],
'uploader' => $a['uploader'],
'return' => $a['return']
));
echo '</div>';
return $form;
}
add_shortcode( 'acf_contact', 'acf_contact_shortcode' );
?>