Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.)

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;
}
share|improve this question

2 Answers 2

up vote 3 down vote accepted

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;
    }
}
share|improve this answer
    
Thanks. And how do I know the post_type or restrict by post_type? –  jbx Oct 19 '12 at 0:24
    
@jbx See update. And userabuser: +1 –  kaiser Oct 19 '12 at 3:53
    
It doesn't seem to have any effect on my installation (Wordpress 3.4.2). I put it in my theme's functions.php but I still could press 'Insert into post' when I clicked on 'Set Featured Image' and the image still went to the WYSIWYG editor of the post. Any idea what could be the reason? –  jbx Oct 20 '12 at 22:16
    
@jbx I tested this extensively on my installation and it seems to work without a problem. So, quick question - are you logged in as admin when doing this? Because if you note in the example above, the first thing this snippet does is check if admin is logged in - if TRUE, then there is no restriction on the insert into post procedure. Let's start with that point first... –  userabuser Oct 21 '12 at 5:36
    
Well done! That was it... I didn't realise that, yes I was using admin. I commented out that line and it worked. The "Insert into post" button still appears but nothing happens when it is clicked. Ideally it is not shown, but your solution is good enough to work with. I applied the same concept to the get_media_item_args hook with a small modification and it worked and also hid the button. –  jbx Oct 21 '12 at 23:23

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.

share|improve this answer
    
I don't want to restrict media upload completely. I just want to restrict users from inserting images directly inline into posts. The Featured Image is used as the post's thumbnail in the list of articles in the front page and in the list of 'related articles' on the single post page. –  jbx Oct 18 '12 at 23:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.