Created
February 21, 2022 18:40
-
-
Save kerijacoby/e2e9b0cab218a9abdd3650cdea6909b9 to your computer and use it in GitHub Desktop.
Filter the featured image and include an overlay if the post is protected. Exclude specific categories.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Filter the featured image and include an overlay if the post is protected. | |
* Use this with the recipe here: https://gist.github.com/kimcoleman/299bb458310e00d7282c090110c6b4f0 | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* | |
*/ | |
/** | |
* Filter the featured image and include an overlay if the post is protected. | |
* | |
*/ | |
function protected_post_custom_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) { | |
global $pmpro_pages; | |
// Return early if the function we are checking doesn't exist. | |
if ( ! function_exists( 'pmpro_has_membership_access' ) ) { | |
return $html; | |
} | |
// If the user doesn't have access, add the overlay. | |
if ( function_exists( 'pmpro_has_membership_access' ) ) { | |
if ( pmpro_has_membership_access() ) { | |
return $html; | |
} else { | |
// Decide which category or categories the blur featured image overlay should NOT be applied to. | |
$cats_to_show = array( 2, 3 ); | |
// Pluck the term IDs out of the post categories. | |
$cats = wp_list_pluck( get_the_category( $post_id ), 'term_id'); | |
// Stop if post is categorized in cats_to_show. | |
if ( array_intersect( $cats_to_show, $cats ) ) { | |
return $html; | |
} | |
// If you're on a single post, the overlay includes a button. | |
// Update this line to change the link or set to another URL. | |
if ( ! empty( $pmpro_pages['levels'] ) ) { | |
$levels_page_link = get_permalink( $pmpro_pages['levels'] ); | |
} | |
// Build the custom overlay. | |
$new_html = '<div class="pmpro_protected_post_featured_image">'; | |
$new_html .= $html; | |
$new_html .= '<div class="pmpro_protected_post_blur_mask">'; | |
$new_html .= '<p><i class="dashicons dashicons-lock"></i><br />Unlock this post by becoming a member.</p>'; | |
if ( is_single() && ! empty( $levels_page_link ) ) { | |
$new_html .= '<p><a class="pmpro_protected_post_button" href="' . esc_url( $levels_page_link ) . '">Join Now</a></p>'; | |
} | |
$new_html .= '</div> <!-- end pmpro_protected_post_blur_mask -->'; | |
$new_html .= '</div> <!-- end pmpro_protected_post_featured_image -->'; | |
$html = $new_html; | |
} | |
} | |
return $html; | |
} | |
add_filter( 'post_thumbnail_html', 'protected_post_custom_post_thumbnail_html', 10, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment