Last active
January 5, 2024 13:40
-
-
Save nikitasinelnikov/75ec8bba8b70ad320b7c4887fc5a8d7d to your computer and use it in GitHub Desktop.
Ultimate Member >= 2.2.1. Change the WP_Post object for displaying the restricted post in archive or single page
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
/** | |
* Customize WP_Post object for the restricted post (if it's not hidden) in the archive page | |
* | |
* @param WP_Post $post | |
* @param array $restriction_settings | |
* @param WP_Post $original_post | |
* | |
* @return WP_Post | |
*/ | |
function um_custom_data_restricted_archive_post( $post, $restriction_settings, $original_post ) { | |
// if you want to show the original post title instead of the restriction content title | |
// use these object keys https://developer.wordpress.org/reference/functions/get_post/#comment-876 | |
$post->post_title = $original_post->post_title; | |
// sometimes $original_post is already customized then please use this way | |
$p = get_post( $original_post->ID ); | |
$post->post_title = $p->post_title; | |
// please use this line if you need to restore the post excerpt | |
$post->post_excerpt = $p->post_excerpt; | |
return $post; | |
} | |
add_filter( 'um_restricted_archive_post', 'um_custom_data_restricted_archive_post', 10, 3 ); | |
/** | |
* Customize WP_Post object for the restricted post (if it's not hidden) in the singular post's page | |
* | |
* @param WP_Post $post | |
* @param array $restriction_settings | |
* @param WP_Post $original_post | |
* | |
* @return WP_Post | |
*/ | |
function um_custom_data_restricted_singular_post( $post, $restriction_settings, $original_post ) { | |
// if you want to show the original post title instead of the restriction content title | |
// use these object keys https://developer.wordpress.org/reference/functions/get_post/#comment-876 | |
$post->post_title = $original_post->post_title; | |
// sometimes $original_post is already customized then please use this way | |
$p = get_post( $original_post->ID ); | |
$post->post_title = $p->post_title; | |
// please use this line if you need to restore the post excerpt | |
$post->post_excerpt = $p->post_excerpt; | |
return $post; | |
} | |
add_filter( 'um_restricted_singular_post', 'um_custom_data_restricted_singular_post', 10, 3 ); | |
// avoid changing the post title to the "restricted content title" | |
add_filter( 'um_ignore_restricted_title', '__return_true' ); |
This gist is outdated. Please use this Gist for recent Ultimate Member versions
https://gist.github.com/nikitasinelnikov/6b56b639f8b40104180b484e4a07bd66
The new version of hook docs is here:
https://ultimatemember.github.io/ultimatemember/hooks/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems not working these code in UM 2.2.2.
um_restricted_archive_post
filter seems not work.um_ignore_restricted_title
filter hook not exist in UM codebase.