0

Im writing a custom plugin, which will do different stuff for specific admin usernames.

So admin will be able to choose couple admin usernames in plugin settings page in backend via repeateable text field ( Im using Admin Page Framework plugin for generating backend settings page - Link to Plugin on Wordpress Repository )

Then in plugin functions file, I have a following code, to identify if logged in admin username exists in my custom plugins settings and if so then do some specific, if not do something else etc..

  function specific_admin_usernames_function() {

  global $current_user;
  get_currentuserinfo();
  $current_logged_in_user = $current_user->user_login;

  $specific_admin_usernames = AdminPageFramework::getOption( 'MyCustomClassforAdminPageFramework', array( 'custom_section_options', 'specific_admin_usernames' ));
  $specific_admin_usernames_imploded = implode($master_admin_usernames);

  if ( $current_logged_in_user !== in_array($current_logged_in_user, $master_admin_usernames)  ){
    // Do Something  
  }

 }

If I submit only one admin username in plugin, it works fine, problem is when I submit more than one usernames via repeatable field. I tried in_array, array_key_exists functions but none of them worked for me.

What I need to achieve is, function to check and make sure that logged in admin username does NOT exist in the plugins settings specific admin usernames list, but so far I couldnt make it work.

Any ideas please?

Thank You :)

1

I'd assume that $specific_admin_usernames is an array-- so there's no need for this line $specific_admin_usernames_imploded = implode($master_admin_usernames);

Also your if statement is a little backwards. PHP's in_array( $needle, $haystack) doesn't need the $current_logged_in_user !== part. Try:

if ( in_array($current_logged_in_user, $master_admin_usernames)  ){
    // this runs when the current user is IN the master list 
}
  • Thanks for quick response @frogg3862 Yes, that imploded variable is extra there, I used it in another test and just kept it there till I figure this out. As for the solution you gave me, It worked fine. Thanks a million :) – Domeniko Mar 13 '16 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.