Skip to content

Instantly share code, notes, and snippets.

@lunule
Forked from saltnpixels/membership_meta.php
Created September 6, 2022 19:47
Show Gist options
  • Save lunule/12306eca2b51b2a775249f61daa024bd to your computer and use it in GitHub Desktop.
Save lunule/12306eca2b51b2a775249f61daa024bd to your computer and use it in GitHub Desktop.
adding membership meta to rcp from ACF
<?php
/*--------------------------------------------------------------
# Saving Features to Membership Levels from an acf repeater field for RCP Pro
--------------------------------------------------------------*/
function some_feature_list( $level ) {
/**
* @var RCP_Levels $rcp_levels_db
*/
global $rcp_levels_db;
$value = '';
?>
<tr class="form-field">
<th><h2 style="margin: 0;"><?php _e( 'SOME FEATURES', 'some' ); ?></h2></th>
</tr>
<?php
//foreach loop would go here for all features. check if level exists, get value and put it into input.
//foreach($features as $feature)...
if ( have_rows( 'features', 'options' ) ) {
while ( have_Rows( 'features', 'options' ) ) {
the_row();
$feature_sanitized = sanitize_title(get_sub_field( 'feature' ));
//check if this is a new level or editing one made
if ( $level ) {
$value = rcp_get_membership_level_meta( $level->id, $feature_sanitized, true );
} else {
$value = '';
}
?>
<tr>
<th scope="row" valign="top">
<input id="<?php echo $feature_sanitized; ?>" type="checkbox" name="<?php echo $feature_sanitized; ?>"
style="transform: scale(1.5);"
<?php if ( $value ) {
echo 'checked';
} ?>
value="1">
</th>
<td>
<label for="<?php echo $feature_sanitized; ?>"><strong><?php echo get_sub_field( 'feature' ); ?></strong></label>
<p class="description"><?php echo get_sub_field('description'); ?></p>
</td>
</tr>
<?php
}
}
?>
<?php
wp_nonce_field( 'some_features', 'some_features' );
}
add_action( 'rcp_add_subscription_form', 'some_feature_list' );
add_action( 'rcp_edit_subscription_form', 'some_feature_list' );
/**
* Saves the subscription level limit settings.
*
* @param int $level_id ID of the subscription level.
* @param array $args Data being added or updated.
*
* @return void
*/
function some_feature_list_save( $level_id = 0, $args = array() ) {
/**
* @var RCP_Levels $rcp_levels_db
*/
global $rcp_levels_db;
//check nonce
if ( empty( $_POST['some_features'] ) || ! wp_verify_nonce( $_POST['some_features'], 'some_features' ) ) {
return;
}
//foreach loop of features
if ( have_rows( 'features', 'options' ) ) {
while ( have_Rows( 'features', 'options' ) ) {
the_row();
$feature_sanitized = sanitize_title(get_sub_field( 'feature' ));
if ( isset( $_POST[$feature_sanitized] ) ) {
rcp_update_membership_level_meta( $level_id, $feature_sanitized, $_POST[$feature_sanitized] );
} else {
rcp_delete_membership_level_meta( $level_id, $feature_sanitized);
}
}
}
return;
}
add_action( 'rcp_add_subscription', 'some_feature_list_save', 10, 2 );
add_action( 'rcp_edit_subscription_level', 'some_feature_list_save', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment