Snippets

Chris Arter Create new field

Created by Chris Arter
add_action('wp_ajax_add_new_shipment', 'tm_add_new_shipment');

function tm_add_new_shipment(){

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

        // JavaScript object with matching property->value pairs that I will turn into key->value below
        $dataFields = $_POST['tmFieldsData'];

        // Clean up that object for JSON decode
        $tempData = str_replace("\\", "", $dataFields);
        
        // Decode JSON and create a key => value array for post meta fields
        $cleanData = json_decode($tempData, true);
        
        
        // Create a new post ID
       $newID = wp_insert_post( array(

            'post_type'=>'shipment',
            'post_status'=>'publish',
            'post_title'=>'shipment',
            'post_content'=>''

        ) );

        
        // Array with key=>value pairs from decoded JSON
        foreach ($cleanData as $key => $value) {
            
            // Let's update that shit.
            update_field($key, $value, $newID);

        }
        
    }

    die;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.