My site shows custom posts made by subscribers (role Subscriber). Now if the subscriber buys a subscription (WooCommerce Subscription) his role is changed to Customer and he gets some extra fields for the custom posts. Another benefit to change role is that a Subscriber can write only 1 custom post but a Customer can write 3 posts.
Site visitor can see the posts of the subscribers and also extra fields are visible, IF the post author is a Customer.
To build this I have some custom coding, which I’ve googled – since I don’t do PHP – and for some I got help here, thank you! Now I need help again 🙂
My problem is now, that when the Subscriber is changed to Customer and he can write the second post and the second post also has the extra fields, these extra fields are not visible for visitors (the fields are visible in editing form though). Same thing with third post. Only the first post’s extra fields are visible for visitors.
So, here’s how everything is done.
1. The role change is done with WooCommerce Subscriptions and it seems to work ok.
2. The new posts are done with Toolset Forms (cred), extra fields are shown with Toolset conditions, seems to work ok.
3. Restricting of post amount by role is done with this code (seems to work ok):
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$field_data;
//validate if specific form
if ($form_data['id']==559)
{
$user = get_current_user_id();
$user_post_count = count_user_posts( $user , 'muusikko' );
$user_data = get_userdata($user);
$user_role = $user_data->roles;
if ( $user_role[0] == 'subscriber' && $user_post_count > 0)
{
//set error message for my_field
$errors['wpcf-user-validation’]=‘Only one profile’;
}
if ( $user_role[0] == 'customer' && $user_post_count > 2)
{
//set error message for my_field
$errors['wpcf-user-validation’]=‘Only three profile.’;
}
}
//return result
return array($fields,$errors);
}
4. The shortcode to check post autho’r role is done with this code, perhaps this is the devil here:
function get_author_role()
{
global $authordata;
$author_roles = $authordata->roles;
$author_role = array_shift($author_roles);
return $author_role;
}
add_shortcode( 'user_role_func', 'get_author_role' );
and in content template the shortcode is used like this:
[wpv-conditional if="( '[user_role_func]' eq 'customer' )"]
here goes the extra fields
[/wpv-conditional]
Do you find the failure, why is only the first post’s extra fields shown in frontend not the second’s and third’s? Their author is after all the same Customer role author than the first one’s.