Secure my plugin
-
Hi there
I have some questions about nonce function.
How do I make that, and how do I best secure I best the plugin.Have made a plugin to make a top banner, where I have a insert update delete function in ajax.
I have made my database like this:
id int(10) unsigned NOT NULL AUTO_INCREMENT, (hidden)_top_banner_name varchar(255) NOT NULL, (hidden)_top_banner_name_color varchar(20) NOT NULL, (hidden)_top_banner_name_top_height varchar(10) NOT NULL, (hidden)_top_banner_name_top_text_line varchar(3) NOT NULL, (hidden)_top_banner_name_top_start_date varchar(10) NOT NULL, (hidden)_top_banner_name_top_stop_date varchar(10) NOT NULL, (hidden)_top_banner_name_media varchar(255) NOT NULL, (hidden)_image_attachment_id int(10) NOT NULL, PRIMARY KEY (<code>id</code>)So there is no possibility of too many characters, but have i am not so good to it, it is my first plugin i have made.
Here is some code to se my ajax handler:
function ajax_(hidden)_insert_top_banner() { // The $_REQUEST contains all the data sent via ajax global $wpdb; $table_name = $wpdb->prefix . "topbanner"; if ( isset($_REQUEST) ) { $(hidden)_top_banner_name = wp_kses_post($_REQUEST['(hidden)_top_banner_name']); $(hidden)_top_banner_name_color = sanitize_hex_color($_REQUEST['(hidden)_top_banner_name_color']); $(hidden)_top_banner_name_top_text_line = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_top_text_line']); $(hidden)_top_banner_name_top_height = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_top_height']); $(hidden)_top_banner_name_top_start_date = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_top_start_date']); $(hidden)_top_banner_name_top_stop_date = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_top_stop_date']); $(hidden)_top_banner_name_media = sanitize_text_field($_REQUEST['(hidden)_top_banner_name_media']); $(hidden)_image_attachment_id = sanitize_text_field($_REQUEST['(hidden)_image_attachment_id']); $wpdb->query($wpdb->prepare( " INSERT INTO $table_name ( (hidden)_top_banner_name, (hidden)_top_banner_name_color, (hidden)_top_banner_name_top_text_line, (hidden)_top_banner_name_top_height, (hidden)_top_banner_name_top_start_date, (hidden)_top_banner_name_top_stop_date, (hidden)_top_banner_name_media, (hidden)_image_attachment_id ) VALUES ( %s, %s, %s, %s, %s, %s, %s, %d ) ", array( $(hidden)_top_banner_name, $(hidden)_top_banner_name_color, $(hidden)_top_banner_name_top_text_line, $(hidden)_top_banner_name_top_height, $(hidden)_top_banner_name_top_start_date, $(hidden)_top_banner_name_top_stop_date, $(hidden)_top_banner_name_media, $(hidden)_image_attachment_id ) ) ); } // Always die in functions echoing ajax content die(); } add_action( 'wp_ajax_ajax_(hidden)_insert_top_banner', 'ajax_(hidden)_insert_top_banner' );And here is the js:
(function($) { $(document).on('click', '#insert_submit', function (e) { e.preventDefault(); // We'll pass this variable to the PHP function example_ajax_request var (hidden)_top_banner_name = tinymce.get('(hidden)_top_banner_name').getContent(); var (hidden)_top_banner_name_color = $('#(hidden)_top_banner_name_color').val(); var (hidden)_top_banner_name_top_text_line = $('#(hidden)_top_banner_name_top_text_line').val(); var (hidden)_top_banner_name_top_height = $('#(hidden)_top_banner_name_top_height').val(); var (hidden)_top_banner_name_top_start_date = $('#(hidden)_top_banner_name_top_start_date').val(); var (hidden)_top_banner_name_top_stop_date = $('#(hidden)_top_banner_name_top_stop_date').val(); var (hidden)_top_banner_name_media = $('#(hidden)_top_banner_name_media').val(); var (hidden)_image_attachment_id = $('#(hidden)_image_attachment_id').val(); // This does the ajax request $.ajax({ url: (hidden)_ajax_obj.ajaxurl, // or example_ajax_obj.ajaxurl if using on frontend data: { 'action': 'ajax_(hidden)_insert_top_banner', '(hidden)_top_banner_name' : (hidden)_top_banner_name, '(hidden)_top_banner_name_color' : (hidden)_top_banner_name_color, '(hidden)_top_banner_name_top_text_line' : (hidden)_top_banner_name_top_text_line, '(hidden)_top_banner_name_top_height' : (hidden)_top_banner_name_top_height, '(hidden)_top_banner_name_top_start_date' : (hidden)_top_banner_name_top_start_date, '(hidden)_top_banner_name_top_stop_date' : (hidden)_top_banner_name_top_stop_date, '(hidden)_top_banner_name_media' : (hidden)_top_banner_name_media, '(hidden)_image_attachment_id' : (hidden)_image_attachment_id, }, success:function(data) { // This outputs the result of the ajax request console.log(data); }, complete:function(data){ location.reload(); }, error: function(errorThrown){ console.log(errorThrown); } }); }); })(jQuery);wp_enqueue_script( '(hidden)-ajax-script', plugin_dir_url( __FILE__ ) . '/js/ajax_insert.js', array('jquery') ); // The wp_localize_script allows us to output the ajax_url path for our script to use. wp_localize_script( '(hidden)-ajax-script', '(hidden)_ajax_obj', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );Best regards
MortenThe page I need help with: [log in to see the link]
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘Secure my plugin’ is closed to new replies.