Skip to content

Instantly share code, notes, and snippets.

@forsvunnet
Created May 11, 2018 12:05
Show Gist options
  • Save forsvunnet/1368ca93cb362fefe8f13545974f35b5 to your computer and use it in GitHub Desktop.
Save forsvunnet/1368ca93cb362fefe8f13545974f35b5 to your computer and use it in GitHub Desktop.
WP: Remove "add new" for post type
<?php
class Post_Type_Waybill {
static function setup() {
// add_filter( 'user_has_cap', __CLASS__ .'::disallow_new_post_button', 10, 2 );
// add_filter( 'user_has_cap', __CLASS__ .'::disallow_new_post_page', 10, 2 );
add_action( 'init', __CLASS__ .'::waybill_capabilities', 0 );
add_action( 'init', __CLASS__ .'::waybill_post_type', 1 );
// add_action( 'admin_menu', __CLASS__ .'::admin_menu' );
}
/**
* Disallow New Post Page
* Prevents users from accessing the "add new waybill" page
* @return array
*/
static function disallow_new_post_page( $allcaps, $caps ) {
if ( ! did_action( 'admin_init' ) ) {
return $allcaps;
}
$screen = get_current_screen();
if ( ! $screen ) {
return $allcaps;
}
if ( $screen->id == 'waybills' && $screen->action == 'add' ) {
return [];
}
return $allcaps;
}
/**
* Disallow New Post Button
* Removes the "add new" button on the listing page
* @return array
*/
static function disallow_new_post_button( $allcaps, $caps ) {
if ( ! did_action( 'all_admin_notices' ) ) {
return $allcaps;
}
$screen = get_current_screen();
if ( 'edit-waybills' != $screen->id ) {
return $allcaps;
}
$cap = reset( $caps );
if ( 'edit_waybills' !== $cap ) {
return $allcaps;
}
if ( isset( $allcaps[ $cap ] ) ) {
$allcaps[ $cap ] = false;
}
// Remove this filter
remove_filter( 'user_has_cap', __CLASS__ .'::has_cap', 10 );
return $allcaps;
}
/**
* Waybill capabilities
* Enables administrators and shop managers to edit waybills
*/
static function waybill_capabilities() {
$allowed_roles = ['administrator', 'shop_manager'];
foreach ( $allowed_roles as $role_name ) {
$role = get_role( $role_name );
if ( ! $role ) {
continue;
}
$role->add_cap( 'edit_waybill' );
$role->add_cap( 'read_waybill' );
$role->add_cap( 'edit_waybills' );
$role->remove_cap( 'delete_waybill' );
$role->add_cap( 'publish_waybills' );
$role->add_cap( 'read_private_waybills' );
}
}
/**
* Waybill post type
*/
static function waybill_post_type() {
$labels = array(
'name' => _x( 'Waybills', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Waybill', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Waybills', 'text_domain' ),
'name_admin_bar' => __( 'Waybill', 'text_domain' ),
'archives' => __( 'Item Archives', 'text_domain' ),
'attributes' => __( 'Item Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'view_items' => __( 'View Items', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Waybill', 'text_domain' ),
'description' => __( 'Waybill information page.', 'text_domain' ),
'labels' => $labels,
'supports' => array(),
'taxonomies' => array(),
'hierarchical' => false,
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-page',
'show_in_admin_bar' => false,
'show_in_nav_menus' => false,
'can_export' => false,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability_type' => 'waybill',
'capabilities' => [
'edit_post' => 'edit_waybill',
'read_post' => 'read_waybill',
'delete_post' => 'delete_waybill',
'edit_posts' => 'edit_waybills',
'edit_others_posts' => 'edit_others_waybills',
'publish_posts' => 'publish_waybills',
'read_private_posts' => 'read_private_waybills',
'delete_posts' => 'delete_waybills',
],
);
register_post_type( 'waybill', $args );
remove_post_type_support('waybill', 'title');
remove_post_type_support('waybill', 'editor');
}
/**
* Admin menu
* removes the "add new waybill" link from the admin menu
*/
static function admin_menu() {
global $submenu;
if ( ! isset( $submenu['edit.php?post_type=waybill'] ) ) {
return;
}
unset( $submenu[ 'edit.php?post_type=waybill' ][ 10 ] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment