WordPress.org

Forums

Media Vault
[resolved] Restrict only for subscribers (7 posts)

  1. Lars
    Member
    Posted 1 year ago #

    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

    http://wordpress.org/plugins/media-vault/

  2. Max GJ Panas
    Member
    Plugin Author

    Posted 1 year ago #

    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!

  3. Max GJ Panas
    Member
    Plugin Author

    Posted 1 year ago #

    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;
    }
  4. Lars
    Member
    Posted 1 year ago #

    That works fine! Thanks a lot!

  5. Max GJ Panas
    Member
    Plugin Author

    Posted 1 year ago #

    Glad to help!

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

  6. Spence
    Member
    Posted 1 year ago #

    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?

  7. hakpersson
    Member
    Posted 10 months ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Media Vault
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.