• Resolved Will

    (@silvercats)


    Looking for a way to remove the insert into post button in the media uploader for a specific custom post type. I can do it when the post type is in the url but when you edit a post, the post type isn’t in the url.

    ex.
    post.php?post=27&action=edit

    I tried $post, $typenow and $current_screen but the only one that works anywhere so far is $_REQUEST[‘post_type’]. Not sure if part of it is using just add_action('admin_head', 'remove_insert_button_13247')

    In any case, how do I detect the post type when editing a post, custom or other?

    function remove_insert_button_13247() {
        echo '<style type="text/css">
            .media-frame a.button-primary {display:none;}
        </style>';
    }
    function get_current_post_type_13247() {
    global $post, $typenow, $current_screen;
     if( $post && $post->post_type )
    $post_type = $post->post_type;
     elseif( $typenow )
    $post_type = $typenow;
    elseif( $current_screen && $current_screen->post_type )
     $post_type = $current_screen->post_type;
    elseif( $_REQUEST['post_type'] )
     $post_type = sanitize_key( $_REQUEST['post_type'] );
    else
    $post_type = '';
     return $post_type;
    }
    if ( get_current_post_type_13247() == 'listing') {
    	add_action('admin_head', 'remove_insert_button_13247');
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Did you try just get_post_type? Did that not work?

    Thread Starter Will

    (@silvercats)

    Did you try just get_post_type? Did that not work?

    Sorry, should have stated I’m working withing the wp-admin. I’ll try a query today. Looking for a simpler way though.

    get_post_type should work in the admin without the need for a separate query:

    <?php
    
    function remove_insert_button_13247() {
    	if('post' === get_post_type()) {
    		echo '<style type="text/css">.media-frame a.button-primary { display: none; }</style>';
    	}
    }
    add_action('admin_head', 'remove_insert_button_13247');

    I just tested that and it removes the button in the post editing and adding interface but not so for pages. Take a look and let me know!

    Thread Starter Will

    (@silvercats)

    get_post_type should work in the admin without the need for a separate query:

    That works perfect! Thanks nick, you saved me a lot of time.

    Corrected function for removing insert into post button in media uploader via admin:

    function get_current_post_type_13247() {
    if (get_post_type())
    $post_type = get_post_type();
    else
    $post_type = '';
     return $post_type;
    }
    add_action('admin_head', 'remove_insert_button_13247');
    function remove_insert_button_13247() {
    	if('listing' === get_post_type()) {
    		echo '<style type="text/css">.media-frame a.button-primary { display: none; }</style>';
    	}
    }

    Thread Starter Will

    (@silvercats)

    I’ve seen this function tried several times with no success, this actually works as of WordPress 3.81.

    Thread Starter Will

    (@silvercats)

    Sorry, one more correction

    function get_current_post_type_13247() {
    if (get_post_type())
    $post_type = get_post_type();
    else
    $post_type = '';
     return $post_type;
    }
    add_action('admin_head', 'remove_insert_button_13247');
    function remove_insert_button_13247() {
    	if('listing' == get_current_post_type_13247()) {
    		echo '<style type="text/css">.media-frame a.button-primary { display: none; }</style>';
    	}
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Remove "insert into button" in media uploader for custom post types’ is closed to new replies.