如何在wordpress中添加上传文件的功能

How to add capability to upload file in wordpress

我正在使用 WordPress 的付费主题。主题是关于汽车交易。在这个主题中有一个 Role => Dealer.

当我以经销商身份登录时,我可以编辑我的个人资料(但没有图像选项)。我的客户想要经销商公司徽标的上传字段。我创建了一个 media up loader 并且它可以正常工作但并不完美。当我点击 upload logo 按钮时,媒体加载器弹出窗口,然后我 select 一张图片,现在媒体加载器开始处理,几秒钟后它显示了这个错误:

我在代码中搜索角色,我在父主题中找到了这段代码:

add_role('tdp_dealer', 'Vehicle Dealer', array(
    'read' => true, // True allows that capability
    'edit_dealer_fields' => true
));

然后我搜索上传功能并找到upload_files。我在代码中写了这个上限,但它不起作用。

add_role('tdp_dealer', 'Vehicle Dealer', array(
    'read' => true, // True allows that capability
    'edit_dealer_fields' => true,
    'upload_files' => true,
    'edit_posts' => true
));

然后我也尝试了这段代码,但它也不起作用:

function tdp_add_dealer_caps() {
    // gets the author role
    $role = get_role( 'tdp_dealer' );

    // This only works, because it accesses the class instance.
    // would allow the author to edit others' posts for current theme only
    //$role->add_cap( 'edit_dealer_fields', true );
    $role->add_cap( 'upload_files', true );
}
add_action( 'init', 'tdp_add_dealer_caps');

所以大家指导我如何将文件、图像上传为 dealer user。希望你能理解我的问题。

尝试使用 word press 插件,您可以在其中为自定义用户角色分配上传权限:

Members

你应该 add_action 到 admin_init。它对我有用。

add_action('admin_init', 'allow_tdp_dealer_uploads');
function allow_tdp_dealer_uploads() {
    $tdp_dealer = get_role('tdp_dealer');
    $tdp_dealer->add_cap('upload_files');
}

将此添加到插件的主文件,即 index.php