• Resolved LRT

    (@lythia)


    Hi there!

    Great PlugIn! I’d like to use it for my homepage and I have some uploads that are also shared by authors, editors and contributors. Currently the vault only limits access to non-admins.

    Is there a way to enhance this? E.g. make attachment visible for every user that is contributor (or more). Or “attachment NOT visible if curuser is subscriber”.

    Regards,
    Lars

    https://www.ads-software.com/plugins/media-vault/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Max GJ Panas

    (@max-gjp)

    Yes, it is possible!

    Though I have not had the time to document it yet, you can add any custom access permissions you want via the function mgjp_mv_add_permission().

    For your particular case you are going to want to add the following code into your theme’s functions.php or alternatively into a custom functions plugin, in order to register the custom permission:

    mgjp_mv_add_permission( 'contributor-plus', array(
      'description'  => 'Contributors and Above',
      'select'       => 'Contributors Plus',
      'logged_in'    => true, // whether the user must be logged in
      'run_in_admin' => true, // whether to run the access check in admin
      'cb'           => 'wpst_mv_restrict_only_for_subscribers'
    ) );

    Notice the cb property, it is the callback that will be called to determine whether a user is finally given access or not. It must return true to grant access or return false or a WP_Error to deny access.

    So in the same place you registered the permission, we’ll define the callback function with the following code:

    function wpst_mv_restrict_only_for_subscribers() {
    
      if ( current_user_can( 'edit_posts' ) )
        return true;
    
      return false;
    }

    This uses WP’s Roles and Capabilities to determine whether a user is at least contributor. If they are, they are allowed access, if not the function returns false and they are denied access.

    Now you should be able to find and set your new custom permission, ‘Contributors Plus’, wherever you need it: in Media Settings as your default protection, or on individual attachments via the Media Vault Metabox.

    Let me know if this worked for you!

    Plugin Author Max GJ Panas

    (@max-gjp)

    Furthermore, you might want to hook the “registering” part of the above to a WordPress action for more robust code. In that case the action hook you are going to want to use is ‘after_setup_theme’.

    You should also wrap plugin specific function calls in function_exists checks, just in case you deactivate the plugin sometime in the future.

    Therefore the final code to put into your functions.php or custom functions plugin would look like this:

    function wpst_mv_register_custom_permissions() {
    
      if ( function_exists( 'mgjp_mv_add_permission' ) ) {
    
        mgjp_mv_add_permission( 'contributor-plus', array(
          'description'  => 'Contributors and Above',
          'select'       => 'Contributors Plus',
          'logged_in'    => true, // whether the user must be logged in
          'run_in_admin' => true, // whether to run the access check in admin
          'cb'           => 'wpst_mv_restrict_only_for_subscribers'
        ) );
    
      }
    
    }
    add_action( 'after_setup_theme', 'wpst_mv_register_custom_permissions' );
    
    function wpst_mv_restrict_only_for_subscribers() {
    
      if ( current_user_can( 'edit_posts' ) )
        return true;
    
      return false;
    }
    Thread Starter LRT

    (@lythia)

    That works fine! Thanks a lot!

    Plugin Author Max GJ Panas

    (@max-gjp)

    Glad to help!

    And please consider giving the plugin a rating/review, it would help me out a lot!

    Spence

    (@spencerreiter)

    Hey quick question, I will want to add these permissions and remove these permissions dynamically, I will be hooking into another plugin, so when that plugin creates a something new I will create the corresponding permission. Is there a function to also remove an existing permission?

    To start of with, very nice plugin an a much needed feature. As Spence states above it would be a great if the plugin could evolve/or have a write up on a “dynamic permission feature”.

    For example if you have a scheduled post with 1 or more attached images/files. And these files are protected (only visible for lets say Admin) as long as the post is not published. Then when the post is for example published the files are automatically moved from “_mediavault” and accessible to whatever permission you choose.

    Something that could be done?

    /All the best!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Restrict only for subscribers’ is closed to new replies.