Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active February 1, 2021 18:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/cddc1714097373503ddc4b64a8ff15c3 to your computer and use it in GitHub Desktop.
Save ipokkel/cddc1714097373503ddc4b64a8ff15c3 to your computer and use it in GitHub Desktop.
Custom shortcode that applies wpautop to the user bio to automatically create paragraphs from double line breaks.
<?php
/**
* This recipe replaces the function for the shortcode [pmpro_member_profile]
* with a custom shortcode function that applies wpautop to the user bio,
* which creates paragraphs from double line breaks, and nl2br to other fields,
* which converts line breaks to html line break <br />.
*
* @link https://developer.wordpress.org/reference/functions/wpautop/
* @link https://www.php.net/manual/en/function.nl2br
*
* This recipe is compatible with PMPro Member Directory
* @version 1.1
* @link https://www.paidmembershipspro.com/add-ons/member-directory/
*
* It is important to regularly check for any changes to the profile shortcode
* that this recipe is derived from and apply any updates to this recipe where necessary.
*
* @link https://github.com/strangerstudios/pmpro-member-directory/blob/dev/templates/profile.php
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpromd_custom_shortcode_init() {
// Only do this on the front-end
if ( is_admin() ) {
return;
}
// Check that Member Directory is active
if ( ! defined( 'PMPRO_MEMBER_DIRECTORY_VERSION' ) ) {
return;
}
// Remove current shortcode
if ( shortcode_exists( 'pmpro_member_profile' ) ) {
remove_shortcode( 'pmpro_member_profile' );
}
// Add custom shortcode as replacement
add_shortcode( 'pmpro_member_profile', 'my_pmpromd_profile_shortcode' );
}
add_action( 'init', 'my_pmpromd_custom_shortcode_init' );
function my_pmpromd_profile_shortcode( $atts, $content = null, $code = '' ) {
// $atts ::= array of attributes
// $content ::= text within enclosing form of shortcode element
// $code ::= the shortcode found, when == callback name
// examples: [pmpro_member_profile avatar="false" email="false"]
extract(
shortcode_atts(
array(
'avatar_size' => '128',
'fields' => null,
'show_avatar' => true,
'show_bio' => true,
'show_billing' => true,
'show_email' => true,
'show_level' => true,
'show_name' => true,
'show_phone' => true,
'show_search' => true,
'show_startdate' => true,
'user_id' => null,
),
$atts
)
);
global $current_user, $display_name, $wpdb, $pmpro_pages, $pmprorh_registration_fields;
//some page vars
if ( ! empty( $pmpro_pages['directory'] ) ) {
$directory_url = get_permalink( $pmpro_pages['directory'] );
} else {
$directory_url = '';
}
if ( ! empty( $pmpro_pages['profile'] ) ) {
$profile_url = get_permalink( $pmpro_pages['profile'] );
}
//turn 0's into falses
if ( $show_avatar === '0' || $show_avatar === 'false' || $show_avatar === 'no' || $show_avatar === false ) {
$show_avatar = false;
} else {
$show_avatar = true;
}
if ( $show_billing === '0' || $show_billing === 'false' || $show_billing === 'no' || $show_billing === false ) {
$show_billing = false;
} else {
$show_billing = true;
}
if ( $show_bio === '0' || $show_bio === 'false' || $show_bio === 'no' || $show_bio === false ) {
$show_bio = false;
} else {
$show_bio = true;
}
if ( $show_email === '0' || $show_email === 'false' || $show_email === 'no' || $show_email === false ) {
$show_email = false;
} else {
$show_email = true;
}
if ( $show_level === '0' || $show_level === 'false' || $show_level === 'no' || $show_level === false ) {
$show_level = false;
} else {
$show_level = true;
}
if ( $show_name === '0' || $show_name === 'false' || $show_name === 'no' || $show_name === false ) {
$show_name = false;
} else {
$show_name = true;
}
if ( $show_phone === '0' || $show_phone === 'false' || $show_phone === 'no' || $show_phone === false ) {
$show_phone = false;
} else {
$show_phone = true;
}
if ( $show_search === '0' || $show_search === 'false' || $show_search === 'no' || $show_search === false ) {
$show_search = false;
} else {
$show_search = true;
}
if ( $show_startdate === '0' || $show_startdate === 'false' || $show_startdate === 'no' || $show_startdate === false ) {
$show_startdate = false;
} else {
$show_startdate = true;
}
if ( isset( $_REQUEST['limit'] ) ) {
$limit = intval( $_REQUEST['limit'] );
} elseif ( empty( $limit ) ) {
$limit = 15;
}
if ( empty( $user_id ) && ! empty( $_REQUEST['pu'] ) ) {
//Get the profile user
if ( is_numeric( $_REQUEST['pu'] ) ) {
$pu = get_user_by( 'id', $_REQUEST['pu'] );
} else {
$pu = get_user_by( 'slug', $_REQUEST['pu'] );
}
$user_id = $pu->ID;
}
if ( ! empty( $user_id ) ) {
$pu = get_userdata( $user_id );
} elseif ( empty( $_REQUEST['pu'] ) ) {
$pu = get_userdata( $current_user->ID );
}
if ( ! empty( $pu ) ) {
$pu->membership_level = pmpro_getMembershipLevelForUser( $pu->ID );
$allmylevels = pmpro_getMembershipLevelsForUser( $pu->ID );
$membership_levels = array();
foreach ( $allmylevels as $curlevel ) {
$membership_levels[] = $curlevel->name;
}
$pu->membership_levels = implode( ', ', $membership_levels );
}
ob_start();
?>
<?php if ( ! empty( $show_search ) ) { ?>
<form action="<?php echo $directory_url; ?>" method="post" role="search" class="pmpro_member_directory_search search-form">
<label>
<span class="screen-reader-text"><?php _e( 'Search for:', 'label' ); ?></span>
<input type="search" class="search-field" placeholder="<?php _e( 'Search Members', 'pmpromd' ); ?>" name="ps" value="
<?php
if ( ! empty( $_REQUEST['ps'] ) ) {
echo esc_attr( $_REQUEST['ps'] );}
?>
" title="<?php _e( 'Search Members', 'pmpromd' ); ?>" />
<input type="hidden" name="limit" value="<?php echo esc_attr( $limit ); ?>" />
</label>
<input type="submit" class="search-submit" value="<?php _e( 'Search Members', 'pmpromd' ); ?>">
</form>
<?php } ?>
<?php
if ( ! empty( $pu ) ) {
if ( ! empty( $fields ) ) {
// Check to see if the Block Editor is used or the shortcode.
if ( strpos( $fields, "\n" ) !== false ) {
$fields = rtrim( $fields, "\n" ); // clear up a stray \n
$fields_array = explode( "\n", $fields ); // For new block editor.
} else {
$fields = rtrim( $fields, ';' ); // clear up a stray ;
$fields_array = explode( ';', $fields );
}
if ( ! empty( $fields_array ) ) {
for ( $i = 0; $i < count( $fields_array ); $i++ ) {
$fields_array[ $i ] = explode( ',', $fields_array[ $i ] );
}
}
} else {
$fields_array = false;
}
// Get Register Helper field options
$rh_fields = array();
if ( ! empty( $pmprorh_registration_fields ) ) {
foreach ( $pmprorh_registration_fields as $location ) {
foreach ( $location as $field ) {
if ( ! empty( $field->options ) ) {
$rh_fields[ $field->name ] = $field->options;
}
}
}
}
?>
<div id="pmpro_member_profile-<?php echo $pu->ID; ?>" class="pmpro_member_profile">
<?php do_action( 'pmpro_member_profile_before', $pu ); ?>
<?php if ( ! empty( $show_avatar ) ) { ?>
<p class="pmpro_member_directory_avatar">
<?php echo get_avatar( $pu->ID, $avatar_size, null, $pu->display_name, array( 'class' => 'alignright' ) ); ?>
</p>
<?php } ?>
<?php if ( ! empty( $show_name ) && ! empty( $pu->display_name ) ) { ?>
<h2 class="pmpro_member_directory_name">
<?php echo esc_html( pmpro_member_directory_get_member_display_name( $pu ) ); ?>
</h2>
<?php } ?>
<?php if ( ! empty( $show_bio ) && ! empty( $pu->description ) ) { ?>
<p class="pmpro_member_directory_bio">
<strong><?php _e( 'Biographical Info', 'pmpromd' ); ?></strong>
<?php echo wpautop( $pu->description ); ?>
</p>
<?php } ?>
<?php if ( ! empty( $show_email ) ) { ?>
<p class="pmpro_member_directory_email">
<strong><?php _e( 'Email Address', 'pmpromd' ); ?></strong>
<?php echo $pu->user_email; ?>
</p>
<?php } ?>
<?php if ( ! empty( $show_level ) ) { ?>
<p class="pmpro_member_directory_level">
<strong><?php _e( 'Level', 'pmpromd' ); ?></strong>
<?php echo ! empty( $pu->membership_levels ) ? $pu->membership_levels : ''; ?>
</p>
<?php } ?>
<?php if ( ! empty( $show_startdate ) ) { ?>
<p class="pmpro_member_directory_date">
<strong><?php _e( 'Start Date', 'pmpromd' ); ?></strong>
<?php echo ! empty( $pu->membership_level ) ? date( get_option( 'date_format' ), $pu->membership_level->startdate ) : ''; ?>
</p>
<?php } ?>
<?php if ( ! empty( $show_billing ) && ! empty( $pu->pmpro_baddress1 ) ) { ?>
<p class="pmpro_member_directory_baddress">
<strong><?php _e( 'Address', 'pmpromd' ); ?></strong>
<?php echo $pu->pmpro_baddress1; ?><br />
<?php
if ( ! empty( $pu->pmpro_baddress2 ) ) {
echo $pu->pmpro_baddress2 . '<br />';
}
?>
<?php if ( $pu->pmpro_bcity && $pu->pmpro_bstate ) { ?>
<?php echo $pu->pmpro_bcity; ?>, <?php echo $pu->pmpro_bstate; ?> <?php echo $pu->pmpro_bzipcode; ?><br />
<?php echo $pu->pmpro_bcountry; ?><br />
<?php } ?>
</p>
<?php } ?>
<?php if ( ! empty( $show_phone ) && ! empty( $pu->pmpro_bphone ) ) { ?>
<p class="pmpro_member_directory_phone">
<strong><?php _e( 'Phone Number', 'pmpromd' ); ?></strong>
<?php echo formatPhone( $pu->pmpro_bphone ); ?>
</p>
<?php } ?>
<?php
//filter the fields
$fields_array = apply_filters( 'pmpro_member_profile_fields', $fields_array, $pu );
if ( ! empty( $fields_array ) ) {
foreach ( $fields_array as $field ) {
if ( empty( $field[0] ) ) {
break;
}
// Fix for a trailing space in the 'fields' shortcode attribute.
if ( $field[0] === ' ' ) {
break;
}
$field_val = $field[1];
$meta_field = $pu->$field_val;
if ( ! empty( $meta_field ) ) {
?>
<p class="pmpro_member_directory_<?php echo esc_attr( $field[1] ); ?>">
<?php
if ( is_array( $meta_field ) && ! empty( $meta_field['filename'] ) ) {
//this is a file field
?>
<strong><?php echo $field[0]; ?></strong>
<?php echo pmpromd_display_file_field( $meta_field ); ?>
<?php
} elseif ( is_array( $meta_field ) ) {
//this is a general array, check for Register Helper options first
if ( ! empty( $rh_fields[ $field[1] ] ) ) {
foreach ( $meta_field as $key => $value ) {
$meta_field[ $key ] = $rh_fields[ $field[1] ][ $value ];
}
}
?>
<strong><?php echo $field[0]; ?></strong>
<?php echo implode( ', ', $meta_field ); ?>
<?php
} elseif ( ! empty( $rh_fields[ $field[1] ] ) && is_array( $rh_fields[ $field[1] ] ) ) {
?>
<strong><?php echo $field[0]; ?></strong>
<?php echo $rh_fields[ $field[1] ][ $meta_field ]; ?>
<?php
} else {
if ( $field[1] == 'user_url' ) {
?>
<a href="<?php echo esc_url( $meta_field ); ?>" target="_blank"><?php echo $field[0]; ?></a>
<?php
} else {
?>
<strong><?php echo $field[0]; ?></strong>
<?php
$meta_field_embed = wp_oembed_get( $meta_field );
if ( ! empty( $meta_field_embed ) ) {
echo $meta_field_embed;
} elseif ( preg_match( '/\R/', $meta_field ) && ! str_contains_a( $meta_field, array( '<br>', '<br />', '<p>' ) ) ) {
echo nl2br( $meta_field ); // Add <br /> tags at end of line
} else {
echo make_clickable( $meta_field );
}
?>
<?php
}
}
?>
</p>
<?php
}
}
}
?>
<?php do_action( 'pmpro_member_profile_after', $pu ); ?>
<div class="pmpro_clear"></div>
</div>
<hr />
<p class="pmpro_actions_nav">
<?php
// Build the links to return.
$pmpro_member_profile_action_links = array();
if ( ! empty( $directory_url ) ) {
$pmpro_member_profile_action_links['view-directory'] = sprintf( '<a id="pmpro_actionlink-view-all-members" href="%s">%s</a>', esc_url( $directory_url ), esc_html__( 'View All Members', 'pmpromd' ) );
}
if ( ! empty( $pu ) && $pu->ID === $current_user->ID ) {
// User viewing their own profile. Show an edit profile link if 'Member Profile Edit Page' is set or dashboard access is allowed.
if ( ! empty( pmpro_getOption( 'member_profile_edit_page_id' ) ) ) {
$edit_profile_url = pmpro_url( 'member_profile_edit' );
} elseif ( ! pmpro_block_dashboard() ) {
$edit_profile_url = admin_url( 'profile.php' );
}
if ( ! empty( $edit_profile_url ) ) {
$pmpro_member_profile_action_links['edit-profile'] = sprintf( '<a id="pmpro_actionlink-profile" href="%s">%s</a>', esc_url( $edit_profile_url ), esc_html__( 'Edit Profile', 'paid-memberships-pro' ) );
}
}
/**
* Filter which links are displayed on the single Member Directory Profile page.
*
* @since 1.0
*
* @param array $pmpro_member_profile_action_links Can be view-directory, edit-profile, or or custom.
*
* @return array $pmpro_member_profile_action_links
*/
$pmpro_member_profile_action_links = apply_filters( 'pmpromd_member_profile_action_links', $pmpro_member_profile_action_links );
$allowed_html = array(
'a' => array(
'class' => array(),
'href' => array(),
'id' => array(),
'target' => array(),
'title' => array(),
),
);
echo wp_kses( implode( pmpro_actions_nav_separator(), $pmpro_member_profile_action_links ), $allowed_html );
?>
</p> <!-- end pmpro_actions_nav -->
<?php
}
?>
<?php
$temp_content = ob_get_contents();
ob_end_clean();
return $temp_content;
}
/*
Polyfill for str_contains() function
https://php.watch/versions/8.0/str_contains
https://www.php.net/manual/en/function.str-contains
*/
if ( ! function_exists( 'str_contains' ) ) {
function str_contains( string $haystack, string $needle ): bool {
return '' === $needle || false !== strpos( $haystack, $needle );
}
}
if ( ! function_exists( 'str_contains_a' ) ) {
function str_contains_a( string $haystack, array $needles ) {
foreach ( $needles as $key => $needle ) {
if ( str_contains( $haystack, $needle ) ) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment