Skip to content

Instantly share code, notes, and snippets.

@nikitasinelnikov
Created January 5, 2024 13:36
Show Gist options
  • Save nikitasinelnikov/6b56b639f8b40104180b484e4a07bd66 to your computer and use it in GitHub Desktop.
Save nikitasinelnikov/6b56b639f8b40104180b484e4a07bd66 to your computer and use it in GitHub Desktop.
Ultimate Member: Change the WP_Post object (title, content, excerpt) for displaying the restricted post. Ignore or use your custom
// It works since the 2.2.3:
// Avoid changing the post title to the "restricted content title".
add_filter( 'um_ignore_restricted_title', '__return_true' );
// Avoid changing the post content to the "restricted content content" not recommended to use or use with caution!
add_filter( 'um_ignore_restricted_content', '__return_true' );
// Avoid flushing the post excerpt when post is restricted.
add_filter( 'um_ignore_restricted_excerpt', '__return_true' );
// It works since the 2.8.2:
/**
* 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, $original_post, $query ) {
// 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_access_restricted_post_instance', 'um_custom_data_restricted_singular_post', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment