3

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;
}

2 Answers 2

3

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;
    }
}
6
  • Thanks. And how do I know the post_type or restrict by post_type?
    – jbx
    Commented Oct 19, 2012 at 0:24
  • @jbx See update. And userabuser: +1
    – kaiser
    Commented Oct 19, 2012 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
    Commented Oct 20, 2012 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...
    – Adam
    Commented Oct 21, 2012 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
    Commented Oct 21, 2012 at 23:23
0

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.

1
  • 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
    Commented Oct 18, 2012 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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