Support

Account

Home Forums ACF PRO acf_form in wp-admin

Solved

acf_form in wp-admin

  • Hi there,

    I realize that acf_form is generally used for creating forms on the front-end, but I actually want to create a form in /wp-admin/ for a specific custom post type.

    Is this possible and, if so, how might I get things to work?

    Thanks in advance!

  • Hi @ryandorn

    Could you please tell me why don’t you use the location rule instead? I think it’s simpler and easier to do.

    If you still want to use acf_form() on the backend, I believe you can do it but you need to create the code to handle the saving process. It will also create issues in the future if it is not handled correctly.

    That being said, I think you can use the acf/render_field and a message field to show the form and then use the acf/save_post to process the posted data.

    I hope this makes sense 🙂

  • Hi James!

    Thanks for the reply. In this particular case I needed to work around the location rule because I need to hook into acf/save_post …as well, in the backend I need to access only one field and would prefer to render it programmatically instead of having another field group.

    I have gotten the field/form to render in the backend via a metabox and acf_form, however the acf/save_post hook is not being executed. Is this because I should use acf/render_field instead …or because acf_form_head() needs to be added (in which case, how would I do that)?

    Here’s my code:

    
    // Meta Box
    	
    function custom_meta_box_markup(){
    
    	$options = array(
    		'post_id'			=> $cpt_id,
    		'post_title'		=> false,
    		'field_groups'		=> array(
    			1234
    		),
    		'fields' => array(
    			'field_012345678901a',
    			'field_012345678901b',
    		),
    		'updated_message' 	=> $updated_message_text,
    		'submit_value' 		=> $submit_value_text,
    		'uploader'			=> 'wp'
    	);
    
    ?>
    	<div class="my-div">
    		<?php acf_form($options); ?>
    	</div>
    <?php
    }
    
    function add_custom_meta_box(){
    	add_meta_box("demo-meta-box", "My Custom Metabox", "custom_meta_box_markup", "dispute", "normal", "high", null);
    }
    
    add_action("add_meta_boxes", "add_custom_meta_box");
    
    // Action Hook
    function my_save_form($cpt_id) {
    	if(is_admin()){
    		global $post
    		$cpt_id = $post->ID;
    		$my_field = get_field('field_012345678901a',$cpt_id);
    		
    		update_post_meta($cpt_id,'test',$my_field); // This is just an example, but it's not working
    	}
    
    };
    add_action('acf/save_post', 'my_save_form', 20);
    

    Huge thanks in advance!

  • Hi @ryandorn

    You need to get the posted data by using the $_POST global variable because ACF won’t be able to save it correctly. Maybe something like this:

    // Action Hook
    function my_save_form($cpt_id) {
        if(is_admin()){
            
            // Check the posted data
            if( isset($_POST['acf']['field_012345678901a']) ){
                
                // Set it to a variable
                $posted_data = $_POST['acf']['field_012345678901a'];
                
                // Update the custom field
                update_field('field_name', $posted_data, $cpt_id);
                
                // or use update_post_meta
                //update_post_meta($cpt_id, 'test', $posted_data);
            }
            
            
        }
    
    };
    add_action('acf/save_post', 'my_save_form', 20);

    Hope this helps 🙂

  • Hey James,

    Thanks for that. After a bunch of fiddling I ended up having to go with a custom AJAX solution, but your replies definitely helped.

    Thanks again!

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

You must be logged in to reply to this topic.