admin管理员组

文章数量:1134244

I'm sorry for my english... I am using the following code to create separate media directories for each author.. ( Thanks to )

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/data/www/mydomain/wp-content/plugins/!functions/my-functions.php:53
Stack trace:
#0 /home/mydomain/data/www/mydomain/wp-includes/class-wp-hook.php(308): per_user_upload_dir(Array)
#1 /home/mydomain/data/www/mydomain/wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array)
#2 /home/mydomain/data/www/mydomain/wp-includes/functions.php(2363): apply_filters('upload_dir', Array)
#3 /home/mydomain/data/www/mydomain/wp-content/plugins/woocommerce/includes/class-woocommerce.php(305): wp_upload_dir(NULL, false)
#4 /home/mydomain/data/www/mydomain/wp-content/plugins/woocommerce/includes/class-woocommerce.php(183): WooCommerce->define_constants()
#5 /home/mydomain/data/www/mydomain/wp-content/plugins/woocommerce/includes/class-woocommerce.php(144): WooCommerce->__construct()
#6 /home/terraturis in /home/mydomain/data/www/mydomain/wp-content/plugins/!functions/my-functions.php on line 53

I would be grateful for any solution.

Thank you!

本文标签: Problem with quotisuserloggedin()quot and woocommerce (Code for custom media upload for each author)