5

I'm using the plugin Custom CSS JS which registers it's own post type but the capability is assigned to "post".

I want to change it to "manage_options".

Is there a correct way without altering the plugin to do that? Calling a hook, a function or whatever?

public function register_post_type() {

    $labels = array (
        'name'               => _x( 'Custom Code', 'post type general name' ),
        'singular_name'      => _x( 'Custom Code', 'post type singular name' ),
        'menu_name'          => _x( 'Custom CSS & JS', 'admin menu' ),
        'name_admin_bar'     => _x( 'Custom Code', 'add new on admin bar' ),
        'add_new'            => _x( 'Add Custom Code', 'add new' ),
        'add_new_item'       => __( 'Add Custom Code' ),
        'new_item'           => __( 'New Custom Code' ),
        'edit_item'          => __( 'Edit Custom Code' ),
        'view_item'          => __( 'View Custom Code' ),
        'all_items'          => __( 'All Custom Code' ),
        'search_items'       => __( 'Search Custom Code' ),
        'parent_item_colon'  => __( 'Parent Custom Code:' ),
        'not_found'          => __( 'No Custom Code found.' ),
        'not_found_in_trash' => __( 'No Custom Code found in Trash.' ),
    );
    $args   = array (
        'labels'              => $labels,
        'description'         => __( 'Custom CSS and JS code' ),
        'public'              => false,
        'publicly_queryable'  => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_position'       => 100,
        'menu_icon'           => 'dashicons-plus-alt',
        'query_var'           => false,
        'rewrite'             => array ( 'slug' => 'custom-css-js' ),
        'capability_type'     => 'post', // <--- I want to manipulate this
        'has_archive'         => true,
        'hierarchical'        => false,
        'exclude_from_search' => true,
        'menu_position'       => null,
        'can_export'          => false,
        'supports'            => array ( 'title' ),
    );

    register_post_type( 'custom-css-js', $args );
}

The above code is in custom-css-js.php lines 200-239.

4

@PieterGoosen is cool, and still awake like me and answering like boss.

To simplify his code and make it work to this specifically without a bunch of junk on your page:

/**
 * Pieter Goosen writes awesome code
 */
add_filter( 'register_post_type_args', 'change_capabilities_of_the_custom_css_js_posttype' , 10, 2 );

function change_capabilities_of_the_custom_css_js_posttype( $args, $post_type ){

 // Do not filter any other post type
 if ( 'custom-css-js' !== $post_type ) {

     // Give other post_types their original arguments
     return $args;

 }

 // Change the capability_type of the "custom-css-js" post_type
 $args['capability_type'] = 'manage_options';

  // Give the custom-css-js post type it's arguments
  return $args;

}

The plugin you have chosen is written in a broad way. It has some insecurities.

5

WordPress 4.4 finally saw the introduction of the register_post_type_args filter which you can use to alter the the arguments used when a custom post type (or build-in type) is registered

I cannot code anything concrete now, but the following should get you going

add_filter( 'register_post_type_args', function ( $args, $post_type )
{
    // Only target our specific post type
    if ( 'my_post_type' !== $post_type )
        return $args;

    // OK, we have our specified post type, lets alter our arguments
    ?><pre><?php var_dump( $args ); ?></pre><?php

    // Change capability_type
    $args['capability_type'] = 'some_new_value';

    return $args;
}, 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.