0

I have a WordPress site with forms on it. I am trying to autofill the name field in the form with the login name from WordPress. How can I do this? I tried some php get current user name but that did not work. Any help will be greatly appreciated.

<?php
//get current username
global $ current_user; 
$current_user=wp_get_current_user();
$cuname=$current_user-> user_name; ?>

// in the  form
value="<?php echo $cuname; ?>" read only
Marc
  • 4,661
  • 3
  • 40
  • 62
mcgergor
  • 1
  • 3

1 Answers1

0

You have user_name when it should be user_login:

$cuname = $current_user->user_login

Colin Marshall
  • 2,142
  • 2
  • 21
  • 31
  • Thanks, where do I add this code in wordpress? in the fuctions php or in a theme. I am kinda new to wordpress. Do I need to add a plugin for this to work? Please help been going crazy all day trying to get this to work. – mcgergor Jan 07 '16 at 04:30
  • Where were you adding it when you tried the code you have in your question? You need to add it to the template file in your theme that has the login form you want to alter. – Colin Marshall Jan 07 '16 at 04:35
  • Thanks, all I am trying to do is to allow the user login name to pre populate a name field in the form so the user does not have to enter their name every time they fill out form. I am so confused with wordpress right now. Any step by step instruction would be great. – mcgergor Jan 07 '16 at 04:43
  • I am using a form builder called perfect forms. In my theme. – mcgergor Jan 07 '16 at 04:51
  • If I was starting out from the beginning how what would I do to accomplish this task. Thanks, for the help. I really appreciate it. – mcgergor Jan 07 '16 at 05:02
  • I'm not sure how it would be done if the form was created using a form builder plugin. If you create the form manually via code in your theme templates it would be easy to use the code in your question (updated with my answer). A form builder plugin handles all the code for you, making customizations like what you want to do more difficult. – Colin Marshall Jan 07 '16 at 05:24
  • The forms were embedded in the pages generated by WP. I manually added the code to embedded the pages into WP site. Would I put the php code in the header of the template theme? Do I need to add a php plugin for this to work? – mcgergor Jan 07 '16 at 18:17