WordPress AJAX表单/ jQuery不在后端更新用户元

时间:2018-04-27 04:28:01

标签: javascript ajax wordpress

我试图在我的Wordpress网站上使用AJAX表单来更新我使用ACF创建的自定义用户元(company_name),但它没有更新。如果我使用内置的用户元数据,如first_name或last_name,它可以正常工作。但无论出于何种原因,自定义用户字段都不会更新。

提前致谢

HTML表单

<form id="um_form2" method="POST">
    <p>
        <label for="um_key">
            Enter your last name:&nbsp;&nbsp;
            <input type="text" name="um_key" id="um_key" value="" style="width:100%;" />
        </label>
        <input type="submit" value="Submit"/>
    </p>
</form>

Snippet Script 1

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' );

Snippet 2

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' );

后端JavaScript

// 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
} );
} );

1 个答案:

答案 0 :(得分:0)

找到正确的方法......

<input class="useFontAwesomeFamily" type="text" placeholder="&#xf167;">

而不是update_user_meta