How do I add my class the earliest possible?
-
Hi there,
I am using the following code to add my class “noTypo”:
function prefix_register_block( $blocks ) { // 'my_block' corresponds to the block slug. $blocks['wpgb_custom_title'] = [ 'name' => __( 'WPGB Custom Title', 'text-domain' ), 'render_callback' => 'prefix_my_block_render', ]; return $blocks; } add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 ); // The render callback function allows to output content in cards. function prefix_my_block_render() { // Get current post, term, or user object. $post = wpgb_get_post(); // Output the post title. echo '<span class="noTypo">' . esc_html( $post->post_title ) . '</span>'; }
But it seems that this code above is adding the class later than this code:
function add_notypo_to_title( $title ) { return "<span class='noTypo'>$title</span>"; } if (! is_admin() ) { add_filter( 'the_title', 'add_notypo_to_title', 10, 1 ); }
Both codes add the class successfully, yet the plugin that should use the class for dewidowing – wpTypography -, does not see the class if it’s added with the first code, but it does see the class when it’s added with the second code.
The conclusion is that the class is not available for the plugin at the time it’s processing my content:
add_filter('wp_grid_builder/the_content', [ 'WP_Typography', 'process' ] );
My question is how do I modify the first code so that the class will be added at the earliest possible time, so that at the time wpTypography is processing the content, it would already be available?
The wpTypography plugin does not inspect the final HTML, but it looks for the class at a much earlier stage.
It seems that when I am hooking into the_title in a global function, the class is added earlier.
Is there a way to make my first function global?
Would that make it add the class earlier?It makes sense that native WP code like the_title runs earlier than plugin code.
I tried to add “global $post;” into my function, but it will not make the class to be added earlier.
Can I troubleshoot this with Query Monitor to see when are these functions running? And how do I do that?
Does anyone have any other ideas?
According to the wpTypography developer:
what matters is whether the string ultimately passed to WP_Typography::process as its first parameter contains the element with the class
How do I verify if the string passed is the first parameter of the process?
Thanks in advance.
- The topic ‘How do I add my class the earliest possible?’ is closed to new replies.