最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom post type media upload error with custom user role

matteradmin4PV0评论

I have created a custom post type named product and assigned a new role manager for it. Everythign works fine, except the media uploader for product. I can even upload images without any error directly in the add media section. So, I started to test with default author, and still same error! Amazingly author can upload images successfully, when they are in a default post, not in product!

The error message says: An Error occurred in the upload. Please Try again later.

Below is the code for custom post type:

function cpt_product() {
    $slug = 'product';
    $labels = array( //all the custom labels );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'menu_position' => '5.1',
        'public' => true,
        'has_archive' => true,
        'supports' => array( 'title', 'editor', 'author', 'excerpt', 'custom-fields', 'revisions' ),
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'show_ui' => true,
        'capability_type' => $slug,
        'capabilities' => array(
            'edit_post' => 'edit_' . $slug,
            'read_post' => 'read_' . $slug,
            'delete_post' => 'delete_' . $slug,
            'edit_posts' => 'edit_' . $slug . 's',
            'edit_others_posts' => 'edit_others_' . $slug . 's',
            'publish_posts' => 'publish_' . $slug . 's',
            'read_private_posts' => 'read_private_' . $slug . 's',
            'delete_posts' => 'delete_' . $slug . 's',
            'delete_private_posts' => 'delete_private_' . $slug . 's',
            'delete_published_posts' => 'delete_published_' . $slug . 's',
            'delete_others_posts' => 'delete_others_' . $slug . 's',
            'edit_private_posts' => 'edit_private_' . $slug . 's',
            'edit_published_posts' => 'edit_published_' . $slug . 's'
        ),
        'map_meta_cap' => true
    );

    register_post_type( $slug, $args );     
}

Below is the code for custom role manager

function add_manager () {
$role_manager = 'manager' ;
$displayname_manager = 'Manager';
$capabilities_manager = array (
    'read' => true,
    'edit_product' => true,
    'read_product' => true,
    'delete_product' => false,
    'edit_products' => true,
    'edit_others_products' => true,
    'publish_products' => true,
    'read_private_products' => true,
    'delete_products' => false,
    'delete_private_products' => false,
    'delete_published_products' => false,
    'delete_others_products' => false,
    'edit_private_products' => true,
    'edit_published_products' => true,
    'edit_posts' => true,
    'upload_files' => true
    );


    create_custom_role ( $role_manager, $displayname_manager, $capabilities_manager );
}
add_action('admin_init', 'add_manager');


function create_custom_role ( $rolename, $roledisplayname, $capabilities ) {
    if ( get_role ( $rolename ) ) {
        return 0;
    }
    else {
        add_role ( $rolename, $roledisplayname, $capabilities );
    }
}

Now I am completely lost here, and can't find a solution! Can anyone please help me and tell me what I missed, or did wrong? :(

I have created a custom post type named product and assigned a new role manager for it. Everythign works fine, except the media uploader for product. I can even upload images without any error directly in the add media section. So, I started to test with default author, and still same error! Amazingly author can upload images successfully, when they are in a default post, not in product!

The error message says: An Error occurred in the upload. Please Try again later.

Below is the code for custom post type:

function cpt_product() {
    $slug = 'product';
    $labels = array( //all the custom labels );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'menu_position' => '5.1',
        'public' => true,
        'has_archive' => true,
        'supports' => array( 'title', 'editor', 'author', 'excerpt', 'custom-fields', 'revisions' ),
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'show_ui' => true,
        'capability_type' => $slug,
        'capabilities' => array(
            'edit_post' => 'edit_' . $slug,
            'read_post' => 'read_' . $slug,
            'delete_post' => 'delete_' . $slug,
            'edit_posts' => 'edit_' . $slug . 's',
            'edit_others_posts' => 'edit_others_' . $slug . 's',
            'publish_posts' => 'publish_' . $slug . 's',
            'read_private_posts' => 'read_private_' . $slug . 's',
            'delete_posts' => 'delete_' . $slug . 's',
            'delete_private_posts' => 'delete_private_' . $slug . 's',
            'delete_published_posts' => 'delete_published_' . $slug . 's',
            'delete_others_posts' => 'delete_others_' . $slug . 's',
            'edit_private_posts' => 'edit_private_' . $slug . 's',
            'edit_published_posts' => 'edit_published_' . $slug . 's'
        ),
        'map_meta_cap' => true
    );

    register_post_type( $slug, $args );     
}

Below is the code for custom role manager

function add_manager () {
$role_manager = 'manager' ;
$displayname_manager = 'Manager';
$capabilities_manager = array (
    'read' => true,
    'edit_product' => true,
    'read_product' => true,
    'delete_product' => false,
    'edit_products' => true,
    'edit_others_products' => true,
    'publish_products' => true,
    'read_private_products' => true,
    'delete_products' => false,
    'delete_private_products' => false,
    'delete_published_products' => false,
    'delete_others_products' => false,
    'edit_private_products' => true,
    'edit_published_products' => true,
    'edit_posts' => true,
    'upload_files' => true
    );


    create_custom_role ( $role_manager, $displayname_manager, $capabilities_manager );
}
add_action('admin_init', 'add_manager');


function create_custom_role ( $rolename, $roledisplayname, $capabilities ) {
    if ( get_role ( $rolename ) ) {
        return 0;
    }
    else {
        add_role ( $rolename, $roledisplayname, $capabilities );
    }
}

Now I am completely lost here, and can't find a solution! Can anyone please help me and tell me what I missed, or did wrong? :(

Share Improve this question asked May 6, 2014 at 18:38 тнє Sufiтнє Sufi 1,7104 gold badges19 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This is very strange. I have got it working by setting edit_others_posts and edit_private_posts to true!

Post a comment

comment list (0)

  1. No comments so far