0

I have a Custom Post Type Called SlidersCPT as I register the CPT like

register_post_type( 'SlidersCPT', $args );

and I need to apply some CSS rules on admin page only when creating New SlidersCPT custom Post Type. I thought this might help:

if(get_post_type() == 'SlidersCPT') {}

but as you can see it just controlling the page of Custopm Post Type not the Admin area.

what I want to do is controlling css of page if it is in admin page of creating a custom post type

function hide_editor() { 
 if(get_post_type() == 'SlidersCPT') { ?>
     <style>
       #insert-media-button {  display: none !important; }
    </style>
  <?php
  }
 }
2

You can use this code:

add_action( 'admin_enqueue_scripts', 'load_admin_style' );
function load_admin_style() {
    global $pagenow;

    if ( 'post.php' === $pagenow && isset($_GET['post']) && 'YOURPOSTTYPE' === get_post_type( $_GET['post'] ) ) {
        wp_enqueue_style( 'admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
    }
}
  • Global $pagenow is a subject to tweak. Use get_current_screen() instead. – Max Yudin Jan 16 '18 at 17:09
  • Hi apavliukov, thanks for comment. I add this solution to functions.php but it is not adding the css file into the page on admin panel! – Mona Coder Jan 16 '18 at 17:46
  • did you change 'YOURPOSTTYPE' to your post type? – apavliukov Jan 16 '18 at 18:05
  • I did it is .. && 'SlidersCPT' === get_post_type( ... ) now – Mona Coder Jan 16 '18 at 18:09
  • Did you create admin-style.css containing your CSS rules? – Max Yudin Jan 16 '18 at 18:14
0

The URL of the admin Add screen looks like this:

https://example.com/wp-admin/post-new.php?post_type=SlidersCPT

So, check the query string (superglobal $_GET) for the post type you try to add:

<?php
function hide_editor() { 
    if( isset( $_GET['post_type'] ) && 'SlidersCPT' == $_GET['post_type'] ) {
?>
    <style>
    #insert-media-button {  display: none !important; }
    </style>
<?php
    }
}
?>

More:

As CSS is easily tweakable in any modern browser, instead of hiding you can completely remove Add Media button, for example for users with Author capabilities and lower:

<?php
function remove_add_media_button(){
    // Check if user is not Author, but Editor or higher
    if( !current_user_can( 'manage_categories' ) ) {
        remove_action( 'media_buttons', 'media_buttons' );
    }
}

if( isset( $_GET['post_type'] ) && 'SlidersCPT' == $_GET['post_type'] ) {
    add_action('admin_head', 'remove_add_media_button');
}
  • Thanks Max but I need to keep the Editor in the post support. Can you please take a look at this Post (wordpress.stackexchange.com/questions/291164/…)? I explained why I need the editor there. – Mona Coder Jan 16 '18 at 17:37
  • Your First Css methos also is generating error Cannot use isset() on the result of an expression (you can use "null !== expression" instead) – Mona Coder Jan 16 '18 at 17:38
  • Oh, sorry, there was is a typo. Fixed. – Max Yudin Jan 16 '18 at 17:45
  • Thanks No error this time but CSS is not applying to the admin page – Mona Coder Jan 16 '18 at 17:51
  • Despite my proposed code should work, that's the wrong way to manage admin CSS. See @apavliukov answer for the correct implementation. – Max Yudin Jan 16 '18 at 18:08

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.