How to disable “insert into post” only when selecting the Featured Image?

I wish to restrict the user from putting inline images in the article. I am using the WordPress Attachments plugin to allow the user to attach images, which I then position and stylize the way I want.

I also managed to remove the Upload/Insert link from the post through adding a hook to my functions.php of my theme. (Thanks to an answer here.)

Read More

However, I noticed that the “Insert into post” link is still there when the user presses the “Set Featured Image” link. I want to allow him to set a featured image but not insert into post.

I tried another solution I found (inverting the logic here) and added this function:

add_filter( 'get_media_item_args', 'force_send' );
function force_send($args)
{
   $args['send'] = false;
   return $args;
}

Which does the job and disables the “Insert into post” button, but on the other hand it also stops the Attachments plugin from working (the Attach button disappears) and also other plugins using the same image uploading dialog stopped working.

Is there any way I can detect that the “Set featured image” link was clicked in the function force_send() above so that I only disable the button when the user clicks the set featured image?

Alternatively, is there any better way to stop the user from putting images in the WYSIWYG editor? There doesn’t seem to be any permission setting anywhere to stop a user from doing so.

UPDATE (ACTUAL SOLUTION)

Just want to post my solution, based on the answer below, just for completeness in case anyone needs it. It also hides the “Insert into Post” button.

add_filter( 'get_media_item_args', 'force_send' );
function force_send($args)
{
  // for the media upload popup we get the Post ID from the $_GET URL parameter list
  $post_id = $_GET['post_id'];

  // with post ID in hand we now get current post_type       
  $post_type  = get_post_type($post_id);

  // define list of our restricted post_types
  $restricted = array('post', 'page');

  // check current post_type against array of restricted post_types
  if (in_array($post_type, $restricted)) 
  {
    // if our current post_type as returned from get_post_type is in array 
    // we return false to void inserting image into post editor
    $args['send'] = false;
  } 

  return $args;
}

Related posts

Leave a Reply

2 comments

  1. You can do this via,

    add_filter('image_send_to_editor', 'restrict_images');
    
    function restrict_images($html){
    
      return false;
    
    }
    

    …now consider the above function primitive, because it will restrict images for all post types. We can modify that on a post_type by post_type basis like so,

    add_filter('image_send_to_editor', 'restrict_images');
    
    function restrict_images($html){
    
        // check if its the admin trying to insert image, if so, quickly
        // return the image to the editor and do not run remainder of function
        if(current_user_can('activate_plugins'))
        return $html;
    
        // global $post and $wp_query not available so we use $_POST['key']
        $post_id    = $_POST['post_id'];
    
        // with post ID in hand we now get current post_type       
        $post_type  = get_post_type($post_id);
    
        // define list of our restricted post_types
        $restricted = array('post', 'page');
    
        // check current post_type against array of restricted post_types
        if (in_array($post_type, $restricted)) {
    
            // if our current post_type as returned from get_post_type is in array 
            // we return false to void inserting image into post editor
            return false;
    
        } else {
    
            // if our post_type does not exist in restricted array, carry on as normal
            return $html;
        }
    }
    
  2. If you aren’t using Featured Image for much, it might be easier to set up a custom field for it. Where and how do you show your Featured Image?

    You could also restrict media upload based on user roles, but doesn’t sound like it’s what you need.