'wpgancio_field_url' ] ); } add_action( 'update_option_wpgancio_options', 'wpgancio_update_options', 15, 2); // Fires before the administration menu loads in the admin, add our options page add_action( 'admin_menu', 'wpgancio_options_page' ); function wpgancio_update_options ($old_value, $new_value) { $instance_url = $new_value['wpgancio_field_url']; $redirect_uri = get_site_url(null, '/wp-admin/options-general.php?page=wpgancio' ); // create this WP instance as a new client in selected gancio instance $response = wp_remote_post( "$instance_url/api/client", array( 'method' => 'POST', 'body' => array( 'client_name' => 'WPGancio', 'redirect_uris' => esc_html($redirect_uri), 'scopes' => 'event:write', 'website' => 'https://gancio.org' ) )); if ( is_wp_error( $response ) ) { add_settings_error('wpgancio_messages', 'wpgancio_messages', $response->get_error_message()); } else { $data = json_decode( wp_remote_retrieve_body($response), true); update_option('wpgancio_client_secret', $data['client_secret']); update_option('wpgancio_client_id', $data['client_id']); $query = join('&', array( 'response_type=code', 'redirect_uri=' . esc_html($redirect_uri), 'scope=event:write', 'client_id=' . get_option('wpgancio_client_id'), 'state=antani' )); wp_redirect("${instance_url}/authorize?${query}"); exit; } }; function wpgancio_options_page() { // add top level menu page add_options_page( 'Gancio', 'Gancio', 'manage_options', 'wpgancio', 'wpgancio_options_page_html' ); } // instance url field cb // field callbacks can accept an $args parameter, which is an array. // $args is defined at the add_settings_field() function. // wordpress has magic interaction with the following keys: label_for, class. // the "label_for" key value is used for the "for" attribute of the