Skip to content

Instantly share code, notes, and snippets.

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 ronalfy/f4ef8109668f24a94f65b7132af87e68 to your computer and use it in GitHub Desktop.
Save ronalfy/f4ef8109668f24a94f65b7132af87e68 to your computer and use it in GitHub Desktop.
PMPro - Add Start Date Membership Account Table
<?php
/**
* Gets the start date for a user and outputs it to the account page membership table.
*
* 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_pmpro_add_extra_column_members_table() {
global $current_user;
// Ensure user is logged in.
if ( ! is_user_logged_in() ) {
return;
}
// Ensure we're on the account page.
if ( ! is_page( get_option( 'pmpro_account_page_id' ) ) ) {
return;
}
// Make sure PMPro exists.
if ( ! class_exists( 'MemberOrder' ) || ! function_exists( 'pmpro_getMembershipLevelForUser' ) ) {
return;
}
$level = pmpro_getMembershipLevelForUser( $current_user->ID );
if ( ! isset( $level->id ) ) {
return;
}
// Perform query to get start date.
global $wpdb;
$sqlQuery = "SELECT startdate FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . esc_sql( $current_user->ID ) . "' AND membership_id = '" . esc_sql( $level->id ) . "' AND status = 'active' ORDER BY id DESC LIMIT 1";
$start_date = strtotime( $wpdb->get_var( $sqlQuery ) );
$start_date = date_i18n( get_option( 'date_format' ), $start_date );
// Output jQuery to output the new table column and data.
if ( wp_script_is( 'jquery', 'done' ) ) {
?>
<script>
var $membership_level = jQuery( '#pmpro_account-membership' );
var $table = $membership_level.find( '.pmpro_table' );
$table.find( 'thead tr' ).append( '<th>Start Date</th>' );
$table.find( 'tbody tr' ).append( '<td class="pmpro_account-membership-start-date"><?php echo esc_js( $start_date ); ?></td>' );
</script>
<?php
}
?>
<?php
}
add_action( 'wp_footer', 'my_pmpro_add_extra_column_members_table' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment