BuddyDev

Search

How can I echo this code in header

  • Inactive
    Level: Enlightened
    Posts: 28
    Kikismedia on #31631

    After implementation of this code I was able to see the generated on user profile via backend but now I want to echo on my site header so all members can see there registration number

    /**
    * Upon user registration, generate a random number and add this to the usermeta table
    *
    * @param required integer $user_id The ID of the newly registerd user
    */
    add_action(‘user_register’, ‘my_on_user_register’);
    function my_on_user_register($user_id){

    $args = array(
    ‘length’ => 6,
    ‘before’ => date(“Y”)
    );
    $random_number = my_random_string($args);
    update_user_meta($user_id, ‘random_number’, $random_number);

    }

    /**
    * Output additional data to the users profile page
    *
    * @param WP_User $user Object properties for the current user that is being displayed
    */
    add_action(‘show_user_profile’, ‘my_extra_user_profile_fields’);
    add_action(‘edit_user_profile’, ‘my_extra_user_profile_fields’);
    function my_extra_user_profile_fields($user){

    $random_number = get_the_author_meta(‘random_number’, $user->ID);
    ?>
    <h3><?php _e(‘Custom Properties’); ?></h3>

    <table class=”form-table”>
    <tr>
    <th><label for=”address”><?php _e(‘Random Number’); ?></label></th>
    <td><?php echo $random_number; ?></td>
    </tr>
    </table>
    <?php
    }

  • Keymaster
    (BuddyDev Team)
    Posts: 19487
    Brajesh Singh on #31638

    Please share the code on pastebin and link me or please use backticks to post code block here.

  • Inactive
    Level: Enlightened
    Posts: 28
    Kikismedia on #31651

    Here is the link to the pastbin

    https://pastebin.com/bcgwRWse

  • Keymaster
    (BuddyDev Team)
    Posts: 19487
    Brajesh Singh on #31660

    Please share your original code. You have copied from your previous post and shared it. It is unusable since the quotes are encoded and I ca not use them.

    Regards
    Brajesh

  • Inactive
    Level: Enlightened
    Posts: 28
    Kikismedia on #31663

    Ok I will do that

  • Inactive
    Level: Enlightened
    Posts: 28
    Kikismedia on #31664
  • Keymaster
    (BuddyDev Team)
    Posts: 19487
    Brajesh Singh on #31681

    Hi,
    Thank you.

    There is no standard hook to inject into your site header(using wp_head will put it in meta).

    I can help you put this in the footer though.

    
    
    add_action( 'wp_footer', function () {
    
    	if ( is_user_logged_in() ) {
    		echo "Reg Number: " . get_user_meta( get_current_user_id(), 'random_number', true );
    	}
    } );
    
    

    You can either use this or if your theme provides some action for injecting in header, you may use that.

    Regards
    Brajesh

  • Inactive
    Level: Enlightened
    Posts: 28
    Kikismedia on #31684

    What if I want to call it in any page how can I do that , for example a custom page like profile

You must be logged in to reply to this topic.

This topic is: not resolved