Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@bananadesign 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

This comment has been minimized.

Copy link

@bananadesign 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