更改插件注册的帖子类型的能力类型

时间:2016-01-26 作者:Karolína Vyskočilová

我正在使用插件Custom CSS JS 它注册自己的职位类型,但能力分配给"post".

我想把它改成"manage_options".

有没有一种不改变插件就可以做到这一点的正确方法?调用钩子、函数或其他什么?

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 );
}
上述代码位于custom-css-js.php 线200-239.

2 个回复
最合适的回答,由SO网友:Nathan Powell 整理而成

@彼得·古森很酷,仍然像我一样清醒,像老板一样回答。

为了简化他的代码,并使其具体实现这一点,您的页面上没有一堆垃圾:

/**
 * 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;

}
您选择的插件是以广泛的方式编写的。它有一些不安全感。

SO网友:Pieter Goosen

WordPress 4.4终于看到了register_post_type_args filter 您可以使用它来更改custom post type (or build-in type) is registered

我现在无法编写任何具体的代码,但以下内容应该可以帮助您

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 );

相关推荐

如何清理wp-content/plugins/wordpress-seo中不必要的文件?

我目前维护Wordpress网站7年。磁盘使用率最近已满,因此我需要定期检查自动备份并保存这些备份,然后将其从服务器中删除。当我检查时,插件文件夹中的wordpress seo文件夹(这是Yoast seo默认插件文件夹)占用了太多空间。据我所知,它充满了各种版本的插件文件。确切地说,它位于wordpress seo插件的js/dist文件夹中。它包含几种类型的小型javascript,每个名称的末尾都有某种序列或版本号,总容量约为250-300 MB。我可以清理这些文件吗?如果是,是否安全?如果我从以前