Support

Account

Home Forums Backend Issues (wp-admin) Using acf_form in admin Reply To: Using acf_form in admin

  • It actually required a bit of a hack. I can’t post the entire plugin but I can try to hit the highlights. These are just snippets from my larger code base so try and just grasp the concepts.

    First enqueue ACF

    
    add_action('admin_enqueue_scripts', array( $this, 'on_admin_enqueue_scripts' ) );
    	function on_admin_enqueue_scripts() {
    		// global
    		global $pagenow;
    		if ( 'nav-menus.php' != $pagenow ) {
    			return;
    		}
    
    	  	wp_enqueue_script('acf-input');
    	  	wp_enqueue_style('acf-mpa');
    	}
    

    Now you need to dequeue the native walker that normally renders the menu admin and replace it with one you copied and edited to include the acf field.

    
    include_once( 'edit_custom_walker.php' );
    add_filter( 'wp_edit_nav_menu_walker', array( $this, 'acf_menu_edit_walker'), 1, 2 );
    function acf_menu_edit_walker($walker, $menu_id) {
    		$options = get_option( 'dcdc_menu_partials' );
    		$keys = array_keys($options['menu_ids']);
    		if ( in_array($menu_id, $keys) ) {
    			remove_all_filters('wp_edit_nav_menu_walker' );
    	    	return 'Walker_Nav_Menu_Edit_Custom';
    		} 
    		return $walker;	    
    	}
    

    Part of your custom walker needs an acf_form()

    
    <!-- end normal -->
    				    <p>
    				        <?php 
    				        $GLOBALS['current_menu_item_id'] = $item_id;
    					    $acf_options = array(
    					    	'post_id' => $item_id,
    					    	'form' => false,
    					    	/* (array) An array of field group IDs/keys to override the fields displayed in this form */
    					    	'field_groups' => array('group_573e71ff44db1'),
    					    	'return' => '',
    					    );
    					    acf_form( $acf_options );
    				        ?>
    				    </p>
    				    <!-- end acf -->
    

    Then you have to catch the acf form value when the menu is being saved

    
    add_action( 'wp_update_nav_menu_item', array( $this, 'update_custom_nav_fields'), 10, 3 );
    function update_custom_nav_fields ($menu_id, $menu_item_db_id, $args) {
    		if ( isset( $_REQUEST['acf']['field_573e727f24ef4'] ) && is_array( $_REQUEST['acf']['field_573e727f24ef4'] ) ) {
    			$field_value = $_REQUEST['acf']['field_573e727f24ef4'][$menu_item_db_id];
    		    $field = get_field_object('field_573e727f24ef4', $menu_item_db_id, false, false);
    		    acf_update_value( $field_value, $menu_item_db_id, $field );
    		}
    	}
    

    This also a requires a custom nav walker for the front end in order to pick up on the field data. Hope that helps