Question

I am trying to restrict content within a Wordpress template file and am using a plugin called Paid Memberships Pro to do so.

The code below restricts content to members with 'levels' of 1 or 2.

if(pmPro_hasMembershipLevel(array(1,2))){
    restricted content goes here
}

The problem comes when I try to use a variable to provide the levels. These levels are held in a custom field group 'restrictions' with field name 'pmpro_id'. I access these levels within the template using...

foreach($restrictions as $restriction){
    $temp=get_field('pmpro_id', $restriction->ID );
    $temp_array[]=$temp;
}

$levels=implode(',', $temp_array);

If I then pass $levels to pmPro_hasMembershipLevel, this works fine if there is only one level but fails if there are 2 or more. I believe this is because the variable type is then a string rather than integer? I had previously tried to pass the $temp_array directly though I felt this wouldn't work and was correct.

I realise this is probably PHP 101. I have searched but don't really know what I'm looking for to be honest! I am not a developer and this is the last thing holding me back from finishing this project so ANY help anyone could provide would be brilliant. Thanks in advance.

Était-ce utile?

La solution

You don't need to implode $temp_array if pmPro_hasMembershipLevel accepts array as its argument. When you implode an array you get string as a return value — that's not what you want here. If you think that the issue might be with the type of values, then you can try to cast them to integers, like this $temp_array[]= (int) $temp;

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top