WordPressでカスタム投稿タイプとアドバンスドカスタムフィールドを利用して、例えば広告バナー表示や特定のページに埋め込んだ内容などのコンテンツの更新を行う場合、管理者以外はメニューの「新規追加」や「編集・クイック編集・ゴミ箱へ移動」ほか「プレビュー」などを非表示にしておく方が都合がよいことがある。
以下はその方法。(カスタム投稿タイプはhogehoge)
ダッシュボード左側のメニューから管理者以外hogehogeの新規追加を無効にする
function remove_admin_menus() {
// level10以外のユーザーの場合
if (!current_user_can('level_10')) {
global $menu;
global $submenu;
// unsetで非表示にするメニューを指定
unset($menu[2]); // ダッシュボード
// unset($menu[5]); // 投稿
unset($menu[10]); // メディア
unset($menu[20]); // 固定ページ
unset($submenu['edit.php?post_type=page'][10]); //固定ページのサブメニュー
unset($menu[25]); // コメント
unset($menu[60]); // 外観
unset($menu[65]); // プラグイン
unset($menu[70]); // ユーザー
unset($menu[75]); // ツール
unset($menu[80]); // 設定
remove_menu_page('wpcf7'); //コンタクトフォーム7
remove_submenu_page( 'edit.php?post_type=hogehoge', 'post-new.php?post_type=hogehoge' ); //hogehogeの新規追加非表示
}
}
編集画面側に表示される「新規追加」を非表示にする
function custom_edit_newpost_delete($hook) {
if($hook == 'edit.php' || $hook == 'post.php'){
$postType = get_post_type();
if ( !current_user_can( 'administrator' ) ) {
if ( $postType == 'hogehoge' ) { //カスタム投稿スラッグがhogehogeなら非表示
echo '<style>.wrap .wp-heading-inline + .page-title-action{display: none;}</style>';
}
}
}
}
add_action('admin_enqueue_scripts', 'custom_edit_newpost_delete');
編集画面の公開エリアにある「プレビュー」または「変更をプレビュー」を無効にするには、カスタム投稿タイプ作成時の設定で「publicly_queryable」をfalseにする。
以下参考
register_post_type( 'support', array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'support' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
) );
一覧の「編集・クイック編集・ゴミ箱へ移動・プレビュー」を非表示にする
function custom_action_row($actions, $post){
$postType = get_post_type();
if ( $postType == "recruit" ){ //カスタム投稿スラッグがrecruitなら非表示
unset($actions['edit']); //編集
unset($actions['inline hide-if-no-js']); //クイック編集
unset($actions['trash']); //ゴミ箱
unset($actions['view']); //プレビュー
}
return $actions;
}
add_filter('post_row_actions','custom_action_row', 10, 2);