cannot exclude element
-
Hi there,
I am unable to exclude classes.
Here is my HTML:
and here are my settings:
But links are excluded at the HTML elements as well, and that post entry title is a link, so widows shouldn’t be prevented on that link.
I have cleared the wp-Typography plugin cache, my grid cache, and all my other caches, CDN, etc…
Am I specifying something wrong in my settings?
-
This topic was modified 3 years, 1 month ago by
berry metal.
-
This topic was modified 3 years, 1 month ago by
berry metal.
-
This topic was modified 3 years, 1 month ago by
-
where would
$grid_id
come from? Are you sure those filter hooks are valid? I didn’t see them in the Grid Builder documentation.What’s your programming knowledge (not PHP specifically, but concepts like control structures, scope, etc.)? (So that I don’t assume prior knowledge while explaining things.)
Hi Pepe,
$grid_id is a variable that is the ID of my grids, and it’s declared already in 2 filters in my functions.php, I use these 2 grid filters to get the content for my grids:
function prefix_query_args( $query_args, $grid_id ) { if ( 2 === $grid_id ) { global $post; $referer = wp_get_referer(); $post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID; $terms = wp_get_post_terms( $post->ID, 'relations' ); $query_args['tax_query'] = array( array( 'taxonomy' => 'relations', 'field' => 'term_id', 'terms' => $terms[0], ), ); $query_args['post_type'] = 'items'; } return $query_args; } add_filter( 'wp_grid_builder/grid/query_args', 'prefix_query_args', 10, 2 ); add_filter( 'wp_grid_builder/grid/query_args', function( $query_args, $grid_id ) { global $post; if ( 3 === $grid_id ) { $referer = wp_get_referer(); $post_id = wp_doing_ajax() ? url_to_postid( $referer ) : $post->ID; $related = get_post_meta( $post_id, 'related_post_ids', true ); if ( ! empty( $related ) ) { $query_args['post__in'] = explode( ',', $related ); unset( $query_args['category__in'] ); } } return $query_args; }, 10, 2 );
If it’s already declared earlier in the code, what would be the correct syntax for me for the conditional?
Regarding the the 2 filters that include my grid content into wpTypography:
add_filter('wp_grid_builder/the_excerpt', [ 'WP_Typography', 'process' ] ); add_filter('wp_grid_builder/the_content', [ 'WP_Typography', 'process' ] );
These function correctly, becuse if I remove these, my grid content is not included in wpTypography.
I presented the API to the Grid developer and he helped me with the functions.
Thanks,
best regards!I don’t know anything about scope and control structures.
That
$grid_id
appears to be an argument of the specific filter hookwp_grid_builder/grid/query_args
you use. The question is, when/where do you have that variable available in the order of WordPress action/filter executions? This is not something I can glean from those code snippets. I also don’t know the control flow of the Grid Builder plugin.If that’s your actual code, i.e. two separate filters running on the same hook just to check for specific grid IDs, I’d suggest refactoring them into a single filter function. You might then add your
add_filter('wp_grid_builder/the_excerpt', ...
to the relevantif
clause.@erikalleman Function parameters and local variables are locally scoped, i.e. valid only inside the function body. That’s why to access the global
$post
variable, you have to tell that to the PHP engine via theglobal
keyword.That’s why I was asking about
$grid_id
. In your code example, it is a function parameter and thus can’t be used outside of that functions body.This can be ignored:
add_filter('wp_grid_builder/the_excerpt', [ 'WP_Typography', 'process' ] );
Only this is actually used:
add_filter('wp_grid_builder/the_content', [ 'WP_Typography', 'process' ] );
This is where the filter is applied in the file:
plugins/wp-grid-builder/frontend/sources/class-posts.php:/** * Query Posts * namespace WP_Grid_Builder\FrontEnd\Sources; use WP_Grid_Builder\Includes\Helpers; use WP_Grid_Builder\Includes\First_Media; // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Post class. * * @class WP_Grid_Builder\FrontEnd\Sources\Post * @since 1.0.0 */ class Posts { /** * Holds settings instance * * @since 1.0.0 * @access protected * * @var array */ protected $settings; /** * WP_Query args * * @since 1.0.0 * @access private * * @var array */ private $query_args; /** * WP_Query * * @since 1.0.0 * @access private * * @var object */ private $query; /** * Holds queried posts * * @since 1.0.0 * @access public * * @var array */ public $posts = []; /** * Holds post object * * @since 1.0.0 * @access public * * @var object */ public $post = []; /** * Constructor * * @since 1.0.0 * @access public * * @param array $settings Holds grid settings. */ public function __construct( $settings ) { $this->settings = $settings; $this->attachment = new Attachment( $settings ); } /** * Parse posts query args * * @since 1.4.0 * @access public */ public function parse_query() { if ( $this->settings->is_main_query ) { $this->main_query(); } else { $this->build_query(); $this->run_query(); } } /** * Retrieves posts based on query variables * * @since 1.4.0 * @access public * * @return array Queried objects */ public function get_results() { if ( ! $this->query || ! $this->query->have_posts() ) { return []; } $this->do_loop(); $this->get_attachments(); return apply_filters( 'wp_grid_builder/grid/the_objects', $this->posts ); } /** * Get attachment * * @since 1.0.0 * @access public */ public function get_attachments() { if ( empty( $this->attachment->ids ) ) { return; } $this->attachment->query( $this->posts ); } /** * Run main WP query * * @since 1.0.0 * @access public */ public function main_query() { global $wp_query; if ( wp_doing_ajax() && ! empty( $this->settings->main_query ) ) { // Add language to prevent issue when querying asynchronously. $this->settings->main_query['lang'] = $this->settings->lang; // Turns off SQL_CALC_FOUND_ROWS even when limits are present. $this->settings->main_query['no_found_rows'] = true; // Add WP Grid Builder to query args. $this->settings->main_query['wp_grid_builder'] = $this->settings->id; $this->query = new \WP_Query( $this->settings->main_query ); } elseif ( is_main_query() && ! is_admin() ) { $this->query = $wp_query; } } /** * Build custom query * * @since 1.0.0 * @access public */ public function build_query() { $this->set_post_type(); $this->set_post_status(); $this->set_posts_per_page(); $this->set_offset(); $this->set_orderby(); $this->set_author__in(); $this->set_post__in(); $this->set_post__not_in(); $this->set_attachments(); $this->set_mime_types(); $this->set_meta_key(); $this->set_meta_query(); $this->set_taxonomies(); $this->set_tax_query(); } /** * Run custom query * * @since 1.0.0 * @access public */ public function run_query() { // Add language to prevent issue when querying asynchronously. $this->query_args['lang'] = $this->settings->lang; // Turns off SQL_CALC_FOUND_ROWS even when limits are present. $this->query_args['no_found_rows'] = true; // Add WP Grid Builder to query args. $this->query_args['wp_grid_builder'] = $this->settings->id; // Filter the query args. $this->query_args = apply_filters( 'wp_grid_builder/grid/query_args', $this->query_args, $this->settings->id ); // Run the query. $this->query = new \WP_Query( $this->query_args ); }
In the row nr. 4 from below.
The filter is also applied in:
plugins/wp-grid-builder/frontend/sources/class-terms.php
and in
plugins/wp-grid-builder/frontend/sources/class-terms.phpCan you see the context now?
-
This reply was modified 3 years ago by
berry metal.
Thanks for the info about the scopes.
The file that I posted an excerpt from is:
plugins/wp-grid-builder/frontend/sources/class-post.php@erikalleman Yeah, sorry, no this does not really help. Have you got any programming knowledge at all or this just copying & pasting snippets provided by other people? (If the latter, you need a ready-made solution, not hints on what to do.)
Hi @pputzer,
my PHP skills are limited, my codes are 90% copy pasted, 10% modified by me as necessary.
I did a grep -r search on my public_html folder, searching for wp_grid_builder/grid/query_args, and it’s not present anywhere else in the code, that is the place where the filter is applied in the code, only.
What else would you need in order to limit the inclusion of the_content into wpTypography only in a specific grid with a certain ID, instead of for the whole wp_grid_builder/grid/query_args, because that will parse the content of every grid into wpTypography.
I need to exclude once specific grid.
Oh wait, I will search for wp_grid_builder/the_content instead, and be back.Yes, that would be helpful. I mostly need the
apply_filters
line itself to see what parameters are available.Here is where the wp_grid_builder/the_content filter is added in
plugins/wp-grid-builder/frontend/blocks/post.php:<?php /** * Post type blocks * */ use WP_Grid_Builder\Includes\Helpers; // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Retrieve post object from custom loop * * @since 1.0.0 * * @return object */ function wpgb_get_post() { return wpgb_get_object(); } /** * Retrieve the post ID * * @since 1.0.0 * * @return integer */ function wpgb_get_the_id() { $post = wpgb_get_post(); if ( ! isset( $post->ID ) ) { return false; } return (int) $post->ID; } /** * Display the post ID * * @since 1.0.0 * * @param array $block Holds block args. * @param array $action Holds action args. */ function wpgb_the_id( $block = [], $action = [] ) { $is_overview = wpgb_is_overview(); wpgb_block_start( $block, $action ); echo esc_html( ! $is_overview ? wpgb_get_the_id() : 9 ); wpgb_block_end( $block, $action ); } /** * Retrieve the post permalink * * @since 1.0.0 * * @return string */ function wpgb_get_the_permalink() { $post = wpgb_get_post(); if ( ! isset( $post->permalink ) ) { return; } return $post->permalink; } /** * Retrieve the post status * * @since 1.0.0 * @global object $wp_post_statuses * * @return string */ function wpgb_get_post_status() { global $wp_post_statuses; $post = wpgb_get_post(); if ( ! isset( $post->post_status ) ) { return; } if ( ! isset( $wp_post_statuses[ $post->post_status ] ) ) { return $post->post_status; } return $wp_post_statuses[ $post->post_status ]->label; } /** * Display the post status * * @since 1.0.0 * * @param array $block Holds block args. * @param array $action Holds action args. */ function wpgb_the_post_status( $block = [], $action = [] ) { $status = wpgb_get_post_status(); if ( empty( $status ) ) { return; } wpgb_block_start( $block, $action ); echo esc_html( $status ); wpgb_block_end( $block, $action ); } /** * Retrieve the format slug * * @since 1.0.0 * * @return string */ function wpgb_get_post_format() { $post = wpgb_get_post(); if ( ! isset( $post->post_format ) ) { return; } return $post->post_format; } /** * Display the format slug * * @since 1.0.0 * * @param array $block Holds block args. * @param array $action Holds action args. */ function wpgb_the_post_format( $block = [], $action = [] ) { $format = wpgb_get_post_format(); if ( empty( $format ) ) { return; } wpgb_block_start( $block, $action ); echo esc_html( get_post_format_string( $format ) ); wpgb_block_end( $block, $action ); } /** * Retrieve the post type * * @since 1.0.0 * @global object $wp_post_types * * @return string */ function wpgb_get_post_type() { $post = wpgb_get_post(); if ( ! isset( $post->post_type ) ) { return; } return $post->post_type; } /** * Display the post type (singular name) * * @since 1.0.0 * * @param array $block Holds block args. * @param array $action Holds action args. */ function wpgb_the_post_type( $block = [], $action = [] ) { global $wp_post_types; $post_type = wpgb_get_post_type(); if ( empty( $post_type ) ) { return; } if ( isset( $wp_post_types[ $post_type ] ) ) { $post_type = $wp_post_types[ $post_type ]->labels->singular_name; } wpgb_block_start( $block, $action ); echo esc_html( $post_type ); wpgb_block_end( $block, $action ); } /** * Filters title. * * @since 1.0.0 */ add_filter( 'wp_grid_builder/the_title', 'capital_P_dangit' ); add_filter( 'wp_grid_builder/the_title', 'wptexturize' ); add_filter( 'wp_grid_builder/the_title', 'convert_chars' ); add_filter( 'wp_grid_builder/the_title', 'trim' ); /** * Retrieve the post title * * @since 1.0.0 * * @return string */ function wpgb_get_the_title() { $post = wpgb_get_post(); if ( ! isset( $post->post_title ) ) { return; } return $post->post_title; } /** * Display the post title * * @since 1.0.0 * * @param array $block Holds block args. * @param array $action Holds action args. */ function wpgb_the_title( $block = [], $action = [] ) { $title = wpgb_get_the_title(); if ( 0 === strlen( $title ) ) { return; } $title = apply_filters( 'wp_grid_builder/the_title', $title ); wpgb_block_start( $block, $action ); echo wp_kses_post( $title ); wpgb_block_end( $block, $action ); } /** * Retrieve the post name * * @since 1.0.0 * * @return string */ function wpgb_get_the_name() { $post = wpgb_get_post(); if ( ! isset( $post->post_name ) ) { return; } return $post->post_name; } /** * Display the post name * * @since 1.0.0 * * @param array $block Holds block args. * @param array $action Holds action args. */ function wpgb_the_name( $block = [], $action = [] ) { $name = wpgb_get_the_name(); if ( 0 === strlen( $name ) ) { return; } wpgb_block_start( $block, $action ); echo esc_html( $name ); wpgb_block_end( $block, $action ); } /** * Filters content. * * We do not apply the_content filter to prevent layout issue. * The aim of this custom filter is to prevent any issue with themes and plugins. * By using the native WordPress filter it may create conflicts. * * @since 1.0.0 */ add_filter( 'wp_grid_builder/the_content', 'capital_P_dangit' ); add_filter( 'wp_grid_builder/the_content', 'shortcode_unautop' ); add_filter( 'wp_grid_builder/the_content', 'wpgb_strip_blocks' ); add_filter( 'wp_grid_builder/the_content', 'wptexturize' ); add_filter( 'wp_grid_builder/the_content', 'convert_smilies', 20 ); add_filter( 'wp_grid_builder/the_content', 'convert_chars' ); add_filter( 'wp_grid_builder/the_content', 'wpautop' ); add_filter( 'wp_grid_builder/the_content', 'do_shortcode', 11 ); add_filter( 'wp_grid_builder/the_content', 'trim' ); if ( function_exists( 'do_blocks' ) ) { add_filter( 'wp_grid_builder/the_content', 'do_blocks', 9 ); } /** * Filters excerpt. * * We do not apply the_excerpt filter to prevent layout issue. * The aim of this custom filter is to prevent any issue with themes and plugins. * By using the native WordPress filter it may create conflicts. * * @since 1.1.8 */ add_filter( 'wp_grid_builder/the_excerpt', 'capital_P_dangit' ); add_filter( 'wp_grid_builder/the_excerpt', 'shortcode_unautop' ); add_filter( 'wp_grid_builder/the_excerpt', 'wpgb_strip_blocks' ); add_filter( 'wp_grid_builder/the_excerpt', 'wpgb_strip_shortcodes' ); add_filter( 'wp_grid_builder/the_excerpt', 'strip_shortcodes' ); add_filter( 'wp_grid_builder/the_excerpt', 'wptexturize' ); add_filter( 'wp_grid_builder/the_excerpt', 'convert_smilies' ); add_filter( 'wp_grid_builder/the_excerpt', 'convert_chars' ); add_filter( 'wp_grid_builder/the_excerpt', 'trim' ); /** * Strip internal shortcodes and blocks. * * @since 1.0.0 * * @param string $string Content to strip. * @return string */ function wpgb_strip_blocks( $string = '' ) { if ( empty( $string ) ) { return ''; } // Strip plugin blocks. $string = preg_replace( '/<!--\s+\/?wp:wp-grid-builder.*?-->\r?\n?/m', '', $string ); // Strip plugin shortcodes (in case someone hook into strip_shortcodes_tagnames). $string = preg_replace( '/\[wpgb_.*?\]/m', '', $string ); return $string; } /** * Strip shortcodes and keep content. * * @since 1.0.0 * * @param string $string Content to strip. * @return string */ function wpgb_strip_shortcodes( $string = '' ) { if ( empty( $string ) ) { return ''; } // Strip all shortcodes. $string = preg_replace( '~(?:\[/?)[^/\]]+/?\]~s', '', $string ); // More aggressive regex '~(\[(?:\[??[^\[]*?\]))~s'. $string = preg_replace( '/\[(\/?(vc_column|vc_column_text).*?(?=\]))\]/', '', $string ); // Because vc_column include slash (1/4) that breaks regex. return $string; } /** * Filters content and keeps only allowable HTML elements. * Prevent layout and style conflicts. * * @since 1.0.0 * * @param string $string Content to filter through kses. * @return string */ function wpgb_kses_post( $string = '' ) { if ( empty( $string ) ) { return; } // Get allowed post tags. $allowed = wp_kses_allowed_html( 'post' ); // Remove img, audio, video and iframe tags. unset( $allowed['img'] ); unset( $allowed['audio'] ); unset( $allowed['video'] ); unset( $allowed['iframe'] ); // Remove not allowed HTML tags. $string = wp_kses( $string, $allowed ); return $string; } /** * Retrieve the post content * * @since 1.0.0 * * @return string */ function wpgb_get_the_content() { $post = wpgb_get_post(); if ( ! isset( $post->post_content ) ) { return; } return $post->post_content; } /** * Display the post content * * @since 1.0.0 * * @param array $block Holds block args. * @param array $action Holds action args. */ function wpgb_the_content( $block = [], $action = [] ) { $content = wpgb_get_the_content(); if ( empty( $content ) ) { return; } // We escape before to apply the_content filter (post content is also sanitized on input). // It allows to preserve blocks and shortcodes markup from 3rd party plugins. $content = wpgb_kses_post( $content ); $content = apply_filters( 'wp_grid_builder/the_content', $content );
Does this clarify the context now?
Yes. It doesn’t look like your
$grid_id
is available there. Without searching the whole plugin integration, did the author give any indication on how to access the ID of the current grid (or even if that’s a good way to identify the grid)? (Secondary question: Why do you want to restrict wp-Typography markup to only one of those grids?)I have just asked the developer how to identify the current grid and mentioned that $grid_id is not available at the time the
$content = apply_filters( ‘wp_grid_builder/the_content’, $content );
filter is applied.
Waiting for answer.
Regarding why I want to limit wpTypography to only one grid, it’s because as you see in the screenshot:the titles of the post entries in the related posts grid have to be dewidowed because widowing is overriding my padding rules for the title. The 2 content marked with the green squares are in 2 different grids.
Initially I tried to add the noTypo class to the title:
function noTypo_title ( $before = '<span class="noTypo">', $after = '<span>', $echo = true, $grid_name ) { $title = get_the_title(); if ( strlen( $title ) == 0 ) { return; } if ( 'related' === $grid_name && ! empty( $related ) ) { $title = $before . $title . $after; if ( $echo ) { echo $title; } else { return $title; } } } add_filter( 'the_title', noTypo_title, 2 );
to exclude it, but this code did not work.
So that is why I want to exclude the whole grid instead.Maybe the reason it didn’t work is the way the_content is parsed or output by the grid?
The code above both with and without the conditional results in a critical error.
-
This reply was modified 3 years ago by
berry metal.
-
This reply was modified 3 years ago by
berry metal.
This did not work because your filter function is incorrect. An example of a working filter would be:
function add_notypo_to_title( $title ) { return "<span class='noTypo'>$title</span>"; } add_filter( 'the_title', 'add_notypo_to_title', 10, 1 );
However, that does not help you if you somehow want to react to a specific grid. As I’m not familiar with your Grid Builder plugin, I cannot help you in that regard.
Personally, at this point I’d suggest disabling dewidowing as the much simpler solution. It’s not that useful or essential for HTML typography.
-
This reply was modified 3 years ago by
- The topic ‘cannot exclude element’ is closed to new replies.