Guest User

Untitled

a guest
Apr 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. <form id="um_form2" method="POST">
  2. <p>
  3. <label for="um_key">
  4. Enter your last name:&nbsp;&nbsp;
  5. <input type="text" name="um_key" id="um_key" value="" style="width:100%;" />
  6. </label>
  7. <input type="submit" value="Submit"/>
  8. </p>
  9. </form>
  10.  
  11. function theme_enqueue2() {
  12.  
  13. $theme_url = get_template_directory_uri(); // Used to keep our Template Directory URL
  14. $ajax_url = admin_url( 'admin-ajax.php' ); // Localized AJAX URL
  15.  
  16. // Register Our Script for Localization
  17. wp_register_script(
  18. 'um-modifications2', // Our Custom Handle
  19. "{$theme_url}/scripts/um-modifications2.js", // Script URL, this script is located for me in `theme-name/scripts/um-modifications.js`
  20. array( 'jquery' ), // Dependant Array
  21. '1.0', // Script Version ( Arbitrary )
  22. true // Enqueue in Footer
  23. );
  24.  
  25. // Localize Our Script so we can use `ajax_url`
  26. wp_localize_script(
  27. 'um-modifications2',
  28. 'ajax_url',
  29. $ajax_url
  30. );
  31.  
  32. // Finally enqueue our script
  33. wp_enqueue_script( 'um-modifications2' );
  34. }
  35. add_action( 'wp_enqueue_scripts', 'theme_enqueue2' );
  36.  
  37. function um_modifications_callback2() {
  38.  
  39. // Ensure we have the data we need to continue
  40. if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {
  41.  
  42. // If we don't - return custom error message and exit
  43. header( 'HTTP/1.1 400 Empty POST Values' );
  44. echo 'Could Not Verify POST Values.';
  45. exit;
  46. }
  47.  
  48. $user_id = get_current_user_id(); // Get our current user ID
  49. $um_val = sanitize_text_field( $_POST['company_name'] ); // Sanitize our user meta value
  50.  
  51. update_user_meta( $user_id, company_name, $um_val ); // Update our user meta
  52. wp_update_user( array(
  53. 'ID' => $user_id,
  54. // 'user_email' => $um_user_email,
  55. )
  56. );
  57.  
  58. echo "<script>window.close();</script>";
  59. exit;
  60. }
  61. add_action( 'wp_ajax_nopriv_um_cb2', 'um_modifications_callback2' );
  62. add_action( 'wp_ajax_um_cb2', 'um_modifications_callback2' );
  63.  
  64. // Declare our JQuery Alias
  65. jQuery( 'document' ).ready( function( $ ) {
  66.  
  67.  
  68. // Form submission listener
  69. $( '#um_form2' ).submit( function() {
  70.  
  71. // Grab our post meta value
  72. var um_val2 = $( '#um_form2 #um_key' ).val();
  73.  
  74. // Do very simple value validation
  75. if( $( '#um_form2 #um_key' ).val().length ) {
  76. $.ajax( {
  77. url : ajax_url, // Use our localized variable that holds the AJAX URL
  78. type: 'POST', // Declare our ajax submission method ( GET or POST )
  79. data: { // This is our data object
  80. action : 'um_cb2', // AJAX POST Action
  81. 'company_name': um_val, // Replace `um_key` with your user_meta key name
  82. }
  83. } )
  84. .success( function( results ) {
  85. alert("success!");
  86.  
  87. window.location.href=window.location.href;
  88.  
  89.  
  90.  
  91. } )
  92. .fail( function( data ) {
  93. console.log( data.responseText );
  94. console.log( 'Request failed: ' + data.statusText );
  95. } );
  96.  
  97. } else {
  98. // Show user error message.
  99. }
  100.  
  101. return false; // Stop our form from submitting
  102. } );
  103. } );
Add Comment
Please, Sign In to add comment