Forked from ipokkel/ninja_forms_field_membership_level.php
Last active
April 4, 2021 03:55
-
-
Save kimcoleman/5b949a1db5cb2add8dc9974076998b75 to your computer and use it in GitHub Desktop.
Hidden Ninja Forms form field that captures PMPro membership level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Add a hidden field in Ninja Forms to capture Membership Level (if user is logged in). | |
* | |
* For Ninja Forms version 3 and higher. | |
*/ | |
// Create Membership Level field for Ninja Forms | |
add_filter( 'ninja_forms_register_fields', function( $fields ) { | |
if ( class_exists( 'PMProMembershipLevelNFInput' ) ) { | |
$fields['pmpromembershiplevel'] = new PMProMembershipLevelNFInput; | |
} | |
return $fields; | |
} ); | |
// Add class for input field. | |
function my_pmpro_add_membership_level_class_for_nf() { | |
if ( ! class_exists( 'NF_Abstracts_Input' ) ) { | |
return; | |
} | |
class PMProMembershipLevelNFInput extends NF_Abstracts_Input { | |
protected $_name = 'pmpromembershiplevel'; | |
protected $_nicename = 'Membership Level'; | |
protected $_section = 'userinfo'; | |
protected $_icon = 'eye-slash'; | |
protected $_type = 'hidden'; | |
protected $_templates = 'hidden'; | |
protected $_wrap_template = 'wrap-no-label'; | |
protected $_settings_only = array( | |
'key', 'label', 'admin_label' | |
); | |
public function __construct() { | |
parent::__construct(); | |
$this->_nicename = __( 'Membership Level', 'ninja-forms' ); | |
$this->_settings[ 'label' ][ 'width' ] = 'full'; | |
} | |
} | |
} | |
add_action( 'plugins_loaded', 'my_pmpro_add_membership_level_class_for_nf', 1 ); | |
// Set value for pmpromembershiplevel field | |
function nf_default_value_membership_level( $default_value, $field_type, $field_settings ) { | |
global $current_user; | |
// Get the current user's membership level object. | |
if ( function_exists( 'pmpro_getMembershipLevelForUser' ) ) { | |
$level = pmpro_getMembershipLevelForUser( $current_user->ID ); | |
} else { | |
$level = false; | |
} | |
// Get the current user's membership level name. | |
if ( ! empty( $level ) ) { | |
$current_membership_level_name = $level->name; | |
} else { | |
$current_membership_level_name = false; | |
} | |
if ( 'pmpromembershiplevel' == $field_type && in_array( 'pmpromembershiplevel', $field_settings ) && ! empty( $current_membership_level_name ) ) { | |
$default_value = $current_membership_level_name; | |
} | |
return $default_value; | |
} | |
add_filter( 'ninja_forms_render_default_value', 'nf_default_value_membership_level' , 10 , 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Ninja Forms: Add Hidden Field for Membership Level" at Paid Memberships Pro here: https://www.paidmembershipspro.com/ninja-forms-add-hidden-field-for-membership-level/