Simple Membership Plugin › Forums › Simple Membership Plugin › Show shortcode of another plugin if member has the required membership level
- This topic has 2 replies, 2 voices, and was last updated 5 years, 11 months ago by
ishop.
-
AuthorPosts
-
July 24, 2020 at 10:06 pm #20605
ishop
ParticipantHello!
I am running a webshop. Each wordpress post is based on a template. In each i want to show a buy button (already exists) or a shortcode to a gallery embedded in the php template if the user is logged in and has the required membership level to view the content.
at this page i found some codes, but i didnt find a php code for “if a member is logged in and has this or that membership level” or “if a member is logged in and has an active subscription”. Both would work for me. After that i need to insert the gallery shortcode via “echo do_shortcode();”
Could you help me with that?
Thank you for your awesome plugin!
ishopPS: that plugin doesnt work for me as i need a shortcode in a shortcode – that is very diffucilt to handle, as i get the id of the post via a variable…
July 25, 2020 at 1:35 am #20607admin
KeymasterThis is a custom requirement so you will need to create your own customized code.
You can use the following PHP code to retrieve the membership level ID of the user:
$member_level = SwpmMemberUtils::get_logged_in_members_level();
Then you can check to see if that level ID matches with the level you want to show the button to. So something like this (this is just test code):
<?php //First check if the visitor viewing the page is logged into the site: if(!SwpmMemberUtils::is_member_logged_in()) { //User is not logged-in. So don't go forward. return 0; } //Lets see what level this member belongs to $member_level = SwpmMemberUtils::get_logged_in_members_level(); if ($member_level == '2'){ //This member has level 2 so you can show something here that will be visible to member with level ID 2. } else if ($member_level == '3') { //This member has level 3 so you can show something here that will be visible to member with level ID 3. } //TODO .... ?>August 2, 2020 at 5:45 pm #20648ishop
ParticipantHello again!
The thing is if want to show content if a user has an active membership.
I found out that querying the status or the account state as php if class would work (found it in another post!) and then give an output or a script in my case if the status is active, not changing something if user does not have access.
This is the codethat works for me,maybe it helps others:?php //Lets see if user is logged in if(SwpmMemberUtils::is_member_logged_in() == true){ // Yes. A user is logged in. // Get the user's member id. We will use this later.* $mId = SwpmMemberUtils::get_logged_in_members_id(); // Get the user's current membership account state. $auth = SwpmAuth::get_instance(); $account_state = $auth->get('account_state'); if ($account_state == 'active'){ // execute script, show subscription contents echo '<script type="text/javascript">', 'myFunction();', '</script>'; ?> <?php } ?> <?php } ?>Thank you for your help!
-
AuthorPosts
- You must be logged in to reply to this topic.