Skip to content

Instantly share code, notes, and snippets.

@ammist
Last active December 7, 2018 22:47
Show Gist options
  • Save ammist/79b225874d56c25a2094fa8a5b039172 to your computer and use it in GitHub Desktop.
Save ammist/79b225874d56c25a2094fa8a5b039172 to your computer and use it in GitHub Desktop.
ACF Front-End Form
/* Append these functions in your functions.php file */
// add the genesis hooks we need based on whether the uesr is logged in, what page we're on, etc.
function prep_genesis(){
if ( ( is_singular( 'my_post_type' ) || is_singular('my_othter_post_type' ) ) && is_user_logged_in () ){
$user = wp_get_current_user();
$post = get_queried_object();
if ( current_user_can( 'edit_post', $post->ID ) && function_exists( 'acf_form_head' ) ){
add_action( 'get_header', 'acf_form_head');
add_action( 'genesis_after_loop' , array( &$this, 'append_editor' ) );
}
}
// add the ACF header to every page that might have an ACF form embedded in it.
if ( is_user_logged_in () && is_singular( 'page' ) && function_exists( 'acf_form_head' ) ) {
add_action( 'get_header', 'acf_form_head');
}
}
function append_editor(){
global $post;
echo '<div class="toggle" data-target="editor_panel">Edit</div><div class="editor_panel">';
acf_form( array(
'post_id' => $post->ID,
'post_type' => $post->post_type,
'post_title' => true,
'post_content' => true,
'submit_value' => 'Update your Item'
));
echo '</div>';
}
<?php
/* Template Name: ACF Form Page */
/* Use this template when you want to create a page that has an Advanced Custom Fields
* form in it.
*/
// you need to know what the group are.
function my_acf_form(){
// this will show all the field groups that apply to a new post of that type.
acf_form( array(
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'my_post_type',
'post_status' => 'publish'
),
'post_title' => true,
'post_content' => true,
'return' => '%post_url%',
'submit_value' => 'Add New'
}
/* ---------------------- Page Rendering --------------------*/
add_action( 'get_header', 'acf_form_head');
add_action( 'genesis_after_loop', 'my_acf_form');
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment