I have a PHP class to manage the membership details, I have a View to display the membership details, I can get all members' custom fields into the View, but there is error if I call the PHP class in a shortcode. I need this class to calculate the status of the membership. Please advise it is true I can use PHP class in shortcode? In my class, I did use wp_reset_postdata() to reset the current $post variable.
Below please find the code of my class, shortcode and view.
PHP class
class WPMembership {
public function __construct($user_id) {
global $post;
$args = array(
'numberposts' => 1,
'post_type' => 'member',
'p' => $user_id
);
$posts = get_posts($args);
$post = $posts[0];
setup_postdata( $post );
$this->termination_date = types_render_field('termination-date', array('output' => 'raw'));
..... (omitted)
wp_reset_postdata();
}
public function membershipStatus() {
$todayVal = strtotime(date('Y-m-d'));
if (($this->termination_date > 0) && ($this->termination_date < $todayVal)) {
return 'invalid';
}
if ($this->membershipEndDateVal >= $todayVal) {
return 'effective';
} else {
return 'finished';
}
}
Function to be called from View as shortcode
function ebz_get_membership_status( $atts ) {
$n = new WPMembership($atts['pid']);
$rtn = strtolower($n->membershipStatus());
return $rtn;
}
add_shortcode('get_membership_status', 'ebz_get_membership_status');
[13-Dec-2022 14:56:15 UTC] PHP Warning: strtotime() expects parameter 2 to be int, string given in /home/u249343135/domains/alivefitness.hk/public_html/staging/wp-content/plugins/uprisepay-payment/includes/class-wp-membership.php on line 59
[13-Dec-2022 14:56:15 UTC] PHP Warning: strtotime() expects parameter 2 to be int, string given in /home/u249343135/domains/alivefitness.hk/public_html/staging/wp-content/plugins/uprisepay-payment/includes/class-wp-membership.php on line 66
It is related to the custom date field, there is a custom field uprise-first-bill-date and uprise-next-bill-date with date format but without default value, I need to calculate the uprise-next-bill-date according to the uprise-first-bill-date but if customer has not subscribed my service, the uprise-first-bill-date is empty because not yet assigned the date to this field, below is the code I calculate the uprise-next-bill-date, some lines are omitted.
Well - errors you shared is actually notices and that does not effect the output.
If any of your field may have empty value, what if you wrap that code with if condition and make sure that it should run only when those date fields have values.
This is a pure custom code which is beyond the scope of our support policy. If you have something not working as expected with Toolset API or function please let us know and we are happy to help you further.
Yes I know but I need to know that what is the return if the date field has not value, is it null, '' or others then I can manage. What is the data type to this empty date field or I should default it as 1970-01-01 to avoid any error?