I’m struggling with the Render Element filter, trying to get it to hide a section of a post unless the post is being vied by the author. My PHP is trash and I’m struggling. I’m using Code Snippets, and the following (finally) isn’t causing a fatal error, but it’s not working either.
add_filter( ‘bricks/element/render’, function( $render, $element ) {
// Render element ID “kgmatw” if user is logged in
$current_user_id = get_current_user_id();
$post_author_id = get_the_author_meta(‘ID’);
// Render elements on single posts having a class of "post-author-only" only if the current user is the author of that post.
add_filter( 'bricks/element/render', function( $render, $element ) {
// ensure that we are on a single post
if ( ! is_single() ) {
return $render;
}
// get the element CSS classes
$classes = ! empty( $element->attributes['_root']['class'] ) ? $element->attributes['_root']['class'] : false;
// check if the element has the special class "post-author-only"
if ( $classes && in_array( 'post-author-only', $classes ) ) {
return get_the_author_meta( 'ID' ) === get_current_user_id();
}
return $render;
}, 10, 2 );
With that in place, elements having a class of post-author-only in the single post template will be output only when viewed by the post author.