Example of an Ajaxified WordPress Admin using OOP Style
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | class Td_Admin { public function __construct() { add_action('admin_menu', array($this, 'cvf_td_register_site_options') ); add_action('wp_ajax_cvf_td_update_trade_settings', array($this, 'cvf_td_update_trade_settings') ); add_action('admin_enqueue_scripts', array($this, 'cvf_td_load_admin_scripts') ); } public static function cvf_td_load_admin_scripts() { wp_enqueue_style( 'admin-css', get_template_directory_uri() . '/css/admin.css', false, "1.1.1", false); } public static function cvf_td_register_site_options() { add_submenu_page( 'edit.php?post_type=page', 'Settings', 'Settings', 'administrator', 'trade-settings', 'Td_Admin::cvf_td_trade_settings' ); } public static function cvf_td_trade_settings(){ ?> <h2>Wp Trade Settings</h2> <span class = "update-trade-settings-response td-response"></span> <table class="form-table"> <tbody> <tr> <th><label for="limit_repost">Items can be reposted after:</label></th> <td><input type="text" name="limit_repost" id="limit_repost" value="<?php echo get_option('td_limit_repost'); ?>" placeholder = "" class="regular-text"> <span class="description">Specify in hours, Default is: 24</span></td> </tr> <tr> <th><label for="max_image_upload">Maximum Image Upload</label></th> <td><input type="text" name="max_image_upload" id="first_name" value="<?php echo get_option('td_max_image_upload'); ?>" class="regular-text"></td> </tr> <tr> <th><label for="max_image_size">Maximum Image Size</label></th> <td><input type="text" name="max_image_size" id="max_image_size" value="<?php echo get_option('td_max_image_size'); ?>" class="regular-text"><span class="description">Specify in kilobytes, Default is: 500</span></td> </tr> <tr> <th><label for="currency_symbol">Currency Symbol</label></th> <td> <select name="currency_symbol" class = "currency_symbol"> <option value = "dollar">$ Dollar</option> <option value = "euro">€ Euro</option> <option value = "pound">£ Pound</option> <option value = "yen">¥ Yen</option> <option value = "peso">₱ Peso</option> </select> </td> </tr> <tr> <th><label for="currency_symbol">Editor</label></th> <td> <?php $content = ''; $editor_id = 'mycustomeditor'; wp_editor( $content, $editor_id ); ?> </td> </tr> </tbody> </table> <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary update_wp_trade_settings" value="Update Settings"></p> <script type="text/javascript"> jQuery(document).ready(function($) { var current_currency = '<?php echo get_option('td_currency_symbol'); ?>'; if(current_currency){ $(".currency_symbol").val(current_currency); } $('input').on('focus', function(){ $('input').removeClass('border-red'); }); $("body").on("click", ".update_wp_trade_settings", function(e) { e.preventDefault(); if(!$("input[name=limit_repost]").val()) { $("input[name=limit_repost]").addClass('border-red'); } else if(!$("input[name=max_image_upload]").val()) { $("input[name=max_image_upload]").addClass('border-red'); } else if(!$("input[name=max_image_size]").val()) { $("input[name=max_image_size]").addClass('border-red'); } else { $(".update-trade-settings-response").html('<p><img src = "<?php bloginfo('template_url'); ?>/images/loading.gif" class = "loader" /></p>'); var data = { 'action': 'cvf_td_update_trade_settings', 'cvf_action': 'update_trade_settings', 'limit_repost': $("input[name=limit_repost]").val(), 'max_image_upload': $("input[name=max_image_upload]").val(), 'max_image_size': $("input[name=max_image_size]").val(), 'currency_symbol': $(".currency_symbol").val(), 'text_area_text': tinymce.editors.mycustomeditor.getContent() }; $.post(ajaxurl, data, function(response) { element = $(".update-trade-settings-response"); $('html, body').animate({scrollTop: $(element).offset().top-100}, 150); $(element).html(response); }); } }); }); </script> <?php } public static function cvf_td_update_trade_settings(){ $errors = array(); $currency_list = array('dollar', 'euro', 'pound', 'yen', 'peso'); if(isset($_POST['cvf_action']) && $_POST['cvf_action'] == 'update_trade_settings'){ foreach($_POST as $k => $value){ $_POST[$k] = sanitize_text_field($value); } if (empty($errors) === true) { if(!is_numeric($_POST['limit_repost'])){ $errors[] = 'Only numeric values are allowed in "Items can be reposted after"'; } elseif ($_POST['limit_repost'] < 0) { $errors[] = 'Repost limit cannot be negative'; } if(!is_numeric($_POST['max_image_upload'])){ $errors[] = 'Only numeric values are allowed in "Maximum Image Upload"'; } elseif ($_POST['max_image_upload'] < 0) { $errors[] = 'Maximum Image Upload cannot be negative'; } if(!is_numeric($_POST['max_image_size'])){ $errors[] = 'Only numeric values are allowed in "Maximum Image Size"'; } elseif ($_POST['max_image_size'] < 10) { $errors[] = '"Maximum Image Upload" must be greater than 10kb'; } if(!in_array($_POST['currency_symbol'], $currency_list)){ $errors[] = 'Invalid Currency Symbol.'; } } if(empty($_POST) === false && empty($errors) === true) { update_option('td_limit_repost', $_POST['limit_repost']); update_option('td_max_image_upload', $_POST['max_image_upload']); update_option('td_max_image_size', $_POST['max_image_size']); update_option('td_currency_symbol', $_POST['currency_symbol']); update_option('td_text_area_text', $_POST['text_area_text']); echo ' <div id="message" class="updated"> <p><strong>Settings updated.</strong></p> </div>'; } elseif (empty($errors) === false) { echo ' <div id="message" class="error"> <ul>'; foreach ($errors as $error) { echo '<li><strong>' . $error . '</strong></li>'; } echo '</ul> </div>'; } } exit(); } } new Td_Admin(); |
Do you need help with a project? or have a new project in mind that you need help with?
Contact Me