Archived topic
Disable element for specific author
19 replies · Started by Chris on October 7, 2021
Hi,
Unfortunately the display settings won't allow me to disable a custom element for a specific author. Could you please give me a workaround snippet that basically says:
If author is "ID number", disable custom element "ID number, ID number, ID, number"
Thanks!!
Hi Chris,
Which element are you trying to disable?
Let me know :)
A custom one I created. A hook, a block-hook etc.
I believe this filter is what you are looking for:
https://docs.generatepress.com/article/generate_element_display/
Let me know :)
So I would use the first one like this:
add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( 100 === $element_id && is_author( 'Tom' ) ) {
$display = false;
}
return $display;
}, 10, 2 );
Where I wouldn't want to display element ID 100 for every post with the author Tom?
Could we change it to author ID instead of name?
This doesn't work for me.
Where I wouldn’t want to display element ID 100 for every post with the author Tom?
That is correct - Element ID 100 won't display when the author is Tom.
Could we change it to author ID instead of name?
This should help: https://codex.wordpress.org/Conditional_Tags#An_Author_Page
Unfortunately this doesn't work for me.
Just to be sure, the element ID is what is displayed in the edit URL as "post=100", correct?
That's correct.
Can you share your full code here and link me to the page in question?
add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( 1351 === $element_id && is_author( '2' ) ) {
$display = false;
}
return $display;
}, 10, 2 );
I can't link the page, but it's the most recent version for plugin and theme.
It's not related to the theme/plugin versions.
Issues like these are tough to help without seeing the site live.
Can you create a staging site and provide the backend access?
Thanks!
I tried it with multiple installs now. The snippet doesn't work in any of them.
Maybe we are not using the right conditional tag.
Can you create a staging site and provide the backend access?
Not really, but I can show you a snippet that works just fine:
add_filter( 'option_generate_blog_settings', 'custom_author_page_settings' );
function custom_author_page_settings( $options ) {
$author = get_the_author_meta( 'ID' );
if ( 2 === $author ) {
$options['author'] = false;
$options['single_author'] = false;
}
return $options;
}
So I don't understand why the other one won't work. Did you test the code yourself? With the new Block/Hook Elements for author meta for example?
Hi there,
where is this element being displayed?
The is_author tag is used to check if you're on an author archive page.
Whereas your second working code is checking the author meta which will apply to a single post.
If you require the latter - try this:
add_filter( 'generate_element_display', function( $display, $element_id ) {
$author = get_post_field( 'post_author', get_the_ID() );
if ( 1351 === $element_id && '2' === $author ) {
$display = false;
}
return $display;
}, 10, 2 );
The element is displayed on a post. Author box or post meta for example, or a hook in wp_head etc.
I want to disable an element on posts by a specific author.
Unfortunately the snippet you provided does not work either.