• I’m wondering if its possible and if so how would I:

    add_action to a specific post type.

    I found a plugin that will remove the media buttons

    function removemediabuttons() {
    	remove_action( 'media_buttons', 'media_buttons' );
    }
    
    add_action('admin_head','removemediabuttons');

    I have a custom post type that i have created called Products. How would I add this action just to that post type so it removes the media buttons just from that post in the admin area.

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • I don’t know if this is the best practice, but it looks like you can just check the $_GET array on admin_init for ‘post_type’ and then remove the media_buttons actions as needed:

    add_action('admin_init', 'removemediabuttons');
    function removemediabuttons() {
    	if(isset($_GET['post_type']) && $_GET['post_type'] == 'products') {
    		remove_action( 'media_buttons', 'media_buttons' );
    	}
    }
    Thread Starter iamcanadian1973

    (@iamcanadian1973)

    Thanks jkovis

    I was thinking more about this last after i walked away from the computer, and was thinking that would be a solution.

    I was just hoping there would be a function like is_custom_post_type()

    or something non related that might do the same thing.

    The problem is, when you edit the custom post, the get variable post_type is nit there in the url.

    Thread Starter iamcanadian1973

    (@iamcanadian1973)

    Here’s a solution. Maybe someone who knows WordPress can come up with a better one. I only started using WordPress 2 days ago so I don’t totally understand the WordPress way of writing code.

    $registered_custom_types = array('product');
    
    	add_action('admin_head', 'removemediabuttons');
    
    	function removemediabuttons()
    	{
    		global $registered_custom_types;
    
    		$post_type = null;
    
    		$post_id = 0;
    
    		if( isset($_GET['post_type']) ) {
    			$post_type = $_GET['post_type'];
    		}
    		else if( isset($_POST['post_type']) ) {
    			$post_type = $_POST['post_type'];
    		}
    
    		if($post_type)
    		{
    			if(in_array($post_type, $registered_custom_types))
    			{
    				remove_action( 'media_buttons', 'media_buttons' );
    			}
    		}
    		else
    		{
    
    			if( isset($_GET['post']) ) {
    				$post_id = $_GET['post'];
    			}
    			else if( isset($_POST['post']) ) {
    				$post_id = $_POST['post'];
    			}
    
    			if($post_id)
    			{
    				$post = get_post($post_id);
    
    				if(in_array($post->post_type, $registered_custom_types))
    				{
    					remove_action( 'media_buttons', 'media_buttons' );
    				}
    
    			}
    		}
    	}
    Thread Starter iamcanadian1973

    (@iamcanadian1973)

    OK, learning the WordPress style of doing things.

    add_action('admin_head', 'removemediabuttons');
    
    	function removemediabuttons()
    	{
    		global $post;
    
    		if($post->post_type == 'products')
    		{
    			remove_action( 'media_buttons', 'media_buttons' );
    		}
    
    	}

    doh…I can’t believe I didn’t even think of checking the $post object…good thinking 😉

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘custom post types’ is closed to new replies.