Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active April 12, 2021 22:39
Show Gist options
  • Select an option

  • Save strangerstudios/7ae2a474c8e3258a54568185297ab81c to your computer and use it in GitHub Desktop.

Select an option

Save strangerstudios/7ae2a474c8e3258a54568185297ab81c to your computer and use it in GitHub Desktop.
Show the "Favorite" button ONLY to logged in members with post access when using the Favorites Plugin for WordPress
<?php
/*
This recipe restricts display of the "Favorite" button for
the Favorites Plugin by Kyle Phillips (https://wordpress.org/plugins/favorites/).
Be sure to turn off the automatic display of the button under Settings > Favorites > Display.
The button will be shown to logged in members only on content they have access to view.
*/
function my_pmpro_members_only_favorites( $content ) {
//Don't show the favorites button on PMPro pages
$excluded_pages = array(pmpro_getOption('billing_page_id'), pmpro_getOption('account_page_id'), pmpro_getOption('levels_page_id'), pmpro_getOption('checkout_page_id'), pmpro_getOption('confirmation_page_id'));
//Show the favorites button for members only when they have access to the post
if( function_exists( 'pmpro_has_membership_access' ) &&
pmpro_has_membership_access( ) &&
is_user_logged_in() &&
//'post' === get_post_type( ) && //add this line to restrict to 'posts' only
!is_page( $excluded_pages ) ) {
if( function_exists( 'get_favorites_button' ) ) {
$content .= get_favorites_button();
}
}
return $content;
}
add_filter( 'the_content', 'my_pmpro_members_only_favorites' );
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Allow Members to Favorite Posts using the Favorites Plugin for WordPress" at Paid Memberships Pro here: https://www.paidmembershipspro.com/allow-members-favorite-posts-using-favorites-plugin-wordpress/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment