Support

Account

Home Forums General Issues make field when form submited

Helping

make field when form submited

  • i’m trying to make a form that make field on form submit for specific post.

    can someone help.

    if ( class_exists( "acf" ) )
    {
    	$inc_files = glob( plugin_dir_path( __FILE__ ) . "inc\*.php" );
    	foreach ( $inc_files as $file )
    	{
    		require_once $file;
    	}
    	function fiction_save_json($path)
    	{
    		$path = plugin_dir_path(__FILE__) . "load_json";
    		return $path;
    	}
    	add_filter("acf/settings/save_json","fiction_save_json");
    	add_filter('acf/settings/load_json', 'fiction_acf_json_load_point');
    
    	function fiction_acf_json_load_point( $paths ) {
    
    		// remove original path (optional)
    		unset($paths[0]);
    
    		// append path
    		$paths[] = plugin_dir_path(__FILE__) . '/load_json';
    
    		// return
    		return $paths;
    
    	}
    	add_action( "admin_menu", "fiction_csv_menu" );
    	function fiction_csv_menu()
    	{
    		add_menu_page( "Csv Import", "CSV Import", "administrator", __FILE__, "fiction_csv_page" );
    
    	}
    
    	function fiction_csv_page()
    	{
    		fiction_include( "view/setting.php" );
    	}
    
    	add_action( "admin_post_fiction_save_setting", "fiction_save_setting" );
    	function fiction_save_setting()
    	{
    		if ( ! ( has_valid_nonce() && current_user_can( "edit_pages" ) ) )
    		{
    			exit;
    		}
    		if ( ! function_exists( 'wp_handle_upload' ) )
    		{
    			require_once( ABSPATH . 'wp-admin/includes/file.php' );
    		}
    		$file = $_FILES["file"];
    
    		$uploadedFile = wp_handle_upload( $file, [ "test_form" => false ] );
    //		wp_die(wp_json_encode($uploadedFile));
    		if ( $uploadedFile && ! isset( $uploadedFile["error"] ) )
    		{
    			$csv    = fiction_csv_parse( $uploadedFile["file"] );
    			$header = array_shift( $csv );
    
    			if ( function_exists( 'acf_add_local_field_group' ) ):
    				function fiction_acf_add_local_field_groups()
    				{
    					acf_add_local_field( array(
    						'key'      => 'group_1',
    						'title'    => 'My Group',
    						'fields'   => array(
    							array(
    								'key'               => 'field_1',
    								'label'             => 'Sub Title',
    								'name'              => 'sub_title',
    								'type'              => 'repeater',
    								'layout'            => "table",
    								"sub_fields"        => [
    									[
    										"key"      => "question",
    										"name"     => 'q',
    										"label"    => "Question",
    										"type"     => "text",
    										'readonly' => 1
    									]
    								],
    								'prefix'            => '',
    								'instructions'      => '',
    								'required'          => 0,
    								'conditional_logic' => 0,
    							)
    						),
    						'location' => array(
    							array(
    								array(
    									'param'    => 'post_type',
    									'operator' => '==',
    									'value'    => 'post',
    								),
    							),
    						),
    					) );
    					$repeater = array( 'question' => 5 );
    					update_row( "field_1", 3, $repeater, 22 );
    				}
    
    				add_action( 'acf/init', 'my_acf_add_local_field_groups' );
    			endif;
    		} else
    		{
    			echo $uploadedFile["error"];
    		}
    		fiction_redirect();
    	}
    }
    
    
    <form action="<?php echo esc_html( admin_url( "admin-post.php" ) ); ?>" enctype="multipart/form-data" method="post">
        <input type="hidden" name="action" value="fiction_save_setting">
        <input type="file" name="file">
        <?php wp_nonce_field( 'acme-settings-save', 'acme-custom-message' );?>
    	<?php submit_button(); ?>
    </form>
    
  • What you’re trying to do will not work.

    acf_add_local_field_group() and acf_add_local_field() do not save any information to the database or files. These field groups and fields will only exist on the page load that runs the code. Since you’re running this only when the form is posted, that’s the only time the fields will exist.

    If you want this to be persistent then you need to store the information yourself and then load that information on every page load and use it to create the field groups/fields.

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

You must be logged in to reply to this topic.