• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Store
    • Cart
    • Checkout
  • Blog
    • Basics [Free]
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Contact
  • Sign In
  • Show Search
Hide Search
Home » Shortcodes » Create a custom shortcode to display content to registered members based on checkbox value

Create a custom shortcode to display content to registered members based on checkbox value

Chad Butler · Oct 12, 2012 ·

Suppose that you have a checkbox in your registration form and you want to have a shortcode to display content to users who have a certain value of that checkbox. This tutorial will show you how to create such a shortcode. This concept could also be extended to be any specific value from registration – it is only limited by your imagination.

This article is only available to WP-Members Support Subscribers. If you have an existing subscription, please login below. If you do not have a current support subscription, you can purchase a support subscription here.

Already a Member? Log In Here
   
Forgot password? Click here to reset

To gain full access to WP-Members premium content, a current support subscription is required. You can purchase an annual support subscription for as little as $59, which provides you with access to priority support, a customer support forum, access to code snippets, and more.

Why wait? Choose your subscription option here.

[
Why join?]

Shortcodes shortcodes

Welcome to RocketGeek Interactive › Forums › Create a custom shortcode to display content to registered members based on checkbox value

Tagged: shortcodes

  • This topic has 19 replies, 4 voices, and was last updated 5 years, 5 months ago by Chad Butler.
