Guest User

Untitled

a guest
Jan 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. $(document).ready(function () {
  2. // AJAX
  3. $( '#um_form' ).submit( function() {
  4. // Grab our post meta value
  5. var um_val = $( '#um_form #um_key' ).val();
  6. $.post(my_ajax_obj.ajax_url, { //POST request
  7. _ajax_nonce: my_ajax_obj.nonce, //nonce
  8. action: "my_tag_count", //action
  9. 'custom_meta': um_val, // Replace `um_key` with your user_meta key name
  10. }//, function() { //callback
  11. //$('#um_key').val(um_val); //will php be required to place the user id specific value?
  12. //}
  13. );
  14. console.log('um_val: ',um_val);
  15. // Stop our form from submitting
  16. return false;
  17. } );
  18. //required post-load event
  19. $( document.body ).trigger( 'post-load' );
  20.  
  21. //AJAX
  22. $ajax_url = admin_url( 'admin-ajax.php' ); // Localized AJAX URL
  23. // Register Our Script for Localization
  24. wp_register_script(
  25. 'custom', // Our Custom Handle
  26. "/wp-content/themes/myTheme/js/custom.js", // Script URL, this script is located for me in `theme-name/scripts/um-modifications.js`
  27. array( 'jquery' ), // Dependant Array
  28. '1.0', // Script Version ( Arbitrary )
  29. true // Enqueue in Footer
  30. );
  31. // Localize Our Script so we can use `ajax_url`
  32. wp_localize_script( 'custom', 'my_ajax_obj', $ajax_url );
  33. wp_enqueue_script( 'custom' );
  34. }
  35.  
  36. function my_action() {
  37. // Ensure we have the data we need to continue
  38. if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {
  39. // If we don't - return custom error message and exit
  40. header( 'HTTP/1.1 400 Empty POST Values' );
  41. echo 'Could Not Verify POST Values.';
  42. exit;
  43. }
  44.  
  45. $user_id = get_current_user_id();
  46. //create custom fields
  47. $initialize = "";
  48. add_user_meta( $user_id, 'custom_meta', $initialize);
  49. //collect & sanitize
  50. $um_val = sanitize_text_field( $_POST['custom_meta'] );
  51. //update info
  52. update_user_meta( $user_id, 'custom_meta', $um_val );
  53.  
  54. exit;
  55. }
  56.  
  57. //add AJAX for logged in users only
  58. add_action( 'wp_ajax_my_action', 'my_action' );
  59.  
  60. $user_id = get_current_user_id();
  61. //collect data for rendering
  62. $test1 = get_user_meta( $user_id, 'custom_meta' );
  63. ?>
  64. <form id="um_form" method="POST">
  65.  
  66. <label for="um_key">
  67. User Meta Value:
  68. <input type="text" name="um_key" id="um_key" value="<?php echo
  69. $test1;?>" style="width:100%;" />
  70. </label>
  71. <input type="submit" value="Submit" />
  72. </form>
Add Comment
Please, Sign In to add comment