Skip to content

Instantly share code, notes, and snippets.

@asadowski10
Created August 11, 2017 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asadowski10/1ee36c268767c2923996241b05517b60 to your computer and use it in GitHub Desktop.
Save asadowski10/1ee36c268767c2923996241b05517b60 to your computer and use it in GitHub Desktop.
Protect media uploaded with content editor
<?php
/**
* Plugin Name: BE API - Media Vault protect media from editor
* Description: Allow to protect media uploaded with content editor
* Plugin URI: http://www.beapi.fr
* Version: 1.0.0
* Author: BeAPI
* Author URI: http://www.beapi.fr
*/
add_action( 'added_post_meta', 'bea_added_post_meta', 10, 4 );
function bea_added_post_meta( $meta_id, $object_id, $meta_key, $meta_value ) {
if ( ! function_exists( 'mgjp_mv_move_attachment_to_protected' ) ) {
return false;
}
remove_action( 'added_post_meta', 'bea_added_post_meta', 15 );
if ( '_wp_attachment_metadata' !== $meta_key ) {
return false;
}
if ( 'attachment' !== get_post_type( $object_id ) ) {
return false;
}
mgjp_mv_move_attachment_to_protected( $object_id );
update_post_meta( $object_id, '_mgjp_mv_permission', 'logged-in' );
add_action( 'added_post_meta', 'bea_added_post_meta', 15, 4 );
}
@bananadesign
Copy link

bananadesign commented Jul 2, 2018

I had to add this action as well to get it to work for me:

add_action( 'updated_post_meta', 'bea_added_post_meta', 10, 4 );

@bananadesign
Copy link

bananadesign commented Sep 11, 2018

Having an odd situation where certain file types aren't protected (.csv, .docs, etc) but others are OK (.jpg, pdf). Commenting out this line:

if ( '_wp_attachment_metadata' !== $meta_key ) { return false; }

works but I'm not sure whether there are any implications for this. Any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment