• 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 March 3rd, 2023 at 11:50 am

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 Override MemberPress Template Files
  2. Add Dynamic Table of Contents to a Series of WordPress Posts
  3. Use Custom Lesson Icons for MemberPress Courses
  4. Custom extensible PHP shortcode function for non-WordPress Web sites
  5. I Built a WordPress Plugin for Downloadable Files
  • Choose the best match.

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

  • Choose the best match.

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

  • Use Case for Custom Post Type: “In The News” March 10, 2023
  • Create a Custom Shortcode to Display a MemberPress Membership Price ANYWHERE on Your Website February 5, 2023
  • Avoid Direct Styling; Use CSS Instead September 21, 2022
  • Blog Tags: What They Are (and What They’re Not) August 5, 2022
  • How to Create a Simple Custom Events Plugin May 24, 2022

Filter By Category/Tag

Categories

  • Case Studies (7)
  • General (61)
  • Portfolio (5)
  • Reviews (12)
  • Snippets (16)
  • Techniques (38)

Popular Tags

Advanced Custom Fields Blogging Child Themes Content Marketing CSS CSS Grid Customer Service Custom Fields Custom Post Types Diagnostics Facebook FooGallery Genesis Gutenberg HTML Images iPhone Libra Live Chat Marketing Media MemberPress MemberPress Courses mu-plugins MySQL Photo Gallery php Pinterest Plugins Post Formats Pricing Project Management Security SEO Seth Godin Shortcodes Social Networking Surveys Taxonomies Trello Twitter Video Web design Web forms WordPress

siteground wp hosting

Web Hosting

wp101

EasyWordPresstutorialvideosforbeginners.
MemberPress CTA

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

×