[Resolved] Disable element for specific author

Home Forums Support [Resolved] Disable element for specific author

Home Forums Support Disable element for specific author

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #1956363
    Chris

    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!!

    #1956374
    Leo
    Staff
    Customer Support

    Hi Chris,

    Which element are you trying to disable?

    Let me know 🙂

    #1956393
    Chris

    A custom one I created. A hook, a block-hook etc.

    #1956395
    Leo
    Staff
    Customer Support

    I believe this filter is what you are looking for:
    https://docs.generatepress.com/article/generate_element_display/

    Let me know 🙂

    #1956403
    Chris

    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.

    #1956409
    Leo
    Staff
    Customer Support

    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

    #1956412
    Chris

    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?

    #1956422
    Leo
    Staff
    Customer Support

    That’s correct.

    Can you share your full code here and link me to the page in question?

    #1956428
    Chris
    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.

    #1956435
    Leo
    Staff
    Customer Support

    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!

    #1956451
    Chris

    I tried it with multiple installs now. The snippet doesn’t work in any of them.

    #1956505
    Leo
    Staff
    Customer Support

    Maybe we are not using the right conditional tag.

    Can you create a staging site and provide the backend access?

    #1957481
    Chris

    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?

    #1957806
    David
    Staff
    Customer Support

    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 );
    #1958042
    Chris

    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.

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.