• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

nSiteful Web Builders

Building a Better Web - One Site at a Time.

  • Home
  • About
    • Testimonials
    • Resources
  • Web Sites
  • Online Marketing
  • WordPress Support
    • Customized WordPress Training
    • 60-for-60 Sessions
  • Web Applications
  • Blog
    • Archive Listing Minimalistic
    • Blog Articles Grouped by Category
    • Case Studies
    • General
    • Portfolio
    • Reviews
    • Snippets
    • Techniques
  • Contact Jeff
    • Purchase Retainer Consulting Hours
    • About Retainer Consulting Hours

By Jeff Cohan, February 5, 2023

Create a Custom Shortcode to Display a MemberPress Membership Price ANYWHERE on Your Website

Last updated September 8th, 2023 at 12:07 pm

Today, as I prepared to change the price of a membership on one of my MemberPress membership sites, I knew that there are several places on the site where the price of the membership was hard-coded. (For example, I’m displaying a custom pricing table that doesn’t utilize MemberPress Groups Price Box. I also have a callout on the home page which displays the price of this membership — also hard-coded.)

A membership price shortcode would eliminate the need to manually update all those places. However, although MemberPress has lots of shortcodes for content restriction, it doesn’t have one for displaying the price of a membership.

So I created one. Feel free to use this snippet.

Following are two snippets. The first is a bare-bones solution that gets the job done. The second is a bit more fancy.

Bare Bones

/* Gets/Returns price of the membership from postmeta table */
function jdc_get_product_price($mepr_product_id) {
	return get_post_meta($mepr_product_id, '_mepr_product_price', true);
}
/* The Shortcode */
function jdc_shortcode_mepr_product_price($atts) {
	$atts = shortcode_atts( array(
		'id'		=> '203',
	), $atts, 'membership_price' );
	$id 		= $atts['id'];
	return jdc_get_product_price( $id );
}
add_shortcode('membership_price', 'jdc_shortcode_mepr_product_price');

This shortcode displays the price as a number with 2 decimal places and no currency symbol.

  • The id parameter corresponds to the product ID

A Bit More Fancy

The function that gets and returns the price from the postmeta table is the same as above, as is the add_shortcode() call.

Here’s the fancier shortcode function (with the return line on separate lines for readability):

/* The Shortcode */
function jdc_shortcode_mepr_product_price($atts) {
	$atts = shortcode_atts( array(
		'id'		=> '203',
		'decimals'	=> '0',
		'sep'		=> '.',
		'thousands'	=> ',',
		'prefix'	=> '',
		'class' 	=> 'price__wrap',
		
	), $atts, 'membership_price' );
	$id 		= $atts['id'];
	$decimals 	= $atts['decimals'];
	$sep 		= $atts['sep'];
	$thousands 	= $atts['thousands'];
	$prefix		= $atts['prefix'];
	$class		= $atts['class'];
	$price = jdc_get_product_price( $id );
	return '<span class="'. $class . '">' . 
	$prefix . 
	number_format($price, $decimals, $sep, $thousands) . 
	'</span>';
}
add_shortcode('membership_price', 'jdc_shortcode_mepr_product_price');

This version of the shortcode offers more control. I added a default class of price__wrap that allows styling with CSS; you could replace that class name with some other to vary how it renders. (The default ‘id’ value is the ID of the membership whose price I changed.)

Example of the shortcode in use:
[membership_price id="999" decimals="2" prefix="$" class="special-price"]

(Of course, you could just enter the currency symbol before the shortcode without having to add the prefix parameter.)

Related Posts

  1. How to Create a Custom MemberPress Course Progress Page
  2. How to Override MemberPress Template Files
  3. Add Dynamic Table of Contents to a Series of WordPress Posts
  4. How (and Why) to Partially Protect Content on your Membership Site
  5. Use Custom Lesson Icons for MemberPress Courses

Written by Jeff Cohan · Categorized: Snippets · Tagged: MemberPress, Membership Sites, WordPress

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

mailchimp signup

Subscribe to get notified when new articles are published. Unsubscribe any time. No spam. I promise. Check out my newsletter archives.

social

Twitter Facebook LinkedIn

Recent Articles

  • Southeastern Bluegrass Association Membership Site September 8, 2023
  • How to Create a Custom MemberPress Course Progress Page August 30, 2023
  • 10 Tips for Using Basecamp Like a Pro August 17, 2023
  • How (and Why) to Partially Protect Content on your Membership Site July 24, 2023
  • Customize User Roles & Capabilities in WordPress: How (and Why) July 18, 2023

Filter By Category/Tag

Categories

  • Case Studies (7)
  • General (61)
  • Portfolio (6)
  • Reviews (12)
  • Snippets (17)
  • Techniques (41)

Popular Tags

Advanced Custom Fields Blogging Child Themes Content Marketing CSS Customer Service Custom Fields Custom Post Types Diagnostics Facebook FooGallery Genesis HTML Images iPhone Libra Live Chat Marketing Media MemberPress MemberPress Courses Membership Sites MySQL Photo Gallery php Pinterest Plugins Post Formats Pricing Project Management SEBA SEO Seth Godin Shortcodes Social Networking SurveyMonkey Surveys Taxonomies Twitter Video Web design Web forms WooCommerce WordPress YouTube

Footer

Background

Web Sites | WordPress Support | Web Applications.

Formally trained in liberal arts and education (I have a B.A. in Government from Harvard and studied Secondary Education at Rutgers Graduate School), I have honed my skills in the communication arts and sciences as a teacher, trainer, instructional designer, writer, photographer, calligrapher, helpdesk manager, database programmer, and multimedia developer.

(I've also been a group counselor, waiter, bartender, bicycle messenger boy, computer salesman, carpenter's helper, financial analyst, and school board president.)

Tech

Systems since 1983.
Web sites since 1994.
PHP since 2001.
WordPress since 2007.

Contact

770-772-5134
Email Jeff
Send Money
All Ways

Copyright 2023, nSiteful Web Builders, Inc.

 

Subscribe

Pardon the interruption. I know popups can be annoying. But I’d love to have you as a subscriber.

Sign up to be notified when new articles are published. Unsubscribe any time.

* indicates required

Powered by MailChimp

×