Skip to content

Instantly share code, notes, and snippets.

@nikitasinelnikov
Last active January 5, 2024 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikitasinelnikov/75ec8bba8b70ad320b7c4887fc5a8d7d to your computer and use it in GitHub Desktop.
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
/**
* 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' );
@sjoonk
Copy link

sjoonk commented Aug 15, 2021

It seems not working these code in UM 2.2.2.

  1. um_restricted_archive_post filter seems not work.
  2. um_ignore_restricted_title filter hook not exist in UM codebase.

@nikitasinelnikov
Copy link
Author

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