I am trying to set up a small plugin for my Wordpress to add via shortcode a custom form in a WP page. The form has also a Google map input field (where I can retrieve Lat and Long by moving the marker and save them with the other inputs) and the possibility of moving images uploaded to Dropbox.
I am completely new to set up a plugin, so I put together some code by reading tutorials in the internet. The plugin is really simple for this reason and I am sure it is not complete yet. However I succeeded to create a a setting page to save Google Map API key, lat/long/center to retrieve the map that is shown in the form (these data are saved in wp_options). In the same settings I succeeded to create also a button in order to Authorize Dropbox (the authorization is managed by a second file, named dropbox-oauth-callbak.php). When authorization has been given, in the setting page the button appears as "Disconnect dropbox". At this point I wrote a function to delete the tokens saved in mysql to revoke the authorization, but I cannot manage to activate this function. Nothing happens.
Can anybody help in this? Thanks
<?php
/*
Plugin Name: Sighting Form
Description: Custom form for data collection and CSV download.
Version: 1.0
*/
define( 'sighting_form_url', plugin_dir_url(__FILE__) );
define( 'sighting_form_path', plugin_dir_path( __FILE__ ) );
define( 'sighting_form_ver', '1.0.0' );
$path = preg_replace('/wp-content.*$/','',__DIR__);
require_once($path."wp-load.php");
// Register the plugin settings
function register_sighting_form_settings() {
add_option('sighting_form_google_maps_api_key', '');
add_option('sighting_form_map_zoom', 2);
add_option('sighting_form_map_center', '0,0');
}
add_action('admin_init', 'register_sighting_form_settings');
// Add a menu item to the Settings menu
function add_sighting_form_menu() {
add_options_page('Sighting Form Settings', 'Sighting Form', 'manage_options', 'sighting_form_settings', 'sighting_form_settings_page');
}
add_action('admin_menu', 'add_sighting_form_menu');
// Settings page content
function sighting_form_settings_page() {
?>
<div class="wrap">
<h1>Sighting Form Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('sighting_form_settings');
do_settings_sections('sighting_form_settings');
submit_button();
?>
</form>
</div>
<?php
}
// Add fields to the settings page
function add_sighting_form_settings_fields() {
add_settings_section('sighting_form_main', 'Main Settings', 'sighting_form_section_text', 'sighting_form_settings');
add_settings_field('sighting_form_google_maps_api_key', 'Google Maps API Key', 'sighting_form_api_key_input', 'sighting_form_settings', 'sighting_form_main');
add_settings_field('sighting_form_map_zoom', 'Map Zoom Level', 'sighting_form_map_zoom_input', 'sighting_form_settings', 'sighting_form_main');
add_settings_field('sighting_form_map_center', 'Map Center (latitude,longitude)', 'sighting_form_map_center_input', 'sighting_form_settings', 'sighting_form_main');
// Add Dropbox OAuth settings fields
add_settings_section('sighting_form_dropbox_oauth', 'Dropbox OAuth Settings', 'sighting_form_dropbox_oauth_section_text', 'sighting_form_settings');
add_settings_field('sighting_form_dropbox_client_id', 'Dropbox Access App', 'sighting_form_dropbox_client_id_input', 'sighting_form_settings', 'sighting_form_dropbox_oauth');
}
add_action('admin_init', 'add_sighting_form_settings_fields');
// Section text
function sighting_form_section_text() {
echo '<p>Configure main settings for the Sighting Form plugin.</p>';
}
// Input for Google Maps API Key
function sighting_form_api_key_input() {
$value = get_option('sighting_form_google_maps_api_key', '');
echo "<input type='text' name='sighting_form_google_maps_api_key' value='$value' />";
}
// Input for Map Zoom Level
function sighting_form_map_zoom_input() {
$value = get_option('sighting_form_map_zoom', 2);
echo "<input type='number' name='sighting_form_map_zoom' value='$value' />";
}
// Input for Map Center
function sighting_form_map_center_input() {
$value = get_option('sighting_form_map_center', '0,0');
echo "<input type='text' name='sighting_form_map_center' value='$value' />";
}
// Section text for Dropbox OAuth settings
function sighting_form_dropbox_oauth_section_text() {
echo '<p>Configure Dropbox OAuth settings for the Sighting Form plugin.</p>';
}
// Input for Dropbox Client ID
function sighting_form_dropbox_client_id_input() {
global $wpdb;
$table = 'idp_sigh_form_db_settings';
$result = $wpdb->get_row("SELECT * FROM $table ORDER BY id DESC LIMIT 1");
$authorized = !empty($result) && !empty($result->access_token);
$client_id = '8v34omgcunsmwp1';
$redirect_url = plugins_url('dropbox-oauth-callback.php', __FILE__);
$authorization_url = 'https://www.dropbox.com/oauth2/authorize?client_id=' . $client_id
. '&token_access_type=offline'
. '&response_type=code'
. '&redirect_uri=' . $redirect_url;
if ($authorized) {
echo "<div class='authorized' style='text-align:left'><form id='disconnect-form' method='post' action='".plugins_url('disconnect-dropbox.php', __FILE__)."'>
<input type='hidden' name='disconnect_dropbox' value='1'>
<button id='disconnect-dropbox' type='submit'>Disconnect Dropbox</button></form></div>";
} else {
echo "<div class='no-authorized' style='text-align:left'><button id='authorize-dropbox'><a href='$authorization_url'>Authorize Dropbox</a></button></div>";
}
}
// Validate and save the settings
function save_sighting_form_settings() {
register_setting('sighting_form_settings', 'sighting_form_google_maps_api_key', 'sanitize_text_field');
register_setting('sighting_form_settings', 'sighting_form_map_zoom', 'intval');
register_setting('sighting_form_settings', 'sighting_form_map_center', 'sanitize_text_field');
register_setting('sighting_form_settings', 'sighting_form_dropbox_oauth', 'sanitize_text_field');
register_setting('sighting_form_settings', 'sighting_form_dropbox_client_id', 'sanitize_text_field');
}
add_action('admin_init', 'save_sighting_form_settings');
//then the HTML/PHP for the form to be used in front-end
?>
In the file
plugins_url('disconnect-dropbox.php', __FILE__)
I wrote this function that is not triggered at all
function disconnect_dropbox_action() {
// Disconnect Dropbox
if (isset($_POST['disconnect_dropbox'])) {
global $wpdb;
$table = 'idp_sigh_form_db_settings';
// Delete all rows from the database table
$wpdb->query("TRUNCATE TABLE $table");
// Redirect to the plugin settings page after disconnect
wp_redirect(admin_url('admin.php?page=sighting_form_settings'));
exit;
}
}