Created
November 6, 2020 22:18
-
-
Save ronalfy/f4ef8109668f24a94f65b7132af87e68 to your computer and use it in GitHub Desktop.
PMPro - Add Start Date Membership Account Table
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 | |
/** | |
* 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