Home / Archive / Featured Images of the Current User’s All Active Memberships
Duplicate Snippet

Embed Snippet on Your Site

Featured Images of the Current User’s All Active Memberships

This code snippet adds the new [membership_thumbnail] shortcode. Adding this shortcode anywhere on the website will display featured images of all memberships the current user is subscribed to.

The membership-thumbnail CSS class can be used for custom styling.

Code Preview
php
<?php
/**
 * Display featured images of the current user's active memberships
 * Add the [membership_thumbnail] shortcode to display the featured images of all active memberships of the current user.
 */
function custom_membership_thumbnail_shortcode() {
    if ( !is_user_logged_in() ) return;
    $user = MeprUtils::get_currentuserinfo();
    $memberships = $user->active_product_subscriptions( 'ids' );
    $output = '';
    if ( empty($memberships) ) return;
    foreach ( $memberships as $membership ) {
        if ( has_post_thumbnail( $membership ) ) {
            $thumbnail_url = get_the_post_thumbnail_url( $membership, 'thumbnail' );
            $output .= "<img src='" . esc_url( $thumbnail_url ) . "' class='membership-thumbnail' width='400' height='400' style='margin: 10px;'>";
        }
        return $output;
    }
    return '';
}
add_shortcode( 'membership_thumbnail', 'custom_membership_thumbnail_shortcode' );

Comments

Add a Comment