Last active
September 1, 2019 00:10
-
-
Save Cauen/cdf282fd108cbcf32714e2e2549ddab7 to your computer and use it in GitHub Desktop.
Ajax Options Page - Unique DB Option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('wp_ajax_save_i9_analytics_options', 'save_i9_analytics_options'); | |
function save_i9_analytics_options() | |
{ | |
if (!empty($_POST)) { | |
$i9_analytics_data = $_POST["data"]; | |
update_option('i9_analytics_integration_data', json_encode($i9_analytics_data)); | |
echo "OK"; | |
wp_die(); | |
} | |
echo "ERROR"; | |
wp_die(); | |
} | |
add_action('admin_menu', 'i9_analytics_integration'); | |
function i9_analytics_integration() | |
{ | |
add_menu_page('i9 Analytics Integration Settings', 'i9 Analytics Integration', 'administrator', __FILE__, 'i9_analytics_integration_options_page', plugins_url('/images/icon.png', __FILE__)); | |
} | |
function i9_analytics_integration_options_page() | |
{ | |
$options_data = json_decode(get_option('i9_analytics_integration_data'), true); | |
?> | |
<div class="wrap"> | |
<h1>i9 Analytics Integration</h1> | |
<form id="i9_analytics" method="post" action="options.php"> | |
<table class="form-table"> | |
<tr valign="top"> | |
<th scope="row">Heap ID</th> | |
<td><input type="text" name="i9_heap_id" value="<?php echo esc_attr($options_data['i9_heap_id']); ?>" /></td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row">MixPanel ID</th> | |
<td><input type="text" name="i9_mixpanel_id" value="<?php echo esc_attr($options_data['i9_mixpanel_id']); ?>" /></td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row">Amplitude ID</th> | |
<td><input type="text" name="i9_amplitude_id" value="<?php echo esc_attr($options_data['i9_amplitude_id']); ?>" /></td> | |
</tr> | |
</table> | |
<?php submit_button(); ?> | |
<span id="message"></span> | |
<script> | |
jQuery('#i9_analytics').on('submit', (e) => { | |
jQuery('#message').html('Saving').show(); | |
e.preventDefault(); | |
let formdata = jQuery("#i9_analytics").serializeArray(); | |
let data = {}; | |
jQuery(formdata).each((index, obj) => { | |
if(obj.name.startsWith("i9_")) | |
data[obj.name] = obj.value; | |
}); | |
let postData = { | |
action: 'save_i9_analytics_options', | |
data | |
}; | |
jQuery.post(ajaxurl, postData).done(() => { | |
jQuery('#message').html('General Settings Saved').fadeOut(0).fadeIn(200); | |
}).fail((err) => { | |
jQuery('#message').html('Error: ' + JSON.stringify(err)).fadeOut(0).fadeIn(200); | |
}); | |
}) | |
</script> | |
</form> | |
</div> | |
<?php } ?> |
Author
Cauen
commented
Aug 31, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment