I am using Social Links and Memberships extensions. I am offering social links as a premium paid feature for people who pay for a listing, therefore I don’t want them to appear on free memberships. Can you please help me find a way to do this? They are already excluded in the backend of Memberships under my ‘free plan’.
I understood your request slightly differently to @ihor, and thought you wanted to hide social links for members without X Membership plan.
The code may need slight tweaks, but here’s HivePress AI’s version of that in case yourself or others in the community find it useful:
1. Hide Social Links Display (Listings & Profiles)
add_filter(
'hivepress/v1/templates/listing_view_page/blocks',
function ( $blocks, $template ) {
$listing = $template->get_context( 'listing' );
if ( ! $listing ) {
return $blocks;
}
// Check if user has a membership
$membership = hivepress()->request->get_context( 'membership' );
// Hide social links if user has no membership or has a free plan
// Replace 123 with your paid membership plan ID
if ( ! $membership || 123 !== $membership->get_plan__id() ) {
hivepress()->template->fetch_block( $blocks, 'listing_social_links' );
}
return $blocks;
},
1000,
2
);
// Hide social links on vendor/user profile pages
add_filter(
'hivepress/v1/templates/vendor_view_page/blocks',
function ( $blocks, $template ) {
$vendor = $template->get_context( 'vendor' );
if ( ! $vendor ) {
return $blocks;
}
// Check if user has a membership
$membership = hivepress()->request->get_context( 'membership' );
// Hide social links if user has no membership or has a free plan
// Replace 123 with your paid membership plan ID
if ( ! $membership || 123 !== $membership->get_plan__id() ) {
hivepress()->template->fetch_block( $blocks, 'vendor_social_links' );
}
return $blocks;
},
1000,
2
);
2. Hide Social Links Input Fields (Listings & Profiles)
// Hide social links in listing form for non-members
add_filter(
'hivepress/v1/forms/listing_update',
function( $form ) {
if ( ! hivepress()->get_version( 'memberships' ) ) {
return $form;
}
$membership = \HivePress\Models\Membership::query()->filter(
[ 'user' => get_current_user_id() ]
)->get_first();
// Hide social links fields if user has no membership or has a free plan
// Replace 123 with your paid membership plan ID
if ( ! $membership || 123 !== $membership->get_plan__id() ) {
if ( isset( $form['fields']['social_links'] ) ) {
unset( $form['fields']['social_links'] );
}
}
return $form;
},
1000
);
// Hide social links in vendor profile form for non-members
add_filter(
'hivepress/v1/forms/vendor_update',
function( $form ) {
if ( ! hivepress()->get_version( 'memberships' ) ) {
return $form;
}
$membership = \HivePress\Models\Membership::query()->filter(
[ 'user' => get_current_user_id() ]
)->get_first();
// Hide social links fields if user has no membership or has a free plan
// Replace 123 with your paid membership plan ID
if ( ! $membership || 123 !== $membership->get_plan__id() ) {
if ( isset( $form['fields']['social_links'] ) ) {
unset( $form['fields']['social_links'] );
}
}
return $form;
},
1000
);
Thank you both for your responses. I used ChrisB’s code, but unfortunately, it only removes the social links for every user, regardless of whether they are premium or not. I replaced the membership ID with the one from my Membership > Plans > Premium URL. Thoughts?
I rely on AI for most of my coding needs, but glad to hear I interpreted your request correctly. I tried using an example provided by Ihor in another topic and asked AI to help correct the one’s that I’d previously shared:
add_filter(
'hivepress/v1/templates/listing_view_page/blocks',
function ( $blocks, $template ) {
$listing = $template->get_context( 'listing' );
if ( ! $listing ) {
return $blocks;
}
// Get user ID of the listing author.
$user_id = (int) get_post_field( 'post_author', $listing->get_id() );
// Allow admins/editors to bypass the check.
if ( user_can( $user_id, 'edit_others_posts' ) ) {
return $blocks;
}
// Get membership.
$membership = \HivePress\Models\Membership::query()->filter(
[
'status' => 'publish',
'user' => $user_id,
]
)->get_first();
// Hide social links if listing author has no membership or has a free plan.
// Replace 123 with your paid membership plan ID.
if ( ! $membership || 123 !== $membership->get_plan__id() ) {
hivepress()->template->fetch_block( $blocks, 'listing_social_links' );
}
return $blocks;
},
1000,
2
);
// Hide social links on vendor/user profile pages
add_filter(
'hivepress/v1/templates/vendor_view_page/blocks',
function ( $blocks, $template ) {
$vendor = $template->get_context( 'vendor' );
if ( ! $vendor ) {
return $blocks;
}
// Get user ID of the vendor.
$user_id = (int) get_post_field( 'post_author', $vendor->get_id() );
// Allow admins/editors to bypass the check.
if ( user_can( $user_id, 'edit_others_posts' ) ) {
return $blocks;
}
// Get membership.
$membership = \HivePress\Models\Membership::query()->filter(
[
'status' => 'publish',
'user' => $user_id,
]
)->get_first();
// Hide social links if vendor has no membership or has a free plan.
// Replace 123 with your paid membership plan ID.
if ( ! $membership || 123 !== $membership->get_plan__id() ) {
hivepress()->template->fetch_block( $blocks, 'vendor_social_links' );
}
return $blocks;
},
1000,
2
);
// Hide social links in listing form for non-members
add_filter(
'hivepress/v1/forms/listing_update',
function( $form ) {
if ( ! hivepress()->get_version( 'memberships' ) ) {
return $form;
}
$user_id = get_current_user_id();
// Allow admins/editors to bypass the check.
if ( user_can( $user_id, 'edit_others_posts' ) ) {
return $form;
}
// Get membership.
$membership = \HivePress\Models\Membership::query()->filter(
[
'status' => 'publish',
'user' => $user_id,
]
)->get_first();
// Hide social links fields if user has no membership or has a free plan.
// Replace 123 with your paid membership plan ID.
if ( ! $membership || 123 !== $membership->get_plan__id() ) {
if ( isset( $form['fields']['social_links'] ) ) {
unset( $form['fields']['social_links'] );
}
}
return $form;
},
1000
);
// Hide social links in vendor profile form for non-members
add_filter(
'hivepress/v1/forms/vendor_update',
function( $form ) {
if ( ! hivepress()->get_version( 'memberships' ) ) {
return $form;
}
$user_id = get_current_user_id();
// Allow admins/editors to bypass the check.
if ( user_can( $user_id, 'edit_others_posts' ) ) {
return $form;
}
// Get membership.
$membership = \HivePress\Models\Membership::query()->filter(
[
'status' => 'publish',
'user' => $user_id,
]
)->get_first();
// Hide social links fields if user has no membership or has a free plan.
// Replace 123 with your paid membership plan ID.
if ( ! $membership || 123 !== $membership->get_plan__id() ) {
if ( isset( $form['fields']['social_links'] ) ) {
unset( $form['fields']['social_links'] );
}
}
return $form;
},
1000
);
Hopefully, it gets it right this time. Else, the staff will likely be able to correct this for you.
Please replace 1,2,3 with an ID or comma-separated IDs of plans that should allow displaying social links on the listing page. You can copy the plan ID when you edit the plan in Membership/Plans, it’s available in the browser address bar, something like “…post=123…”.
This snippet hides social links in the listing page sidebar, if the listing author (vendor) doesn’t have a membership plan with a specific ID. Please note that it only hides social links on the listig page, the fields for entering these links will still appear in the listing form, but the links will not be visible without an active membership.