0

I have been trying to make a sign-out plugin that allows users to sign out from military training activities that they should be at. After they fill out a form explaining why they are not going to be at an activity, they submit it, and it puts all the data into a mysql database. I am trying to save the user's id to their other data so we can tell who is signing out, but everything I have tried breaks the plugin, and gives me the white screen of death.

Thanks for your help. Here is my code to get the users id:

function f2user() { // Get the current user's info $current_user = wp_get_current_user(); if ( !($current_user instanceof WP_User) ) return; return $current_user->ID; } $usersid = f2user(); $activity = $_POST['activity']; $reason = $_POST['reason']; $explanation = $_POST['explanation']; 
4
  • You need WP_DEBUG on. Otherwise it's a wild goose chase working out what's wrong. See codex.wordpress.org/WP_DEBUG or codex.wordpress.org/Debugging_in_WordPress Commented Jul 13, 2016 at 4:15
  • What means "breaks"? Do you get some errors? I so, what erros? No errors but the purpose of the function is not get? Please, explain. Also, where and when do you execute f2user() function? Please, show us the context of execution. I guess you are trying to use wp_get_current_user() outside WordPress. Commented Jul 13, 2016 at 6:55
  • I have used debug but I am only getting error from another plugin installed Notice: Undefined index: page in /var/www/wordpress/wp-content/plugins/thinkup-panels/thinkup-panels.php on line 6 Commented Jul 13, 2016 at 18:30
  • Maybe I am doing something wrong with f2user(). All I am trying to do is turn $usersid = to the user's ID number so I can save it with their form. Everything else maybe me being stupid and doing unnecessary tasks. Commented Jul 13, 2016 at 18:43

2 Answers 2

1

You can get current user ID easily using get_current_user_id().

$usersid = get_current_user_id(); $activity = $_POST['activity']; $reason = $_POST['reason']; $explanation = $_POST['explanation']; 

For your function f2user() you can change your code to this:

$current_user = wp_get_current_user(); return $current_user->ID; 

According to wp_get_current_user() doc, if no user is signed in, $current_user->ID would return 0.

1
  • I have tried replacing my code with this, and I am still getting the white screen of death with only one theme error that has been around before I started the plugin, and the f2_signout page doesn't even use the theme. Commented Jul 13, 2016 at 18:36
0

Try this. If no user are signed in then wp_get_current_user get fatal error so your plugin breaks. But if you want only user id then use get_current_user_id. If no user signed in then it will return 0. So your plugin will not break. Thanks

function f2user() { // Get the current user's info $current_user = get_current_user_id(); return $current_user; } $usersid = f2user(); 
1
  • The OP could just as well use "get_current_user_id()" without a function alias wrapper. Commented Sep 29 at 9:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.