WordPress.org

Ready to get started?Download WordPress

Forums

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

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

    That works fine! Thanks a lot!

  5. Max GJ Panas
    Member
    Plugin Author

    Posted 4 months 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 month 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?

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.