• Hey all,

    So I added some user profile fields to my site and need to be able to update them from one of my pages (long story short I’m using them to store user experience and whatnot for an rpg). My knowledge of php is sort of limited to bits I had to learn to make things functional so I would imagine there’s an easy way to do this (hopefully without me needing to go back and learn AJAX). Anyway, here’s an example of my code in the functions.php

    <?php
    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    
    function my_show_extra_profile_fields( $user ) { ?>
    
        <h3>Extra profile information</h3>
    
        <table class="form-table">
            <tr>
                <th><label for="userxp">Total Experience</label></th>
                <td>
                    <input type="text" name="userxp" id="userxp" value="<?php echo esc_attr( get_the_author_meta( 'userxp', $user->ID ) ); ?>" class="regular-text" /><br />
                    <span class="description">Your Total Experience</span>
                </td>
            </tr>
        </table>
    <?php }
    
    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    
        if ( !current_user_can( 'edit_user', $user_id ) )
            return false;
    
        update_usermeta( $user_id, 'userxp', $_POST['userxp'] );
    }
    ?>

    and then on my page that I want to be able to update the userxp:

    <form method="post" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>">
    <div style="clear: both;">
    <?php if ( get_the_author_meta( 'experience' ) ) { ?>
    <input name="userxp" id="userxp" value="<?php the_author_meta( 'userxp' ); ?>" type="text" readonly>
    <?php } // End check for experience ?>
    </div>
    <input name="updatexp" id="updatexp" class="btn2" type="submit" value="Update Experience">
    <?php wp_nonce_field( 'update-user' ) ?>
    <input name="action" type="hidden" id="action" value="update-user" />
    </form>

    I know the action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" isn’t right it was just the last thing I tried. Anyway, if you guys have any awesome solutions, I would be forever grateful. Thanks in advance

    Thomas

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Update User Profile fields from frontend’ is closed to new replies.