Ask SkyVerge

Today’s question comes from Scott:

Can I use or create a shortcode to have the “My Memberships” area outside of the “My Account” page? In other words, if I wanted to place My Memberships on a page other than the default /my-account page, would I be able to do that?


This is an interesting question, as you could add the “My Memberships” table to a custom page on your site, protect that page for members, and then remove it from the “My Account” area.

However, there’s no shortcode to generate the “My Memberships” section with WooCommerce Memberships currently. With that said, it’s fairly easy to add one, as there’s already a function to output this area that Memberships uses itself:

wc_memberships()->get_frontend_instance()->get_members_area_instance()->my_account_memberships();

We’ll make a shortcode to output this and then wrap it in a div with the CSS class woocommerce so it’s styled automatically like a WooCommerce table:

Now you can use [wcm_my_memberships] within your content anywhere that shortcodes are expanded, and this area will render if a user has 1 or more memberships. If the user does not have a membership, the section will not be shown:

 
WooCommerce Memberships: My membership shortcode - nonmember

Non-member view

WooCommerce Memberships: My membership shortcode - member

Member view

Easy enough!

Bonus round: If you wanted to move the My Memberships table completely, you can remove this from the My Account area with a tiny snippet as well.

Hope this helps, Scott!

Published by Nik McLaughlin

You can find Nik around the WP space, on LinkedIn, or on his personal blog.