Viewing 17 reply threads
  • Author
    Posts
    • October 12, 2012 at 8:47 am #1460
      Chad Butler
      Keymaster

      Suppose that you have a checkbox in your registration form and you want to have a shortcode to display content to users who have a certain value of th
      [See the full post at: Create a custom shortcode to display content to registered members based on checkbox value]

    • October 19, 2012 at 10:10 am #1501
      HansRuedi Keller
      Participant

      [my-shortcode]This content will only be viewable by registered users who have a value of “yes” for “my-checkbox”.[/my-checkbox] should be

      [my-shortcode]This content will only be viewable by registered users who have a value of “yes” for “my-checkbox”.[/my-shortcode] I think…

      Chad, could it be that such a shortcode isn’t working in CPTs ?!?

       

    • October 19, 2012 at 10:27 am #1502
      HansRuedi Keller
      Participant

      Hhmmm… sometimes the keyboard is faster than a brain – or my brain. It’s working now. Thanks for your code.

    • October 19, 2012 at 1:10 pm #1503
      Chad Butler
      Keymaster

      No problem Hans!  Hope it works well for you!

    • October 29, 2012 at 1:17 pm #1556
      nantokuko
      Participant

      Hi,

      it’s possible to show or hide some fields, depending of the value of the checkbox, DURING the registration?

       

      thanks

    • October 29, 2012 at 5:04 pm #1558
      Chad Butler
      Keymaster

      That would only be possible with some client-side script (such as an AJAX script).  I couldn’t write something like that for you, but if you have some javascript skills, I could point you in the right direction of where to slip things into the plugin’s output.

       

    • October 29, 2012 at 5:37 pm #1560
      nantokuko
      Participant

      Hi Chad, thanks for reply. I’ll show my problem. I need that when users fill out the fields to register, they can (or check a chekbox, or click a button or something else) to show other fields. What do you suggest me? Any solution is ok!

      Thnks

    • October 29, 2012 at 8:13 pm #1561
      Chad Butler
      Keymaster

      I understand what you asking, but as I indicated, that’s something that would involve some custom client-side scripting (such as JavaScript).  That’s not something I could do for you.  Do you have an skill with JavaScript or do you have access to someone who does?

       

    • October 30, 2012 at 3:50 am #1562
      nantokuko
      Participant

      Hi Chad, i’m basic level in javascript. I’ve found on the web, some javascript functions about how to show or hide something, and how to detect the checking of a checkbox. It could be enough for us?
      Thanks

    • October 30, 2012 at 1:05 pm #1565
      Chad Butler
      Keymaster

      I would think that something could be put together.

      You would need to make sure that the registration form has all of the fields that you intend to use.  You would probably not want to have the hidden fields as required (as far as the WP-Members plugin is concerned).  Any required fields should probably be managed with the client side script that you write.

      While there are IDs that you could use for the input fields themselves, you might need to write some kind of filter for the registration form to wrap the label and the input field with some kind of ID that your script could recognize.  You should probably review the use of the wpmem_register_form filter for how to filter the registration form:

      https://rocketgeek.com/plugins/wp-members/users-guide/filter-hooks/wpmem_register_form/

      There are quite a few example tutorials linked there.  While not specific to what you are doing, they will give you a better idea of how the filter can be used so you can build a filter function to get done what you need.

       

    • October 30, 2012 at 1:44 pm #1566
      nantokuko
      Participant

      Hi Chad,

      go here http://www.misterwebby.com/index_demo.php/creare-web-form-in-modo-semplice-e-veloce and check “I would like to recieve updates.”

      I need something like this!!! 🙂

    • October 30, 2012 at 4:57 pm #1567
      nantokuko
      Participant

      Hi Chad, i was wrong the previous post!!!!!!

      go here http://www.misterwebby.com/index_demo.php/creare-web-form-in-modo-semplice-e-veloce, choose in the menu on the left “Registration”, then check “I would like to recieve updates.”

      I need something like this!!! 

    • November 2, 2012 at 9:27 am #1574
      nantokuko
      Participant

      Hi Chad,

      do you have read my previous post?

      go here http://www.misterwebby.com/index_demo.php/creare-web-form-in-modo-semplice-e-veloce, choose in the menu on the left “Registration”, then check “I would like to recieve updates.”

      I need something like this!!!

    • November 2, 2012 at 10:07 am #1575
      Chad Butler
      Keymaster

      I did, and I have looked at that.  It doesn’t really change my original answer.

      You can implement something like that by working with the registration form filter.  But you will need to have a handle on how the filter works.

    • November 2, 2012 at 10:13 am #1576
      nantokuko
      Participant

      Sorry,

      but how  registration form filter works? What file i have to manage for my test?

    • November 2, 2012 at 11:30 am #1577
      Chad Butler
      Keymaster

      We are getting way off topic here from “Create a custom shortcode to display content to registered members based on checkbox value” so let’s pick this up over here:

      https://rocketgeek.com/forums/topic/hide-and-show-with-checkbox-value/

       

    • September 3, 2015 at 4:12 pm #8492
      lancelunsford
      Participant

      I’ve had some success getting two additional member types into by code, but I’m trying to add one called “340B” … and when I update the functions file, the whole site goes down. Page goes white.

      Here’s the code I’m pasting:

      add_shortcode( '340B', '340B' );
      function add_my_shortcode( $attr, $content = null )
      {
      	global $user_ID;
      	$val = get_user_meta( $user_ID, '340B', true );
      
      	if( is_user_logged_in() && ( $val == 'yes' ) ) {
      		return do_shortcode( $content );
      	}
      
      	return;
      • September 3, 2015 at 8:04 pm #8497
        Chad Butler
        Keymaster

        It looks like you are missing the closing curly brace “}” for the function. It should be:

        add_shortcode( '340B', '340B' );
        function add_my_shortcode( $attr, $content = null )
        {
        	global $user_ID;
        	$val = get_user_meta( $user_ID, '340B', true );
        
        	if( is_user_logged_in() && ( $val == 'yes' ) ) {
        		return do_shortcode( $content );
        	}
        
        	return;
        }
        
    • September 7, 2015 at 7:36 pm #8516
      lancelunsford
      Participant

      I added a request last week regarding a problem I was having on this, and we made sure the extra curly bracket “}” was in there that we thought was missing … I’m still having trouble adding a third member type in … here’s the code I’m adding. any idea what’s wrong here?

      add_shortcode( 'gov-relations', 'gov-relations' );
      function exec_com( $attr, $content = null )
      {
      	global $user_ID;
      	$val = get_user_meta( $user_ID, 'gov-relations', true );
      
      	if( is_user_logged_in() && ( $val == 'yes' ) ) {
      		return do_shortcode( $content );
      	}
      
      	return;
      }
      • September 7, 2015 at 9:52 pm #8518
        Chad Butler
        Keymaster

        You actually have a couple of problems here (and one of them was in the original question – sorry, I should have caught it then).

        First, when you use add_shortcode(), that function takes two arguments. First is the shortcode tag, the second is the function that will be called for that shortcode.

        So, let’s look at what you have for add_shortcode():
        add_shortcode( 'gov-relations', 'gov-relations' );

        This means for your shortcode [gov-relations] it will call the function gov-relations().

        First, your function is called “exec_com()”, not “gov-relations()”.

        Your other problem is the use of the hyphen. First, no function can ever have a hyphen in its name. So “exec_com()” would be OK, but “gov-relations()” would be invalid.

        Hyphens are also discouraged in WordPress shortcode tags. So [gov-relations] would be discouraged. [gov_relations] would be OK. The hyphen may not throw you an error here, but it is possible.

        I would also discourage hyphens in meta keys for custom fields. However, if you already have things up and running with user data in place for the meta key gov-relations, then I would leave it. But if you don’t have existing user data in there, I would change that field to something like gov_relations.

        That’s a lot to pack in here, so the main thing to mention about what is wrong with the code you posted is that your shortcode is calling the function “gov-relations” but your function name is “exec_com” so the first line should be:
        add_shortcode( 'gov-relations', 'exec_com' );
        That should solve your error. Then if you are able to address the hyphens, that would be improve things.

  • Author
    Posts
Viewing 17 reply threads
  • You must be logged in to reply to this topic.
Log In

Ready to get started?

Join Today!

© 2021 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Refund Policy