Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form id="um_form2" method="POST">
- <p>
- <label for="um_key">
- Enter your last name:
- <input type="text" name="um_key" id="um_key" value="" style="width:100%;" />
- </label>
- <input type="submit" value="Submit"/>
- </p>
- </form>
- function theme_enqueue2() {
- $theme_url = get_template_directory_uri(); // Used to keep our Template Directory URL
- $ajax_url = admin_url( 'admin-ajax.php' ); // Localized AJAX URL
- // Register Our Script for Localization
- wp_register_script(
- 'um-modifications2', // Our Custom Handle
- "{$theme_url}/scripts/um-modifications2.js", // Script URL, this script is located for me in `theme-name/scripts/um-modifications.js`
- array( 'jquery' ), // Dependant Array
- '1.0', // Script Version ( Arbitrary )
- true // Enqueue in Footer
- );
- // Localize Our Script so we can use `ajax_url`
- wp_localize_script(
- 'um-modifications2',
- 'ajax_url',
- $ajax_url
- );
- // Finally enqueue our script
- wp_enqueue_script( 'um-modifications2' );
- }
- add_action( 'wp_enqueue_scripts', 'theme_enqueue2' );
- function um_modifications_callback2() {
- // Ensure we have the data we need to continue
- if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {
- // If we don't - return custom error message and exit
- header( 'HTTP/1.1 400 Empty POST Values' );
- echo 'Could Not Verify POST Values.';
- exit;
- }
- $user_id = get_current_user_id(); // Get our current user ID
- $um_val = sanitize_text_field( $_POST['company_name'] ); // Sanitize our user meta value
- update_user_meta( $user_id, company_name, $um_val ); // Update our user meta
- wp_update_user( array(
- 'ID' => $user_id,
- // 'user_email' => $um_user_email,
- )
- );
- echo "<script>window.close();</script>";
- exit;
- }
- add_action( 'wp_ajax_nopriv_um_cb2', 'um_modifications_callback2' );
- add_action( 'wp_ajax_um_cb2', 'um_modifications_callback2' );
- // Declare our JQuery Alias
- jQuery( 'document' ).ready( function( $ ) {
- // Form submission listener
- $( '#um_form2' ).submit( function() {
- // Grab our post meta value
- var um_val2 = $( '#um_form2 #um_key' ).val();
- // Do very simple value validation
- if( $( '#um_form2 #um_key' ).val().length ) {
- $.ajax( {
- url : ajax_url, // Use our localized variable that holds the AJAX URL
- type: 'POST', // Declare our ajax submission method ( GET or POST )
- data: { // This is our data object
- action : 'um_cb2', // AJAX POST Action
- 'company_name': um_val, // Replace `um_key` with your user_meta key name
- }
- } )
- .success( function( results ) {
- alert("success!");
- window.location.href=window.location.href;
- } )
- .fail( function( data ) {
- console.log( data.responseText );
- console.log( 'Request failed: ' + data.statusText );
- } );
- } else {
- // Show user error message.
- }
- return false; // Stop our form from submitting
- } );
- } );
Add Comment
Please, Sign In to add comment