32 Comments

  1. Thanks for the article works great, is it possible to also create a ‘My Membership Content’ table shortcode? I’d like to put that on another page.

    • Did you ever find a solution for this? I also want to do this.

      • I would also love to be able to do this, as our client has monthly content releases that are available through a variety of membership plans, and to avoid having to click through an additional page to get to the actual content would be preferable for our users. Thanks!

  2. Is it possible to filter the default shortcode function so the tag name can be customized to work with existing shortcodes already added using another membership system.

    Example [members]

    I would like to start using the WooCommerce membership extension.

    • There’s no way to filter the shortcode tag name, but you could add your own shortcode to replace the members shortcode instead with a wrapper. I have not tested this code, but something along these lines would work — you’d want to convert the “members” attributes to [wcm_restrict] attributes (if it takes any arguments, if not this could be simpler):

      function wcm_shortcode_tag_converter( $atts, $content = null ) {
          $a = shortcode_atts( array(
              // use whatever atts the existing [member] shortcode has that can apply to [wcm_restrict]
              'type'  => '',
              'delay' => '',
          ), $atts );
      
          ob_start();
      
          do_shortcode( '[wcm_restrict plans="' . esc_html( $a['type'] ) . '" delay="' . esc_html( $a['delay'] ) . '"]' . $content . '[/wcm_restrict]' );
      
          return ob_get_clean();
      }
      add_shortcode( 'members', 'wcm_shortcode_tag_converter' );
      
    • Same. I’d like to JUST show “my content” on another page via template tags or shortcode.

  3. Could you add a filter to your add_shortcode function so anyone using other membership shortcode tags would find it easier to migrate?

    Another option i thought about was doing a SQL query in phpMyAdmin and replace all existing shortcodes with the new one.

    Thanks for the code. I did think of that option as well but hadn’t worked out how to write it just yet.

    • Hey Brad, we probably won’t filter this because most membership plugin shortcodes wont have the same attributes as our restriction shortcode, so filtering the tag itself won’t be helpful, and it’s a pretty niche need regardless. Creating a new shortcode wrapper to convert attributes is probably the better way to go, or if your previous shortcode doesn’t have attributes, doing the sql replace can work.

  4. I’m new in this wordpress thing, sorry if this sounds dumb. But, which file i have to add the code to make a shortcode? thank you.

  5. I absolutely LOVE this code snippet, LOVE IT!

    However, I am looking for a way to style it, how would this be done?

  6. Checking to see if there is a solution for this yet. Being able to go straight to content without requiring the user to click through a bunch of links is key when you don’t have multiple levels of membership.

  7. Hi Becka! Seriously, thank you so much for all of your help here – really appreciate it.

    I was actually just curious if you’d be willing to update the “remove memberships info from dashboard” snippet, to make it compatible with the current Memberships. I was able to use

    (https://gist.github.com/bekarice/62cc848397fdedb02cfe)

    I’m still relatively new to actual php coding, so i’ve been experimenting and trying to update it for the correct functions, with no luck.

    Thanks again!

    • Hey Misha, this snippet has been updated a few times and works for me right now with current Memberships + WooCommerce 🙂

      • Literally JUST saw that – I had seen the comments about it being antiquated and started unnecessarily tinkering with it… ?

        Out of curiosity, how easy would it be to create similar shortcodes, but for showing specific sections of the Member’s Area?

        I would love to remove the table from the Dashboard, but have a tab on the My Account page for “My Discounts.” This could be easily done with a shortcode and the Yith Customize My Account Page plugin, but again, such a novice. I’ve tried adapting your snippet here, and I think I’m getting close, but I just can’t crack it…

        Impressively fast response, by the way – thank you!

        /**
        * Creates a shortcode to output the “My Membership Content” table anywhere on the site
        * Outputs this section only if the current user has 1 or more memberships
        *
        * Use the shortcode: [wcm_my_memberships_content]
        */

        function sv_wc_memberships_my_memberships_content_shortcode() {

        // bail if Memberships isn't active or we're in the admin
        if ( ! function_exists( 'wc_memberships' ) || is_admin() ) {
        return;
        }

        // buffer contents
        ob_start();

        ?><div class="woocommerce"><?php
        wc_memberships()->get_frontend_instance()->get_member_area_instance()->get_template( 'myaccount/my-membership-content.php', array(
        'customer_membership' => $args['user_membership'],
        'restricted_content' => $args['user_membership']->get_plan()->get_restricted_content( $paged ),
        'user_id' => $args['user_id'],
        ) );

        ?></div><?php

        // output buffered content
        echo ob_get_clean();

        }

        add_shortcode( ‘wcm_my_memberships_content’, ‘sv_wc_memberships_my_memberships_content_shortcode’ );

  8. Hey Neil, Skyler & Gray!

    To output the Membership-Plan Content via shortcode, the combination of Beka & Misha did the trick for me, thanx 4 that!

    /**
    * Creates a shortcode to output the “My Membership Content” table anywhere on the site
    * Outputs this section only if the current user has 1 or more memberships
    *
    * Use the shortcode: [wcm_my_memberships_content]
    */
    function sv_wc_memberships_get_active_memberships() {
    $user_id = get_current_user_id();
    // bail if this is user is not logged in
    if ( ! is_numeric( $user_id ) || 0 === $user_id ) {
    return array();
    }

    $args = array(
    'status' => array( 'active', 'complimentary', 'pending', 'free-trial' ),
    );
    return wc_memberships_get_user_memberships( $user_id, $args );

    }

    function sv_wc_memberships_my_memberships_content_shortcode() {

    // bail if Memberships isn't active or we're in the admin
    if ( ! function_exists( 'wc_memberships' ) || is_admin() ) {return; }

    $active_memberships = sv_wc_memberships_get_active_memberships();

    ob_start();

    ?><div class="woocommerce"><?php

    foreach ( $active_memberships as $membership ) {

    echo '<h1>'.$membership->plan->name.'</h1>';

    wc_get_template( 'myaccount/my-membership-content.php',
    array(
    'customer_membership' => $membership,
    'restricted_content' => $membership->get_plan()->get_restricted_content(),
    'user_id' => get_current_user_id(),
    )
    );

    // output buffered content

    }

    ?></div><?php

    echo ob_get_clean();

    }

    add_shortcode( ‘wcm_my_memberships_content’, ‘sv_wc_memberships_my_memberships_content_shortcode’ );

  9. Hello Frank, I am very interested in that functionality of displaying authorized content through a shortcode. I’ve tested the code on the function code you’ve developed, but it does not work for me. Any alternative?

    • Hi Angel!

      The code above works for me, even in Woocommerce 3 but it think there was a problem with my here pasted single quotes in the add_shortcode-line. Here are the corrected quotes:

      add_shortcode( ‘wcm_my_memberships_content’, ‘sv_wc_memberships_my_memberships_content_shortcode’ );

      Best,

      Frank

    • Hello Frank, I applied your code, also with WooCommerce 3 and the updated “add_shortcode” line but couldn’t get it to work. Beka’s shortcode works just fine, however the [wcm_my_memberships_content] won’t do it. I hope someone can help me on this one. What could I possibly be doing wrong?

      • Hi Gustav!

        I don’t know if I am stupid or the quotes were replaced by contactform. Probably the former 🙂 Please check again the quotes from the add_shortcode line. They have to be single quotes (‘) I’ll post it here again, maybe be the awesome Beka could edit/correct it the comments if she things that the snippet is ok so far.

        If that was not your problem, the I also dont’ know…

        add_shortcode( 'wcm_my_memberships_content', 'sv_wc_memberships_my_memberships_content_shortcode' );

        Best,
        Frank

    • Is there a way to modify this code to output the contents of only ONE specific membership plan’s content for users who have multiple memberships? I want to be able to output the contents of a subscription with dripped posts on a specific page, but using this shortcode outputs from all active memberships. Appreciate any help on this!
      Jeff

  10. Hi Frank.

    I had not seen the quotes. Now, it works perfectly.

    Thank you very much.

    • What’s concerning the quotes, I’m at least not that stupid 🙂 , there is a conact form translation thing here with single quotes and I don’t know how to post em right maybe escaping ' or \’ ?
      Sorry, I don’t want to spam here either…

  11. Hey Bekka-
    We are looking to add a list of member names for our highest membership level to a page to recognize their contribution. Do you have any tips on how I would go about adding a short-code for this?

    • Hey Sena, you’ve got good timing 🙂 We’ve put together a simple shortcode to show member lists, we’re working on putting this into a plugin so it can be installed and will write up a tutorial on it when we’re done!

  12. Excellent, thanks for the reply!

  13. I had a problem using this shortcode with my Toolset Starter theme. The table was appearing outside of the theme (on top of the page). Toolset recommended changing

    echo ob_get_clean();

    to

    return ob_get_clean();

    which fixed the issue.
    FYI for those struggling this with particular issue.

    A very useful shortcode! Thanks,
    Jeff

  14. Sorry, but I don’t know where to add this code either. I tried on my functions.php file, and added the shortcode to my wp-content/themes/my-theme/woocommerce/myaccount/dashboard.php file.
    Also tried adding the code using the snippets plugin.
    Also added a file using this route wp-content/themes/my-theme/woocommerce-memberships/frontend/add-my-memberships-shortcode.php
    If I add this code to the woocommerce-membership folder would it be overwritten/erased by an update?
    My first time with shortcodes, I hope someone helps, Thanks

    • It finally worked using the snippets plugin. The problem is that My Membership area is not displaying on my account section, haven’t being able to figure out why. So I added this shortcode and it works, but it shows up on every tab under my account page. Does anyone has an idea on why this section is missing?

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.