I'm sorry for my english... I am using the following code to create separate media directories for each author.. ( Thanks to https://wordpress.stackexchange.com/a/400352/220907 )

function per_user_upload_dir( $original ){
  $modified = $original;
  if ( is_user_logged_in() && current_user_can('instructor') ) {
      $current_user = wp_get_current_user();
      $subdir = 'user_docs/'.$current_user->user_login;
      $modified['subdir'] = $subdir;
      $modified['url'] = $original['baseurl'] . '/' . $subdir;
      $modified['path'] = $original['basedir'] . DIRECTORY_SEPARATOR . $subdir;
  return $modified;
add_filter( 'upload_dir', 'per_user_upload_dir');

It works well, but the site dies with errors if we activate woocommerce

[03-Aug-2023 09:01:18 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function is_user_logged_in() in /home/mydomain.com/data/www/mydomain/wp-content/plugins/!functions/my-functions.php:53
Stack trace:
#0 /home/mydomain.com/data/www/mydomain/wp-includes/class-wp-hook.php(308): per_user_upload_dir(Array)
#1 /home/mydomain.com/data/www/mydomain/wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array)
#2 /home/mydomain.com/data/www/mydomain/wp-includes/functions.php(2363): apply_filters('upload_dir', Array)
#3 /home/mydomain.com/data/www/mydomain/wp-content/plugins/woocommerce/includes/class-woocommerce.php(305): wp_upload_dir(NULL, false)
#4 /home/mydomain.com/data/www/mydomain/wp-content/plugins/woocommerce/includes/class-woocommerce.php(183): WooCommerce->define_constants()
#5 /home/mydomain.com/data/www/mydomain/wp-content/plugins/woocommerce/includes/class-woocommerce.php(144): WooCommerce->__construct()
#6 /home/terraturis in /home/mydomain.com/data/www/mydomain/wp-content/plugins/!functions/my-functions.php on line 53

I would be grateful for any solution.

Thank you!

  • when you say include WooCommerce can you be more specific? Activating the plugin? Accessing a specific file? That function being undefined heavily implies that you're hitting a PHP file in a theme or plugin directly instead of using the normal methods. Have you asked this in a WooCommerce community? Questions about 3rd party plugins are offtopic here and not in this stacks scope
    – Tom J Nowell
    Commented Aug 4, 2023 at 13:42
  • For what it's worth, current_user_can('instructor') doesn't do anything. Even current_user_can('administrator') doesn't actually check if the user is an administrator. Admins can do anything. So if you put current_user_can('fight_a_gorilla_and_win') it will return true for administrators because they have permission to do anything. Check here: developer.wordpress.org/reference/functions/current_user_can and the list of capabilities is here: wordpress.org/documentation/article/roles-and-capabilities Commented Aug 4, 2023 at 16:23
  • @TomJNowell This code works fine and completes the tasks, but there are problems after activating woocommerce. I am sorry for my English. I can't always translate thoughts correctly.
    – Andrei Ka
    Commented Aug 4, 2023 at 18:23
  • @TonyDjukic current_user_can('instructor') doesn't matter, we can remove that. I gave an example from the original code. Error points to is_user_logged_in() I gave an example above Thanks
    – Andrei Ka
    Commented Aug 4, 2023 at 18:28
  • are you making direct calls to PHP files in your theme/plugins using AJAX/forms/the browser? Have you raised this in a WooCommerce community? If this is related to WooCommerce then it's very likely the question will need to be closed, this isn't a place to get help with WooCommerce/3rd party plugins
    – Tom J Nowell
    Commented Aug 4, 2023 at 18:33


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.