• Dongsan

    (@dongsan)


    I’m trying to list all capabilities assigned to a post type by making use of a WordPress function get_post_type_capabilites().

    Here’s what WordPress Codex explains:

    Function Reference/get post type capabilities

    Description

    Builds an object with all post type capabilities out of a post type object. Post type capabilities use the ‘capability_type’ argument as a base, if the capability is not set in the ‘capabilities’ argument array or if the ‘capabilities’ argument is not supplied. The capability_type argument can optionally be registered as an array, with the first value being singular and the second plural, e.g. array(‘story, ‘stories’) Otherwise, an ‘s’ will be added to the value for the plural form. After registration, capability_type will always be a string of the singular value.

    Usage

    get_post_type_capabilities( $args );

    Parameters

    $args

    (array or string) (required) The desired capability type (e.g. ‘post’). Set [‘capability_type’] to an array to allow for alternative plurals when using this argument as a base to construct the capabilities, e.g. array(‘story’, ‘stories’). Set [‘map_meta_cap’] to true to obtain those capabilities as well.

    Default: None

    ……

    This page is marked as incomplete. You can help Codex by expanding it.
    After reading this codex explanation, I tried the following, but it led to warnings, returning NULL as the result:

    var_dump( get_post_type_capabilities( 'post' ) );
    Warning: Attempt to assign property of non-object in ...\wp-includes\post.php on line 1526
    Warning: array_merge(): Argument #2 is not an array in ...\wp-includes\post.php on line 1557
    object(stdClass)#728 (1) { ["create_posts"]=> NULL }

    Playing with PHP as a hobby, I don’t have professional knowledge on PHP language. So I tried this and that for a few days but always failed to get what result I wanted.

    Today I modified both my code and WordPress core-file code as following, and it seems to work:

    [My PHP file]

    $args = get_post_type_object( 'post' );
    var_dump( get_post_type_capabilities( $args ) );

    [Wordpress core file – wp-includes/post.php]

    function get_post_type_capabilities( $args ) {
        ......
        //$capabilities = array_merge( $default_capabilities, $args->capabilities );
        $capabilities = array_merge( $default_capabilities, (array) $args->cap );
        ......
    }

    Yes, I made three changes. First, in my code, instead of giving a plain string ‘post’, I gave an object as an input to the function get_post_type_capabilities(). Then, in WordPress core file wp-includes\post.php, I changed $args->capabilities to $args->cap and forced it to be an array.

    And now I am getting the result I wanted:

    object(stdClass)#728 (15) { ["edit_post"]=> string(9) "edit_item" ["read_post"]=> string(9) "read_item" ["delete_post"]=> string(11) "delete_item" ["edit_posts"]=> string(10) "edit_items" ["edit_others_posts"]=> string(17) "edit_others_items" ["publish_posts"]=> string(13) "publish_items" ["read_private_posts"]=> string(18) "read_private_items" ["read"]=> string(4) "read" ["delete_posts"]=> string(12) "delete_items" ["delete_private_posts"]=> string(20) "delete_private_items" ["delete_published_posts"]=> string(22) "delete_published_items" ["delete_others_posts"]=> string(19) "delete_others_items" ["edit_private_posts"]=> string(18) "edit_private_items" ["edit_published_posts"]=> string(20) "edit_published_items" ["create_posts"]=> string(10) "edit_items" }

    Happy, but not too much happy.

    I don’t want to modify the core files of WordPress. There should be a proper way to do this without touching the core of WordPress.

    Does anyone have any idea how to properly use this function?

Viewing 1 replies (of 1 total)
  • Hey @dongsan,

    I needed use the get_post_type_capabilities function. When I research about this function, I saw this topic. I didn’t find an cool example for this. So I “hacking” the function to understand its funcionality.

    To get the default post capabilities I used this code:


    $cap_object = new stdClass();
    $cap_object->capability_type = 'post';
    $cap_object->capabilities = array();
    $capabilities = get_post_type_capabilities($cap_object);

    I hope that I helped. So late ??

Viewing 1 replies (of 1 total)
  • The topic ‘How to use get_post_type_capabilities()’ is closed to new replies.