Below, you’ll find a detailed guide on how to add the WP-Members Membership Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the WP-Members Membership Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the WP-Members Membership Plugin and the shortcodes it provides:
"WP-Members Membership Plugin is a comprehensive tool for managing your WordPress site's user membership. This plugin streamlines registration, login, and profile management, ensuring seamless user experience."
- [wpmem_user_memberships]
- [wpmem_user_membership_posts]
- [wpmem_field]
- [wpmem_logged_in]
- [wpmem_logged_out]
- [wpmem_logout]
- [wpmem_form]
- [wpmem_show_count]
- [wpmem_profile]
- [wpmem_loginout]
- [wpmem_tos]
- [wpmem_avatar]
- [wpmem_login_link]
- [wpmem_login_button]
- [wpmem_form_nonce]
WP-Members Membership [wpmem_user_memberships] Shortcode
The WP-Members plugin shortcode ‘wpmem_user_memberships’ displays a user’s memberships. It lists memberships with their respective expiration dates, formatted according to specified parameters. The shortcode generates a title, followed by a list of memberships. If a membership doesn’t expire, it displays ‘Does not expire’.
Shortcode: [wpmem_user_memberships]
Parameters
Here is a list of all possible wpmem_user_memberships shortcode parameters and attributes:
title_before
– Defines the opening tag before the title.title_after
– Defines the closing tag after the title.title
– Sets the title of the membership list.list_before
– Defines the opening tag before the list.list_after
– Defines the closing tag after the list.item_before
– Defines the opening tag before each list item.item_after
– Defines the closing tag after each list item.date_format
– Sets the date format for the membership expiration.no_expire
– Sets the text to display when no expiration is set.
Examples and Usage
Basic Example – Showcasing the use of the shortcode to display the user’s memberships without any additional parameters.
[wpmem_user_memberships]
Advanced Examples
Displaying the user’s memberships with a custom title. In this case, the title is set to “Your Subscriptions”.
[wpmem_user_memberships title="Your Subscriptions"]
Using the shortcode to display the user’s memberships with a custom date format. The date is formatted according to the PHP date format.
[wpmem_user_memberships date_format="d-m-Y"]
Customizing the shortcode to show memberships with a custom message for non-expiring memberships. Here, the message is set to “Lifetime Membership”.
[wpmem_user_memberships no_expire="Lifetime Membership"]
Combining multiple parameters to customize the display of user memberships. In this example, we set a custom title, date format, and non-expiring membership message.
[wpmem_user_memberships title="Your Subscriptions" date_format="d-m-Y" no_expire="Lifetime Membership"]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_user_memberships]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_user_memberships', array( $this, 'sc_show_memberships' ) );
Shortcode PHP function:
function sc_show_memberships( $atts, $content, $tag ) {
$pairs = array(
'title_before' => '<h2>',
'title_after' => '</h2>',
'title' => __( 'Memberships', 'wp-members' ),
'list_before' => '<ul>',
'list_after' => '</ul>',
'item_before' => '<li>',
'item_after' => '</li>',
'date_format' => 'default',
'no_expire' => __( 'Does not expire', 'wp-members' ),
);
$args = shortcode_atts( $pairs, $atts, $tag );
$content = $args['title_before'] . $args['title'] . $args['title_after'];
$content .= $args['list_before'];
foreach ( wpmem_get_user_memberships() as $key => $value ) {
$exp_date = wpmem_get_user_expiration( $key );
if ( $exp_date > 1 ) {
$exp_formatted = ( 'default' == $args['date_format'] ) ? wpmem_format_date( $exp_date ) : date( $args['date_format'], $exp_date );
} else {
$exp_formatted = $args['no_expire'];
}
if ( $value ) {
$content .= $args['item_before'] . wpmem_get_membership_name( $key ) . " - " . $exp_formatted . $args['item_after'];
}
}
$content .= $args['list_after'];
return $content;
}
Code file location:
wp-members/wp-members/includes/class-wp-members-products.php
WP-Members Membership [wpmem_user_membership_posts] Shortcode
The WP-Members plugin shortcode ‘wpmem_user_membership_posts’ lists posts related to a user’s membership. It loops through each membership the user has, getting a list of content for each membership. . The function ‘sc_show_membership_posts’ is used to format the list, with options to customize the HTML tags before and after the title and list items.
Shortcode: [wpmem_user_membership_posts]
Parameters
Here is a list of all possible wpmem_user_membership_posts shortcode parameters and attributes:
title_before
– Specifies opening tag before the membership titletitle_after
– Specifies closing tag after the membership titlelist_before
– Defines opening tag before the list of postslist_after
– Specifies closing tag after the list of postsitem_before
– Determines opening tag before each post itemitem_after
– Sets closing tag after each post item
Examples and Usage
Basic example – Show membership posts without any additional parameters.
[wpmem_user_membership_posts /]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_user_membership_posts]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_user_membership_posts', array( $this, 'sc_show_membership_posts' ) );
Shortcode PHP function:
function sc_show_membership_posts( $atts, $content, $tag ) {
$pairs = array(
'title_before' => '<h2>',
'title_after' => '</h2>',
'list_before' => '<ul>',
'list_after' => '</ul>',
'item_before' => '<li>',
'item_after' => '</li>',
);
$args = shortcode_atts( $pairs, $atts, $tag );
// Go through each membership the user has.
foreach ( wpmem_get_user_memberships() as $key => $value ) {
// Get a list of content for this membership.
$ids = wpmem_get_membership_post_list( $key );
if ( $ids ) {
$title = $args['title_before'] . wpmem_get_membership_name( $key ) . $args['title_after'];
$post_list = $args['list_before'];
foreach ( $ids as $id ) {
$title = get_the_title( $id );
$link = '<a href="' . get_permalink( $id ) . '">' . $title . '</a>';
$post_list .= $args['item_before'] . $link . $args['item_after'];
}
$post_list .= $args['list_after'];
$content .= $title . $post_list;
}
}
return $content;
}
Code file location:
wp-members/wp-members/includes/class-wp-members-products.php
WP-Members Membership [wpmem_field] Shortcode
The WP-Members plugin shortcode, ‘wpmem_field’, is designed to retrieve and display specific user data. This shortcode accepts a variety of parameters, allowing you to customize the output. The shortcode function processes the attributes to determine the field and user ID. It then retrieves the user data and sanitizes the user ID. Depending on the field type (e.g., select, radio, multiselect, file, image, textarea, date), it handles the output differently. It also provides options to make the output clickable, display the field label, and filter the shortcode before returning the value.
Shortcode: [wpmem_field]
Parameters
Here is a list of all possible wpmem_field shortcode parameters and attributes:
field
– specifies the field to be usedid
– identifies the user, can be ‘author’ or specific user IDdisplay
– controls how field data is displayed, ‘raw’ for raw datawrapper_tag
– defines HTML tag for the wrapperwrapper_id
– sets the ID for the wrapperwrapper_class
– sets the class for the wrapperitem_tag
– defines HTML tag for the itemunderscores
– if set to ‘off’, will remove underscores from valueclickable
– if set to ‘true’, makes the content clickablelabel
– if set to ‘true’, displays field labelsize
– sets the size for ‘image’ fieldformat
– sets the format for ‘date’ field
Examples and Usage
Basic Example – Displaying User’s Login Name
[wpmem_field field="user_login"]
This shortcode will display the login name of the currently logged in user.
Advanced Examples
Displaying User’s First Name with Label
[wpmem_field field="first_name" label=true]
This shortcode will display the first name of the currently logged in user along with the label “First Name”.
Displaying Author’s Email in a Post
[wpmem_field field="user_email" id="author"]
This shortcode will display the email of the author of the current post. This can be useful to provide contact information within a post or article.
Displaying User’s Website with Clickable Link
[wpmem_field field="user_url" clickable=true]
This shortcode will display the website URL of the currently logged in user as a clickable link, allowing other users to easily visit the website.
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_field]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_field', array( $this, 'fields' ) );
Shortcode PHP function:
function fields( $atts, $content, $tag ) {
// What field?
if ( isset( $atts[0] ) ) {
$field = $atts[0];
} elseif ( isset( $atts['field'] ) ) {
$field = $atts['field'];
} else {
return; // If the field is not directly set in the attributes array, or keyed as "field", then it's not used correctly so return void.
}
// What user?
if ( isset( $atts['id'] ) ) {
if ( 'author' == $atts['id'] ) {
global $post;
$field_user_id = get_post_field( 'post_author', $post->ID );
// Alternate method:
// $field_user_id = get_the_author_meta( 'ID' );
} else {
$field_user_id = ( $atts['id'] == 'get' ) ? wpmem_get( 'uid', '', 'get' ) : $atts['id'];
}
} else {
$field_user_id = get_current_user_id();
}
// Sanitize the result.
$sanitized_user_id = intval( $field_user_id );
// Get the user data.
$user_info = get_userdata( $sanitized_user_id );
// If there is userdata.
if ( $user_info && isset( $user_info->{$field} ) ) {
global $wpmem;
$fields = wpmem_fields();
$field_type = ( 'user_login' == $field || ! isset( $fields[ $field ] ) ) ? 'text' : $fields[ $field ]['type'];
$user_info_field = ( isset( $field ) && is_object( $user_info ) ) ? $user_info->{$field} : '';
$result = false;
// Handle each field type.
switch ( $field_type ) {
// Select and radio groups have single selections.
case 'select':
case 'radio':
$result = ( isset( $atts['display'] ) && 'raw' == $atts['display'] ) ? $user_info_field : wpmem_select_field_display( $field, $user_info_field );
break;
// Multiple select and multiple checkbox have multiple selections.
case 'multiselect':
case 'multicheckbox':
case 'membership':
if ( isset( $atts['display'] ) && 'raw' == $atts['display'] ) {
$result = $user_info_field;
} else {
$saved_vals = explode( $fields[ $field ]['delimiter'], $user_info_field );
$result = ''; $x = 1;
if ( 'list' == $atts['display'] ) {
/**
* Filter list multi field list display HTML parts.
*
* @since 3.4.5
*
* @param array {
* The HTML parts (defaults as a bulleted list)
*
* @type string $wrapper_before
* @type string $item_before
* @type string $item_after
* @type string $wrapper_after
* }
* @param string $field
* /
$multi_args = apply_filters( 'wpmem_field_shortcode_multi_args', array(
'wrapper_before' => '<ul id="wpmem_sc_field_' . $field . '">',
'item_id' => 'wpmem-sc-multi-' . $field,
'item_class' => 'wpmem-sc-multi',
'item_before' => '<li id="%s" class="%s">',
'item_after' => '</li>',
'wrapper_after' => '</ul>',
), $field );
foreach ( $saved_vals as $value ) {
$rows[ $value ] = array(
'item_before' => $multi_args['item_before'],
'id' => $multi_args['item_id'] . '-' . $value,
'class' => $multi_args['item_class'],
'value' => $value,
'title' => $value,
'item_after' => $multi_args['item_after'],
);
}
/**
* Filter the row parts
*
* @since 3.4.5
*
* @param array $rows
* @param string $field
* /
$rows = apply_filters( 'wpmem_field_shortcode_multi_rows', $rows, $field );
$row_items = '';
foreach ( $rows as $value => $row ) {
$row_items .= sprintf( $row['item_before'], esc_attr( $row['id'] . '-' . $row['value'] ), esc_attr( $row['class'] ) ) . esc_attr( $row['title'] ) . $row['item_after'];
}
$result = $multi_args['wrapper_before'] . $row_items . $multi_args['wrapper_after'];
*/
$args = array(
'wrapper' => array(
'tag' => ( isset( $atts['wrapper_tag'] ) ) ? $atts['wrapper_tag'] : 'ul',
'atts' => array(
'id' => ( isset( $atts['wrapper_id'] ) ) ? $atts['wrapper_id'] : 'wpmem_field_' . $field,
'class' => ( isset( $atts['wrapper_class'] ) ) ? $atts['wrapper_class'] : 'wpmem-field-multi-list',
),
),
);
foreach ( $saved_vals as $value ) {
$args['item'][ $value ] = array(
'tag' => ( isset( $atts['item_tag'] ) ) ? $atts['item_tag'] : 'li',
'atts' => array(
'id' => 'wpmem_field_' . $field . '_' . $value,
'class' => 'wpmem-field-' . $field . '-item-' . $value,
),
'content' => $value
);
}
/**
* Filter list multi field list display HTML parts.
*
* @since 3.4.5
*
* @param array {
* The HTML parts (defaults as a bulleted list)
*
* @type array $wrapper {
* The wrapper parts.
*
* @type string $tag The HTML tag (default: ul)
* @type array $atts The HTML tag attributes
* @type string $content The content wrapped by the tag (default: list items)
* }
* @type array $item {
* An item for each list item.
*
* @type string $tag The HTML tag (default: li)
* @type array $atts The HTML tag attributes
* @type string $content The list item value
* }
* }
* @param string $field
*/
$multi_args = apply_filters( 'wpmem_field_sc_multi_html', $args, $field );
$list = '';
foreach ( $multi_args['item'] as $item ) {
$list .= rktgk_build_html_tag( $item );
}
$multi_args['wrapper']['content'] = $list;
$result = rktgk_build_html_tag( $multi_args['wrapper'] );
} else {
foreach ( $saved_vals as $value ) {
$result.= ( $x > 1 ) ? ', ' : ''; $x++;
$result.= wpmem_select_field_display( $field, $value );;
}
}
}
break;
case 'file':
case 'image':
if ( isset( $atts['display'] ) ) {
switch ( $atts['display'] ) {
case "url":
$result = wp_get_attachment_url( $user_info_field );
break;
case "raw":
default:
$result = $user_info_field;
break;
}
} else {
if ( 'file' == $field_type ) {
$attachment_url = wp_get_attachment_url( $user_info_field );
/**
* Filter the file html tag parts.
*
* @since 3.4.5
*
* @param array $args
* @param string $field
*/
$html_args = apply_filters( 'wpmem_field_sc_file_html', array(
'tag' => 'a',
'atts' => array(
'href' => esc_url( $attachment_url ),
'id' => ( isset( $atts['id'] ) ) ? esc_attr( $atts['id'] ) : esc_attr( 'wpmem_field_file_' . $field ),
'class' => ( isset( $atts['class'] ) ) ? esc_attr( $atts['class'] ) : esc_attr( 'wpmem-field-file-' . $field ),
),
'content' => get_the_title( $user_info_field ),
), $field );
$result = ( $attachment_url ) ? rktgk_build_html_tag( $html_args ) : '';
} else {
$size = 'thumbnail';
if ( isset( $atts['size'] ) ) {
$sizes = array( 'thumbnail', 'medium', 'large', 'full' );
$size = ( ! in_array( $atts['size'], $sizes ) ) ? explode( ",", $atts['size'] ) : $atts['size'];
}
$image = wp_get_attachment_image_src( $user_info_field, $size );
/**
* Filter the image html tag parts.
*
* @since 3.4.5
*
* @param array $args
* @param string $field
*/
$html_args = apply_filters( 'wpmem_field_sc_image_html', array(
'tag' => 'img',
'atts' => array(
'src' => esc_url( $image[0] ),
'width' => esc_attr( $image[1] ),
'height' => esc_attr( $image[2] ),
'id' => ( isset( $atts['id'] ) ) ? esc_attr( $atts['id'] ) : esc_attr( 'wpmem_field_img_' . $field ),
'class' => ( isset( $atts['class'] ) ) ? esc_attr( $atts['class'] ) : esc_attr( 'wpmem-field-img-' . $field )
),
), $field );
$result = ( $image ) ? rktgk_build_html_tag( $html_args ) : '';
}
}
break;
case 'textarea':
// Handle line breaks for textarea fields
$result = ( isset( $atts['display'] ) && 'raw' == $atts['display'] ) ? $user_info_field : nl2br( $user_info_field );
break;
case 'date':
if ( isset( $atts['format'] ) ) {
// Formats date: https://secure.php.net/manual/en/function.date.php
$result = ( '' != $user_info_field ) ? date( $atts['format'], strtotime( $user_info_field ) ) : '';
} else {
// Formats date to whatever the WP setting is.
$result = ( '' != $user_info_field ) ? date_i18n( get_option( 'date_format' ), strtotime( $user_info_field ) ) : '';
}
break;
// Handle all other fields.
default:
$result = ( ! $result ) ? $user_info_field : $result;
break;
}
// Remove underscores from value if requested (default: on).
if ( isset( $atts['underscores'] ) && 'off' == $atts['underscores'] && $user_info ) {
$result = str_replace( '_', ' ', $result );
}
$content = ( $content ) ? $result . $content : $result;
// Make it clickable?
$content = ( isset( $atts['clickable'] ) && ( true == $atts['clickable'] || 'true' == $atts['clickable'] ) ) ? make_clickable( $content ) : $content;
// Display field label?
$content = ( isset( $fields[ $field ] ) && isset( $atts['label'] ) && ( true == $atts['label'] ) ) ? $fields[ $field ]['label'] . ": " . $content : $content;
}
/**
* Filters the field shortcode before returning value.
*
* @since 3.2.5
* @since 3.4.5 Added $field
*
* @param string $content
* @param array $atts
* @param string $field
*/
$content = apply_filters( 'wpmem_field_shortcode', $content, $atts, $field );
return do_shortcode( $content );
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_logged_in] Shortcode
The WP-Members shortcode ‘wpmem_logged_in’ is designed to manage user accessibility. It checks if a user is logged in, their status, role, and specific attributes like meta keys and product access.
Shortcode: [wpmem_logged_in]
Parameters
Here is a list of all possible wpmem_logged_in shortcode parameters and attributes:
status
– Indicates the user’s login status (‘in’, ‘out’, ‘sub’).id
– Specifies the user ID(s) to check for.role
– Specifies the user role(s) to check for.meta_key
– Specifies the meta key to check for in user’s data.meta_value
– Used with ‘meta_key’, specifies the meta value to check for.compare
– Used with ‘meta_key’, specifies the comparison operator (‘=’, ‘!=’).product
– Checks if the user has access to a specific product.membership
– Checks if the user has a specific membership.msg
– Determines if a message should be displayed for subscription or product access.not_in
– Checks if the user does not have a specified product or membership.wrap_id
– Specifies the ID for the optional wrapper.wrap_class
– Specifies the class for the optional wrapper.wrap_tag
– Specifies the HTML tag for the optional wrapper.
Examples and Usage
Basic example – The following shortcode checks if a user is logged in. If the user is logged in, the content within the shortcode will be displayed.
[wpmem_logged_in]Welcome, you are logged in![/wpmem_logged_in]
Using the shortcode to check if a user with a specific role is logged in. If a user with the specified role is logged in, the content within the shortcode will be displayed.
[wpmem_logged_in role="administrator"]Hello admin, you're logged in![/wpmem_logged_in]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_logged_in]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_logged_in', array( $this, 'logged_in' ) );
Shortcode PHP function:
function logged_in( $atts, $content, $tag ) {
global $wpmem;
// Handles the 'status' attribute.
if ( ( isset( $atts['status'] ) ) || $tag == 'wpmem_logged_in' ) {
$do_return = false;
// If there is a status attribute of "out" and the user is not logged in.
$do_return = ( isset( $atts['status'] ) && $atts['status'] == 'out' && ! is_user_logged_in() ) ? true : $do_return;
if ( is_user_logged_in() ) {
// In case $current_user is not already global
$current_user = wp_get_current_user();
// If there is a status attribute of "in" and the user is logged in.
$do_return = ( isset( $atts['status'] ) && $atts['status'] == 'in' ) ? true : $do_return;
// If using the wpmem_logged_in tag with no attributes & the user is logged in.
$do_return = ( $tag == 'wpmem_logged_in' && ( ! $atts ) ) ? true : $do_return;
// If there is an "id" attribute and the user ID is in it.
if ( isset( $atts['id'] ) ) {
$ids = explode( ',', $atts['id'] );
foreach ( $ids as $id ) {
if ( trim( $id ) == $current_user->ID ) {
$do_return = true;
}
}
}
// If there is a "role" attribute and the user has a matching role.
if ( isset( $atts['role'] ) ) {
$roles = explode( ',', $atts['role'] );
if ( wpmem_user_has_role( $roles ) ) {
$do_return = true;
}
}
// If there is a status attribute of "sub" and the user is logged in.
if ( ( isset( $atts['status'] ) ) && $atts['status'] == 'sub' ) {
if ( defined( 'WPMEM_EXP_MODULE' ) && $wpmem->use_exp == 1 ) {
if ( ! wpmem_is_user_current() ) {
$do_return = true;
} elseif ( $atts['msg'] == "true" ) {
$do_return = true;
$content = wpmem_sc_expmessage();
}
}
}
// If there is a meta key attribute.
if ( isset( $atts['meta_key'] ) ) {
$do_return = false;
$value = ( isset( $atts['meta_value'] ) ) ? $atts['meta_value'] : false;
if ( ! isset( $atts['compare'] ) || "=" == $atts['compare'] ) {
if ( wpmem_user_has_meta( $atts['meta_key'], $value ) ) {
$do_return = true;
}
}
if ( isset( $atts['compare'] ) && "!=" == $atts['compare'] ) {
if ( ! wpmem_user_has_meta( $atts['meta_key'], $value ) ) {
$do_return = true;
}
}
}
// If there is a product attribute.
if ( isset( $atts['product'] ) || isset( $atts['membership'] ) ) {
// @todo What if attribute is comma separated/multiple?
$membership = ( isset( $atts['membership'] ) ) ? $atts['membership'] : $atts['product'];
$message = ( isset( $atts['msg'] ) && ( true === $atts['msg'] || "true" === strtolower( $atts['msg'] ) ) ) ? true : false;
$not_in = ( isset( $atts['not_in'] ) && "false" != $atts['not_in'] ) ? true : false;
if ( true == $not_in ) {
$do_return = ( wpmem_user_has_access( $membership ) || ! is_user_logged_in() ) ? false : true;
} else {
if ( wpmem_user_has_access( $membership ) ) {
$do_return = true;
} elseif ( true === $message ) {
$do_return = true;
$settings = array(
'wrapper_before' => '<div class="product_restricted_msg">',
'msg' => sprintf( wpmem_get_text( 'product_restricted' ), wpmem_get_membership_name( $membership ) ),
'wrapper_after' => '</div>',
);
/**
* Filter the access failed message.
*
* @since 3.3.0
* @since 3.3.3 Changed from 'wpmem_sc_product_access_denied'
*
* @param array $settings.
*/
$settings = apply_filters( 'wpmem_sc_product_restricted', $settings );
$content = $settings['wrapper_before'] . $settings['msg'] . $settings['wrapper_after'];
}
}
}
// Prevents display if the current page is the user profile and an action is being handled.
if ( ( wpmem_current_url( true, false ) == wpmem_profile_url() ) && isset( $_GET['a'] ) ) {
$do_return = false;
}
// Adds optional wrapper.
if ( isset( $atts['wrap_id'] ) || isset( $atts['wrap_class'] ) ) {
$tag = ( isset( $atts['wrap_tag'] ) ) ? $atts['wrap_tag'] : 'div';
$wrapper = '<' . $tag;
$wrapper .= ( isset( $atts['wrap_id'] ) ) ? ' id="' . $atts['wrap_id'] . '"' : '';
$wrapper .= ( isset( $atts['wrap_class'] ) ) ? ' class="' . $atts['wrap_class'] . '"' : '';
$wrapper .= '>';
$content = $wrapper . $content . '</' . $tag . '>';
}
}
// Return content (or empty content) depending on the result of the above logic.
return ( $do_return ) ? do_shortcode( $content ) : '';
}
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_logged_out] Shortcode
The WP-Members plugin shortcode ‘wpmem_logged_out’ is designed to display content only when a user is logged out. It uses the ‘is_user_logged_in()’ function to check the user status.
Shortcode: [wpmem_logged_out]
Examples and Usage
Basic example – The shortcode ‘wpmem_logged_out’ is used to display content only to users who are not logged in. The content to be displayed is placed between the opening and closing tags of the shortcode.
[wpmem_logged_out] Welcome, Guest! Please login to access more features. [/wpmem_logged_out]
Advanced example – The shortcode can be used in conjunction with other shortcodes to display different content to logged out users. In the example below, a contact form with the id ‘1’ is displayed to users who are not logged in.
[wpmem_logged_out] [contact-form-7 id="1"] [/wpmem_logged_out]
Another advanced usage is to display a custom message along with a login form to the users who are not logged in. The login form shortcode is nested within the ‘wpmem_logged_out’ shortcode.
[wpmem_logged_out] You need to be logged in to view this content. [wpmem_form login] [/wpmem_logged_out]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_logged_out]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_logged_out', array( $this, 'logged_out' ) );
Shortcode PHP function:
function logged_out( $atts, $content, $tag ) {
return ( ! is_user_logged_in() ) ? do_shortcode( $content ) : '';
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_logout] Shortcode
The WP-Members shortcode ‘wpmem_logout’ facilitates user logout functionality. It checks if a user is logged in and creates a logout link. When clicked, this link logs the user out. The URL for redirection after logout can be set via ‘url’ attribute. The content of the link is customizable, defaulting to ‘Click here to log out.’
Shortcode: [wpmem_logout]
Parameters
Here is a list of all possible wpmem_logout shortcode parameters and attributes:
url
– The destination webpage after logging outcontent
– Custom text for the logout link
Examples and Usage
Basic example – A simple logout link with default text
[wpmem_logout /]
Advanced examples
Customize the logout link with a specific redirect URL after logout
[wpmem_logout url="http://yourwebsite.com/after-logout" /]
Customize the logout link with a specific text and redirect URL after logout
[wpmem_logout url="http://yourwebsite.com/after-logout"]Click here to sign out[/wpmem_logout]
In the first advanced example, the ‘url’ attribute is used to specify a URL to redirect the user to after they have logged out. In the second advanced example, the text to be displayed for the logout link is customized by placing it between the opening and closing tags of the shortcode. The ‘url’ attribute is also used for redirection after logout.
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_logout]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_logout', array( $this, 'logout' ) );
Shortcode PHP function:
function logout( $atts, $content, $tag ) {
// Logout link shortcode.
if ( is_user_logged_in() && $tag == 'wpmem_logout' ) {
$link = ( isset( $atts['url'] ) ) ? add_query_arg( array( 'a'=>'logout', 'redirect_to'=>$atts['url'] ) ) : add_query_arg( 'a', 'logout' );
$text = ( $content ) ? $content : __( 'Click here to log out.', 'wp-members' );
return do_shortcode( '<a href="' . esc_url( $link ) . '">' . $text . '</a>' );
}
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_form] Shortcode
The WP-Members plugin shortcode ‘wpmem_form’ is used to display various forms such as login, register, password reset, user edit, and forgot username. Depending on the user’s logged-in status and the form specified in the attributes, it dynamically generates the appropriate form. The shortcode also supports redirection and texturize options. It’s highly customizable.
Shortcode: [wpmem_form]
Parameters
Here is a list of all possible wpmem_form shortcode parameters and attributes:
form
– Determines the type of form to be rendered (login, register, password, user_edit, forgot_username).redirect_to
– URL to redirect to after successful form submission.texturize
– If set to false, prevents WordPress from formatting the shortcode content.form_id
– Allows you to specify a unique ID for the form.
Examples and Usage
Basic example – Displaying a login form using the ‘wpmem_form’ shortcode without any additional parameters.
[wpmem_form]
Advanced examples
Using the shortcode to display a registration form by setting the ‘form’ attribute to ‘register’.
[wpmem_form form="register"]
Using the shortcode to display a login form with a custom ID and a redirect URL after successful login.
[wpmem_form form="login" form_id="custom_login_form" redirect_to="https://www.example.com/dashboard"]
Using the shortcode to display a password reset form by setting the ‘form’ attribute to ‘password’.
[wpmem_form form="password"]
Using the shortcode to display a user edit form by setting the ‘form’ attribute to ‘user_edit’.
[wpmem_form form="user_edit"]
Using the shortcode to display a ‘forgot username’ form by setting the ‘form’ attribute to ‘forgot_username’.
[wpmem_form form="forgot_username"]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_form]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_form', array( $this, 'forms' ) );
Shortcode PHP function:
function forms( $atts, $content, $tag ) {
if ( is_admin() ) {
return;
}
global $wpmem, $wpmem_themsg;
// Defaults.
$defaults = array(
'form' => '',
'redirect_to' => null,
'texturize' => false,
'form_id' => false,
);
$atts = wp_parse_args( $atts, $defaults );
$atts['form'] = ( isset( $atts[0] ) ) ? $atts[0] : 'login';
unset( $atts[0] );
$customizer = ( is_customize_preview() ) ? get_theme_mod( 'wpmem_show_logged_out_state', false ) : false;
// If $atts is an array, get the tag from the array so we know what form to render.
switch ( $atts['form'] ) {
case 'wp_login':
if ( is_user_logged_in() && '1' != $customizer ) {
// If the user is logged in, return any nested content (if any) or the default bullet links if no nested content.
$content = ( $content ) ? $content : $this->render_links( 'login' );
} else {
$atts['echo'] = false;
$atts['redirect'] = ( isset( $atts['redirect'] ) ) ? $atts['redirect'] : ( ( isset( $atts['redirect_to'] ) ) ? $atts['redirect_to'] : wpmem_current_url() );
$content = wpmem_wp_login_form( $atts );
}
break;
case 'register':
// Set up register form args.
$reg_form_args = array( 'tag' => 'new' );
if ( isset( $redirect_to ) ) {
$reg_form_args['redirect_to'] = $redirect_to;
}
if ( is_user_logged_in() && '1' != $customizer ) {
/*
* If the user is logged in, return any nested content (if any)
* or the default bullet links if no nested content.
*/
$content = ( $content ) ? $content : $this->render_links( 'register' );
} elseif ( is_user_logged_in() && is_customize_preview() && get_theme_mod( 'wpmem_show_form_message_dialog', false ) ) {
$wpmem_themsg = __( "This is a generic message to display the form message dialog in the Customizer.", 'wp-members' );
$content = wpmem_get_display_message( $wpmem->regchk, $wpmem_themsg );
$content .= wpmem_register_form( $reg_form_args );
} else {
if ( $wpmem->regchk == 'loginfailed' ) {
$content = $wpmem->dialogs->login_failed() . wpmem_login_form();
break;
}
// @todo Can this be moved into another function? Should $wpmem get an error message handler?
if ( $wpmem->regchk == 'captcha' ) {
global $wpmem_captcha_err;
$wpmem_themsg = wpmem_get_text( 'reg_captcha_err' ) . '<br /><br />' . $wpmem_captcha_err;
}
$content = ( $wpmem_themsg || $wpmem->regchk == 'success' ) ? wpmem_get_display_message( $wpmem->regchk, $wpmem_themsg ) : '';
$content .= ( $wpmem->regchk == 'success' ) ? wpmem_login_form() : wpmem_register_form( $reg_form_args );
}
break;
case 'password':
$content = $this->render_pwd_reset( $wpmem->regchk, $content );
break;
case 'user_edit':
$content = $this->render_user_edit( $wpmem->regchk, $content, $atts );
break;
case 'forgot_username':
$content = $this->render_forgot_username( $wpmem->regchk, $content );
break;
// @todo Review - is this actually ever triggered?
case 'customizer_login':
$content = wpmem_login_form();
break;
// @todo Review - is this actually ever triggered?
case 'customizer_register':
$content = wpmem_register_form( 'new' );
break;
case 'login':
default:
if ( is_user_logged_in() && '1' != $customizer ) {
// If the user is logged in, return any nested content (if any) or the default bullet links if no nested content.
$content = ( $content ) ? $content : $this->render_links( 'login' );
} else {
$content = '';
if ( $wpmem->regchk == 'loginfailed' || ( is_customize_preview() && get_theme_mod( 'wpmem_show_form_message_dialog', false ) ) ) {
$content = wpmem_get_display_message( 'loginfailed' );
}
$form_id = ( $atts['form_id'] ) ? $atts['form_id'] : 'wpmem_login_form';
$content .= wpmem_login_form( array( 'redirect_to'=>$atts['redirect_to'], 'form_id'=>$form_id ) );
}
break;
}
return do_shortcode( $content );
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_show_count] Shortcode
The WP-Members plugin shortcode ‘wpmem_show_count’ is designed to count the number of users. It allows to display total users or users based on specific roles or meta values.
Shortcode: [wpmem_show_count]
Parameters
Here is a list of all possible wpmem_show_count shortcode parameters and attributes:
key
– The meta key to filter users by.value
– The value of the meta key to match.role
– The user role to count.label
– The label to prepend to the user count.
Examples and Usage
Basic example – The WP-Members plugin shortcode ‘wpmem_show_count’ can be used to display the total count of users on your WordPress site.
[wpmem_show_count /]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_show_count]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_show_count', array( $this, 'user_count' ) );
Shortcode PHP function:
function user_count( $atts, $content, $tag ) {
if ( isset( $atts['key'] ) && isset( $atts['value'] ) ) {
// If by meta key.
global $wpdb;
$user_count = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = %s",
$atts['key'],
$atts['value']
) );
} else {
// If no meta, it's a total count.
$users = count_users();
$user_count = ( isset( $atts['role'] ) ) ? $users['avail_roles'][ $atts['role'] ] : $users['total_users'];
}
// Assemble the output and return.
$content = ( isset( $atts['label'] ) ) ? $atts['label'] . ' ' . $user_count : $content . ' ' . $user_count;
return do_shortcode( $content );
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_profile] Shortcode
The WP-Members plugin shortcode ‘wpmem_profile’ is used to manage user profiles. It checks if the user is logged in, handles actions like profile editing, password change, and account renewal.
Shortcode: [wpmem_profile]
Parameters
Here is a list of all possible wpmem_profile shortcode parameters and attributes:
register
– Determines whether to show or hide the registration formredirect_to
– URL where users are redirected after certain actions
Examples and Usage
Basic example – Displaying the user profile with default settings
[wpmem_profile /]
Advanced examples
Displaying the user profile but hiding the registration form
[wpmem_profile register="hide" /]
Displaying the user profile and redirecting the user to a specific URL after profile update
[wpmem_profile redirect_to="http://yourwebsite.com/your-page" /]
Combining both parameters to display the user profile, hide the registration form, and redirecting the user to a specific URL after profile update
[wpmem_profile register="hide" redirect_to="http://yourwebsite.com/your-page" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_profile]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_profile', array( $this, 'user_profile' ) );
Shortcode PHP function:
function user_profile( $atts, $content, $tag ) {
global $wpmem, $wpmem_themsg;
$pairs = array(
'register' => 'show',
'redirect_to' => '',
);
$args = shortcode_atts( $pairs, $atts, $tag );
// @todo $redirect_to is not currently used in the user profile.
$redirect_to = $args['redirect_to'];
$hide_register = ( isset( $args['register'] ) && 'hide' == $args['register'] ) ? true : false;
$content = '';
if ( $wpmem->regchk == "captcha" ) {
global $wpmem_captcha_err;
$wpmem_themsg = wpmem_get_text( 'reg_captcha_err' ) . '<br /><br />' . $wpmem_captcha_err;
}
if ( is_user_logged_in() ) {
switch( $wpmem->action ) {
case "edit":
$content = $content . wpmem_register_form( 'edit' );
break;
case "update":
// Determine if there are any errors/empty fields.
if ( $wpmem->regchk == "updaterr" || $wpmem->regchk == "email" ) {
$content = $content . wpmem_get_display_message( $wpmem->regchk, $wpmem_themsg );
$content = $content . wpmem_register_form( 'edit' );
} else {
//Case "editsuccess".
$content = $content . wpmem_get_display_message( $wpmem->regchk, $wpmem_themsg );
$content = $content . $this->render_links();
}
break;
case "pwdchange":
$content = $this->render_pwd_reset( $wpmem->regchk, $content );
$content = ( 'pwdchangesuccess' == $wpmem->regchk ) ? $content . $this->render_links() : $content;
break;
case "renew":
if ( function_exists( 'wpmem_renew' ) ) {
$content = wpmem_renew();
} else {
$content = '';
}
break;
default:
$content = $this->render_links();
break;
}
} else {
if ( ( 'login' == $wpmem->action ) || ( 'register' == $wpmem->action && ! $hide_register ) ) {
$content = wpmem_get_display_message( $wpmem->regchk, $wpmem_themsg );
$content.= ( 'loginfailed' == $wpmem->regchk || 'success' == $wpmem->regchk ) ? wpmem_login_form() : wpmem_register_form();
} elseif ( 'pwdreset' == $wpmem->action ) {
$content = $this->render_pwd_reset( $wpmem->regchk, $content );
} elseif ( 'set_password_from_key' == $wpmem->action ) {
$content = ( false != $wpmem->pwd_reset->content ) ? $wpmem->pwd_reset->content : $content;
} elseif ( 'getusername' == $wpmem->action ) {
$content = $this->render_forgot_username( $wpmem->regchk, $content );
} else {
$content = $content . wpmem_login_form( 'profile' );
$content = ( ! $hide_register ) ? $content . wpmem_register_form() : $content;
}
}
return $content;
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_loginout] Shortcode
The WP-Members shortcode ‘wpmem_loginout’ is designed to manage the login/logout process. It triggers the ‘loginout’ function which returns a login/out link.
Shortcode: [wpmem_loginout]
Examples and Usage
Basic example – The given shortcode [wpmem_loginout] can be used to display a login/logout link on your WordPress site. It’s a simple and straightforward way to manage user authentication.
[wpmem_loginout /]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_loginout]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_loginout', array( $this, 'loginout' ) );
Shortcode PHP function:
function loginout( $atts, $content, $tag ) {
$link = wpmem_loginout( $atts );
return do_shortcode( $link );
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_tos] Shortcode
The WP-Members shortcode ‘wpmem_tos’ is designed for handling Terms of Service URLs. It checks if the URL starts with ‘http://’ or ‘https://’ and returns the secure URL.
Shortcode: [wpmem_tos]
Parameters
Here is a list of all possible wpmem_tos shortcode parameters and attributes:
url
– the web address to be returned by the shortcode
Examples and Usage
Basic example – A simple way to use the ‘wpmem_tos’ shortcode is to provide the URL attribute. This will generate a link to the specified URL.
[wpmem_tos url="https://www.example.com"]
Advanced examples
While the basic usage of the ‘wpmem_tos’ shortcode is quite straightforward, you can also use this shortcode in a more advanced way. For example, you could use it to dynamically generate a URL based on the site’s home URL. This can be useful if you want to create a link to a specific page on your site, but you don’t want to hardcode the entire URL.
[wpmem_tos url="/terms-and-conditions"]
In this example, the ‘url’ attribute is set to ‘/terms-and-conditions’. This is a relative URL, which means it’s relative to the site’s home URL. When this shortcode is processed, it will generate a link to ‘https://www.yoursite.com/terms-and-conditions’ (assuming that ‘https://www.yoursite.com’ is your site’s home URL).
Another advanced usage could involve using the shortcode within a template file. This can be done using the ‘do_shortcode’ function:
<?php echo do_shortcode('[wpmem_tos url="/privacy-policy"]'); ?>
This example would output a link to the site’s privacy policy page, again based on the site’s home URL.
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_tos]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_tos', array( $this, 'tos' ) );
Shortcode PHP function:
function tos( $atts, $content, $tag ) {
$url = ( strpos( $atts['url'], 'http://' ) || strpos( $atts['url'], 'https://' ) ) ? $atts['url'] : home_url( $atts['url'] );
return esc_url( $url );
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_avatar] Shortcode
The WP-Members plugin shortcode ‘wpmem_avatar’ is designed to display the avatar of a user. The shortcode functions by checking if a user ID has been specified. If it has, it retrieves and displays the avatar associated with that ID. Alternatively, if a user is logged in and no ID is specified, it will display the avatar of the currently logged in user.
Shortcode: [wpmem_avatar]
Parameters
Here is a list of all possible wpmem_avatar shortcode parameters and attributes:
id
– The unique identifier of the user’s avatarsize
– Defines the dimension of the avatar image
Examples and Usage
Basic example – Show the avatar of the currently logged-in user.
[wpmem_avatar /]
As per the shortcode function, if no ‘id’ attribute is provided and a user is logged in, it will display the avatar of the currently logged-in user.
Advanced examples
Display the avatar of a specific user by specifying the user’s ID.
[wpmem_avatar id=3 /]
In this example, the ‘id’ attribute is used to specify the ID of the user whose avatar you want to display. If the user with the given ID exists, the shortcode will display their avatar.
Specify the size of the avatar to be displayed.
[wpmem_avatar id=3 size=50 /]
Here, in addition to the ‘id’ attribute, the ‘size’ attribute is used to specify the size of the avatar. The size is measured in pixels, and in this example, the avatar will be displayed as a 50×50 pixel image.
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_avatar]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_avatar', array( $this, 'avatar' ) );
Shortcode PHP function:
function avatar( $atts, $content, $tag ) {
$content = '';
$size = ( isset( $atts['size'] ) ) ? $atts['size'] : '';
if ( isset( $atts['id'] ) ) {
$content = get_avatar( $atts['id'], $size );
} elseif ( is_user_logged_in() ) {
// If the user is logged in and this isn't specifying a user ID, return the current user avatar.
global $current_user;
wp_get_current_user();
$content = get_avatar( $current_user->ID, $size );
}
return do_shortcode( $content );
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_login_link] Shortcode
The WP-Members plugin shortcode ‘wpmem_login_link’ is designed to generate a login link. It checks if there are any attributes set, and if so, assigns them. If the tag is ‘wpmem_reg_link’, it sets the content to ‘Register’ if no content is specified, and returns a registration link. If not, it sets the content to ‘Log In’ if no content is specified, and returns a login link.
Shortcode: [wpmem_login_link]
Examples and Usage
Basic example – A simple usage of the ‘wpmem_login_link’ shortcode without any additional parameters. This will display a default login link.
[wpmem_login_link /]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_login_link]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_login_link', array( $this, 'login_link' ) );
Shortcode PHP function:
function login_link( $atts, $content, $tag ) {
if ( isset( $atts ) ) {
$args['attributes'] = $atts;
}
if ( 'wpmem_reg_link' == $tag ) {
$args['content'] = ( isset( $content ) && '' != $content ) ? $content : __( 'Register' );
return do_shortcode( wpmem_get_reg_link( $args ) );
} else {
$args['content'] = ( isset( $content ) && '' != $content ) ? $content : __( 'Log In' );
return do_shortcode( wpmem_get_login_link( $args ) );
}
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
WP-Members Membership [wpmem_login_button] Shortcode
The WP-Members plugin shortcode, ‘wpmem_login_button’, is designed to display a login button on your webpage. It uses the function ‘login_button’ to format the login/logout button.
Shortcode: [wpmem_login_button]
Examples and Usage
Basic example – A simple login button can be displayed using the ‘wpmem_login_button’ shortcode without any additional parameters.
[wpmem_login_button /]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpmem_login_button]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpmem_login_button', array( $this, 'login_button' ) );
Shortcode PHP function:
function login_button( $atts, $content, $tag ) {
$content = wpmem_loginout( array( 'format'=>'button' ) );
return do_shortcode( $content );
}
Code file location:
wp-members/wp-members/includes/class-wp-members-shortcodes.php
Conclusion
Now that you’ve learned how to embed the WP-Members Membership Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply