Last active
January 25, 2022 19:23
-
-
Save kimcoleman/faf2cd0a83f3ca7a5c6c368e371ad1f3 to your computer and use it in GitHub Desktop.
Filter the featured image and include an overlay if the post is protected.
This file contains hidden or 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 overla. | |
if ( function_exists( 'pmpro_has_membership_access' ) ) { | |
if ( pmpro_has_membership_access() ) { | |
return $html; | |
} else { | |
// 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