• veri63

    (@veri63)


    By adding a query loop block to the page and displaying the post feed, password-protected posts display a thumbnail, while the regular post feed hides the thumbnail.
    How can I hide the thumbnail in the “request loop” block? Or, similarly to the standard option, display text stating that the entry is protected.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    To literally hide it, you could use some custom CSS, for example:

    .post-password-required img {
        display: none;
    }

    (works on my site, success could vary by theme)
    Someone with a bit of technical knowledge could easily unhide this image, its URL still appears in the page’s HTML source code. To fully remove it from the query loop so its URL cannot be discovered, you’d need a customized query loop block, or a custom shortcode that does the same thing.

    Hiding with CSS is a reasonable approach for most images. Someone would need to know something is hidden to start with to be inspired to try to unhide it. Be aware that it’s not 100% secure.

    DevinLabs Solutions

    (@devinlabsolutions)

    Hi @veri63,

    Defintaly you can do that via CSS, but I would suggest you to look into your theme setting as well once. if your issue still persist there.

    then you can prefer to use custom code for the loop. but for that you need some techincal knowlege of code.

    Thread Starter veri63

    (@veri63)

    @bcworkz @devinlabsolutions it doesn’t depend on the theme because it works a different layout, in the editor it’s a block called “post loop”.

    If I hide the thumbnail solely through styles, then no one will see the thumbnail, even by entering the entry password. so this is not the option I’m looking for.

    the code of this block itself is \wp-includes\blocks\post-featured-image.php

    and the code responsible for displaying this block in a loop looks like this:

    <?php
    /**
     * Server-side rendering of the core/post-featured-image block.
     *
     * @package WordPress
     */
    
    /**
     * Renders the core/post-featured-image block on the server.
     *
     * @param array    $attributes Block attributes.
     * @param string   $content    Block default content.
     * @param WP_Block $block      Block instance.
     * @return string Returns the featured image for the current post.
     */
    function render_block_core_post_featured_image( $attributes, $content, $block ) {
    	if ( ! isset( $block->context['postId'] ) ) {
    		return '';
    	}
    	$post_ID = $block->context['postId'];
    
    	$is_link        = isset( $attributes['isLink'] ) && $attributes['isLink'];
    	$size_slug      = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
    	$attr           = get_block_core_post_featured_image_border_attributes( $attributes );
    	$overlay_markup = get_block_core_post_featured_image_overlay_element_markup( $attributes );
    
    	if ( $is_link ) {
    		if ( get_the_title( $post_ID ) ) {
    			$attr['alt'] = trim( strip_tags( get_the_title( $post_ID ) ) );
    		} else {
    			$attr['alt'] = sprintf(
    				// translators: %d is the post ID.
    				__( 'Untitled post %d' ),
    				$post_ID
    			);
    		}
    	}
    
    	$extra_styles = '';
    
    	// Aspect ratio with a height set needs to override the default width/height.
    	if ( ! empty( $attributes['aspectRatio'] ) ) {
    		$extra_styles .= 'width:100%;height:100%;';
    	} elseif ( ! empty( $attributes['height'] ) ) {
    		$extra_styles .= "height:{$attributes['height']};";
    	}
    
    	if ( ! empty( $attributes['scale'] ) ) {
    		$extra_styles .= "object-fit:{$attributes['scale']};";
    	}
    
    	if ( ! empty( $extra_styles ) ) {
    		$attr['style'] = empty( $attr['style'] ) ? $extra_styles : $attr['style'] . $extra_styles;
    	}
    
    	$featured_image = get_the_post_thumbnail( $post_ID, $size_slug, $attr );
    	if ( ! $featured_image ) {
    		return '';
    	}
    	if ( $is_link ) {
    		$link_target    = $attributes['linkTarget'];
    		$rel            = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : '';
    		$height         = ! empty( $attributes['height'] ) ? 'style="' . esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . '"' : '';
    		$featured_image = sprintf(
    			'<a href="%1$s" target="%2$s" %3$s %4$s>%5$s%6$s</a>',
    			get_the_permalink( $post_ID ),
    			esc_attr( $link_target ),
    			$rel,
    			$height,
    			$featured_image,
    			$overlay_markup
    		);
    	} else {
    		$featured_image = $featured_image . $overlay_markup;
    	}
    
    	$aspect_ratio = ! empty( $attributes['aspectRatio'] )
    		? esc_attr( safecss_filter_attr( 'aspect-ratio:' . $attributes['aspectRatio'] ) ) . ';'
    		: '';
    	$width        = ! empty( $attributes['width'] )
    		? esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';'
    		: '';
    	$height       = ! empty( $attributes['height'] )
    		? esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';'
    		: '';
    	if ( ! $height && ! $width && ! $aspect_ratio ) {
    		$wrapper_attributes = get_block_wrapper_attributes();
    	} else {
    		$wrapper_attributes = get_block_wrapper_attributes( array( 'style' => $aspect_ratio . $width . $height ) );
    	}
    	return "<figure {$wrapper_attributes}>{$featured_image}</figure>";
    }
    
    /**
     * Generate markup for the HTML element that will be used for the overlay.
     *
     * @param array $attributes Block attributes.
     *
     * @return string HTML markup in string format.
     */
    function get_block_core_post_featured_image_overlay_element_markup( $attributes ) {
    	$has_dim_background  = isset( $attributes['dimRatio'] ) && $attributes['dimRatio'];
    	$has_gradient        = isset( $attributes['gradient'] ) && $attributes['gradient'];
    	$has_custom_gradient = isset( $attributes['customGradient'] ) && $attributes['customGradient'];
    	$has_solid_overlay   = isset( $attributes['overlayColor'] ) && $attributes['overlayColor'];
    	$has_custom_overlay  = isset( $attributes['customOverlayColor'] ) && $attributes['customOverlayColor'];
    	$class_names         = array( 'wp-block-post-featured-image__overlay' );
    	$styles              = array();
    
    	if ( ! $has_dim_background ) {
    		return '';
    	}
    
    	// Apply border classes and styles.
    	$border_attributes = get_block_core_post_featured_image_border_attributes( $attributes );
    
    	if ( ! empty( $border_attributes['class'] ) ) {
    		$class_names[] = $border_attributes['class'];
    	}
    
    	if ( ! empty( $border_attributes['style'] ) ) {
    		$styles[] = $border_attributes['style'];
    	}
    
    	// Apply overlay and gradient classes.
    	if ( $has_dim_background ) {
    		$class_names[] = 'has-background-dim';
    		$class_names[] = "has-background-dim-{$attributes['dimRatio']}";
    	}
    
    	if ( $has_solid_overlay ) {
    		$class_names[] = "has-{$attributes['overlayColor']}-background-color";
    	}
    
    	if ( $has_gradient || $has_custom_gradient ) {
    		$class_names[] = 'has-background-gradient';
    	}
    
    	if ( $has_gradient ) {
    		$class_names[] = "has-{$attributes['gradient']}-gradient-background";
    	}
    
    	// Apply background styles.
    	if ( $has_custom_gradient ) {
    		$styles[] = sprintf( 'background-image: %s;', $attributes['customGradient'] );
    	}
    
    	if ( $has_custom_overlay ) {
    		$styles[] = sprintf( 'background-color: %s;', $attributes['customOverlayColor'] );
    	}
    
    	return sprintf(
    		'<span class="%s" style="%s" aria-hidden="true"></span>',
    		esc_attr( implode( ' ', $class_names ) ),
    		esc_attr( safecss_filter_attr( implode( ' ', $styles ) ) )
    	);
    }
    
    /**
     * Generates class names and styles to apply the border support styles for
     * the Post Featured Image block.
     *
     * @param array $attributes The block attributes.
     * @return array The border-related classnames and styles for the block.
     */
    function get_block_core_post_featured_image_border_attributes( $attributes ) {
    	$border_styles = array();
    	$sides         = array( 'top', 'right', 'bottom', 'left' );
    
    	// Border radius.
    	if ( isset( $attributes['style']['border']['radius'] ) ) {
    		$border_styles['radius'] = $attributes['style']['border']['radius'];
    	}
    
    	// Border style.
    	if ( isset( $attributes['style']['border']['style'] ) ) {
    		$border_styles['style'] = $attributes['style']['border']['style'];
    	}
    
    	// Border width.
    	if ( isset( $attributes['style']['border']['width'] ) ) {
    		$border_styles['width'] = $attributes['style']['border']['width'];
    	}
    
    	// Border color.
    	$preset_color           = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
    	$custom_color           = $attributes['style']['border']['color'] ?? null;
    	$border_styles['color'] = $preset_color ? $preset_color : $custom_color;
    
    	// Individual border styles e.g. top, left etc.
    	foreach ( $sides as $side ) {
    		$border                 = $attributes['style']['border'][ $side ] ?? null;
    		$border_styles[ $side ] = array(
    			'color' => isset( $border['color'] ) ? $border['color'] : null,
    			'style' => isset( $border['style'] ) ? $border['style'] : null,
    			'width' => isset( $border['width'] ) ? $border['width'] : null,
    		);
    	}
    
    	$styles     = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
    	$attributes = array();
    	if ( ! empty( $styles['classnames'] ) ) {
    		$attributes['class'] = $styles['classnames'];
    	}
    	if ( ! empty( $styles['css'] ) ) {
    		$attributes['style'] = $styles['css'];
    	}
    	return $attributes;
    }
    
    /**
     * Registers the core/post-featured-image block on the server.
     */
    function register_block_core_post_featured_image() {
    	register_block_type_from_metadata(
    		__DIR__ . '/post-featured-image',
    		array(
    			'render_callback' => 'render_block_core_post_featured_image',
    		)
    	);
    }
    add_action( 'init', 'register_block_core_post_featured_image' );
    Moderator bcworkz

    (@bcworkz)

    It’s impractical to alter core code and there does not appear to be any filter hooks we can use to cleanly alter what the code does. To alter query loop output at the PHP level you’d need to develop your own custom query loop block. It’s OK to copy core code into your custom block and alter that version. Just do not alter the original core version.

    CSS might still be a valid approach if there is some attribute in some element on the page that indicates whether a password was successfully entered. Unfortunately I don’t think there is, but I’ve not verified to be sure.

    Another option would be if JavaScript could detect successful password use, it could directly change the image’s display style. There may not be a simple solution.

    A crude workaround would be to not use the featured image and instead place the image in post content so it too is password protected.

    Thread Starter veri63

    (@veri63)

    @bcworkz It’s a shame if there is no other way.
    The easiest way is to really not put a thumbnail or put another photo – “empty”.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hide thumbnail in request loop’ is closed to new replies.