Storing Current User ID in hidden field before saving

0

While user is trying to submit publicForm, I need to save the current logged in user ID in some hidden field. I created some field named 'user_id', and made it hidden using Input Helper. Also, Created some 'Pre-Save Helper' for the pod, which does: <?php

global $current_user;
$current_user_id = $current_user->ID;

$columns['user_id']['value'] = $current_user_id;
?>

But, I always get NULL in the table in DB for that field. So, What would be the problem ?

asked Feb 4 at 6:26

code2be

1

edited Feb 4 at 6:26

OK, I think I should use: $current_user->data->ID Instead of: $current_user->ID to get the ID, But still get NULL in DB ! – code2be Feb 5 at 7:52
I did json_encode and mail for $columns, and got "value":"1" for the user_id column !, That means column get it's value correct, but still get NULL in DB ! – code2be Feb 5 at 7:57
add comment
enter at least 15 characters

3 Answers

0

You need to make a call to wp_get_current_user(); after you declare $current_user.

answered Feb 4 at 9:15

chris.pilko

889

Thanks, But no difference ! – code2be Feb 5 at 7:42
add comment
enter at least 15 characters
0

OK, I think I should use: $current_user->data->ID Instead of: $current_user->ID

to get the ID, But still get NULL in DB !

answered Feb 5 at 7:49

code2be

1

edited Feb 5 at 7:52

add comment
enter at least 15 characters
0

I did some more checking. I have this on one of my sites.

I use this code to set the user's id, and hide the field using CSS.

<?php global $current_user; ?>
<select class="form pick1 <?php echo $name; ?>" disabled>
    <option value="">-- Select one --</option>
<?php
    if (!empty($value))        {
        foreach ($value as $key => $val)             {
            $selected = ($val['id'] === $current_user->ID) ?  'selected' : '';       ?>
    <option value="<?php echo $val['id']; ?>"<?php echo $selected; ?>><?php echo $val['name']; ?></option> 
    <?php
        }
    }
 ?>
</select>

answered Feb 5 at 4:22

chris.pilko

889

add comment
enter at least 15 characters