Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- // AJAX
- $( '#um_form' ).submit( function() {
- // Grab our post meta value
- var um_val = $( '#um_form #um_key' ).val();
- $.post(my_ajax_obj.ajax_url, { //POST request
- _ajax_nonce: my_ajax_obj.nonce, //nonce
- action: "my_tag_count", //action
- 'custom_meta': um_val, // Replace `um_key` with your user_meta key name
- }//, function() { //callback
- //$('#um_key').val(um_val); //will php be required to place the user id specific value?
- //}
- );
- console.log('um_val: ',um_val);
- // Stop our form from submitting
- return false;
- } );
- //required post-load event
- $( document.body ).trigger( 'post-load' );
- //AJAX
- $ajax_url = admin_url( 'admin-ajax.php' ); // Localized AJAX URL
- // Register Our Script for Localization
- wp_register_script(
- 'custom', // Our Custom Handle
- "/wp-content/themes/myTheme/js/custom.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( 'custom', 'my_ajax_obj', $ajax_url );
- wp_enqueue_script( 'custom' );
- }
- function my_action() {
- // 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();
- //create custom fields
- $initialize = "";
- add_user_meta( $user_id, 'custom_meta', $initialize);
- //collect & sanitize
- $um_val = sanitize_text_field( $_POST['custom_meta'] );
- //update info
- update_user_meta( $user_id, 'custom_meta', $um_val );
- exit;
- }
- //add AJAX for logged in users only
- add_action( 'wp_ajax_my_action', 'my_action' );
- $user_id = get_current_user_id();
- //collect data for rendering
- $test1 = get_user_meta( $user_id, 'custom_meta' );
- ?>
- <form id="um_form" method="POST">
- <label for="um_key">
- User Meta Value:
- <input type="text" name="um_key" id="um_key" value="<?php echo
- $test1;?>" style="width:100%;" />
- </label>
- <input type="submit" value="Submit" />
- </form>
Add Comment
Please, Sign In to add comment