Php WordPress的自定义元框和字段:根据用户编辑页面更改目录上载

Php WordPress的自定义元框和字段:根据用户编辑页面更改目录上载,php,wordpress,custom-fields,meta-boxes,Php,Wordpress,Custom Fields,Meta Boxes,我使用为具有自定义角色cliente的不同用户上载文件 我能够显示字段并使其工作(尽管它们仅在编辑用户时显示,而不是在创建用户时显示,但这是另一个问题)。我现在试图实现的是为不同的用户将文件上传到不同的文件夹 代码如下: // Here we add a new user role, "cliente" add_role( 'cliente', 'Cliente' ); add_filter('wp_handle_upload_prefilter', 'my_upload_prefilter'

我使用为具有自定义角色
cliente
的不同用户上载文件

我能够显示字段并使其工作(尽管它们仅在编辑用户时显示,而不是在创建用户时显示,但这是另一个问题)。我现在试图实现的是为不同的用户将文件上传到不同的文件夹

代码如下:

// Here we add a new user role, "cliente"
add_role( 'cliente', 'Cliente' );

add_filter('wp_handle_upload_prefilter', 'my_upload_prefilter');
add_filter('wp_handle_upload', 'my_upload_postfilter');

function my_upload_prefilter( $file ) {
    add_filter('upload_dir', 'custom_upload_dir');
    return $file;
}

function my_upload_postfilter( $fileinfo ) {
    remove_filter('upload_dir', 'custom_upload_dir');
    return $fileinfo;
}

function custom_upload_dir( $path ) {
    global $pagenow;

    // Check if we are on the user-edit.php page
    if ( $pagenow == 'user-edit.php' && isset($_GET['user_id']) ) {
        // Set the role we want to change the path for
        $role_to_check = 'cliente';
        // Get a bunch of user info for later use
        $user_id = filter_var( $_GET['user_id'], FILTER_SANITIZE_NUMBER_INT );
        $meta = get_user_meta($user_id);
        $roles = unserialize($meta['wp_capabilities'][0]);
        // If we are on the chosen role page, set the $customdir to first_name + last_name
        if ( !empty($roles[$role_to_check]) ) {
            $customdir = '/' . $meta['first_name'][0] . $meta['last_name'][0];
        }
    } else {
        // Here we are not on the user-edit.php page. This is just a check to prove that WP is not recognizing the correct page, maybe because we are doing an Ajax call when this function is called. Confusing.
        $customdir = '/did-not-work';
    }

    // If there is any error, just return the $path and abort the rest.
    if ( !empty( $path['error'] ) ) {
        return $path;
    }

    // Here we set the new $path with the $customdir set above
    $path['path']    = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
    $path['url']     = str_replace($path['subdir'], '', $path['url']);      
    $path['subdir']  = $customdir;
    $path['path']   .= $customdir; 
    $path['url']    .= $customdir; 
    return $path;

}
从我运行的一些检查来看,我的代码似乎确实检索了用户id和数据库中存储的数据,但在上载图像时没有检测到它。这可能与我们通过Ajax或类似的方式上传图像有关吗

我不想基于当前登录的用户进行上传,但作为超级管理员,我正在使用edit-user.php页面对用户进行编辑


任何帮助都将不胜感激。

我自己解决了。首先是正确的代码,然后是解释:

// Here we add a new user role, "cliente".
add_role( 'cliente', 'Cliente' );

// These are the filters we need to add in order to modify the default upload path.
add_filter('wp_handle_upload_prefilter', 'my_upload_prefilter');
add_filter('wp_handle_upload', 'my_upload_postfilter');

function my_upload_prefilter( $file ) {
    add_filter('upload_dir', 'custom_upload_dir');
    return $file;
}
function my_upload_postfilter( $fileinfo ) {
    remove_filter('upload_dir', 'custom_upload_dir');
    return $fileinfo;
}

function custom_upload_dir( $path ) {
    // When uploading, the file gets sent to upload_async.php, so we need to take the referral page in order to be able to get the user_id we need. We then take the query string, pass it through parse_str and store it in a $query_array. Took me a while to figure it out, but now it works like a charm.
    $actual_page = $_SERVER['HTTP_REFERER'];
    parse_str( parse_url($actual_page, PHP_URL_QUERY), $query_array );

    // Check if we are uploading from the user-edit.php page.
    if ( strpos($actual_page, 'user-edit.php') ) {
        // Set the role we want to change the path for.
        $role_to_check = 'cliente';
        // Get a bunch of user info for later use
        $user_id = filter_var( $query_array['user_id'], FILTER_SANITIZE_NUMBER_INT );
        $meta = get_user_meta( $user_id );
        $roles = unserialize( $meta['wp_capabilities'][0] );
        // If we are on the chosen role page, set the $customdir to first_name + last_name
        if ( !empty($roles[$role_to_check]) ) {
            $customdir = '/docs/' . $meta['first_name'][0] . $meta['last_name'][0];

            // If there is any error, just return the $path and abort the rest.
            if ( !empty( $path['error'] ) ) {
                return $path;
            }

            // Here we set the new $path with the $customdir set above
            $new_subdir = $customdir . $path['subdir'];
            $path['path']    = str_replace( $path['subdir'], $new_subdir, $path['path'] );
            $path['url']     = str_replace( $path['subdir'], $new_subdir, $path['url'] );
            $path['subdir']  = $new_subdir;
            return $path;
        }
    } else {
        // We are not uploading from user-edit.php, so go ahead as per default.
        return $path;
    }
}
问题是通过Ajax上传时,
$pagenow
正确地存储了
async upload.php
页面,而不是我们所在的url。我只需通过php
$\u服务器['HTTP\u REFERER']
(请注意,
REFERER
打字错误是因为HTTP规范中的遗留打字错误,很有趣)

还请注意,PHP规范不鼓励使用
HTTP\u REFERER
,因为根据服务器配置,它可能会产生意外的结果,但在这种情况下,我应该完全控制服务器,所以这应该不是问题。如果您遇到任何问题,我建议您检查一下

一旦我有了正确的url,我就能够解析它并检查我们是否在
user edit.php
,如果是,从查询字符串中获取
user\u id
,然后从那里继续

我花了一段时间才弄明白,但事后看来这很容易


希望它将来能帮助其他人。

等一下,这是否意味着当您(通过AJAX)上载文件时,您无法检索任何其他信息?(…通过另一个ajax请求?)我不知道。这只是猜测。在页面上检查时检索数据(我在不同的情况下进行了几个var_转储),但在通过Ajax上载时,
if($pagenow=='user edit.php'&&isset($\u GET['user_id'))
语句失败,文件上载到文件夹
/不工作
。您是否尝试过记录(回显)这两个$\u GET['user_id']现在呢?只是想知道问题是在GET请求中还是在$pagenow变量中。是的,我这样做了,并按预期记录:
user edit.php
和正在编辑的用户id。但是上传文件时这些文件没有被发送,因此导致
$customdir
变量被设置为
/不起作用。我想这可能是因为WordPress调用钩子的时间和方式,但我不知道如何调试它们。是的,那么你应该检查为什么不发送钩子。你的上传脚本从哪里调用?