JavaScript for WordPress › Forums › Gutenberg Development › wpautop behavior for dynamic blocks
- This topic has 8 replies, 2 voices, and was last updated 5 years, 10 months ago by crerem.
-
AuthorPosts
-
December 13, 2018 at 12:29 pm #76378creremParticipant
I’m creating some custom dynamic blocks for WordPress Gutenberg editor
I use the PHP render for these blocks, meaning I have this code on save:
save: function( props ) {
// Rendering in PHP
return;},
The render function is called via this callback:
register_block_type( ‘my-plugin/latest-post’, array(
‘render_callback’ => ‘my_plugin_render_block_latest_post’,
) );
I’m not gonna post the function code since is irrelevant in this case. (I do a WP_Query and display some custom post data and return a html code),Gutenberg takes the output from the function and adds <p> and <br> tags (classic wpautop behaviour) and mess up the block.
Is this normal?
Is there any solution beside remove_filter( ‘the_content’, ‘wpautop’ ); ?Thank you for any input.
December 13, 2018 at 12:44 pm #76387Zac GordonKeymasterHi there! Not sure exactly where this is being called. In our example we return markup and nothing extra is added or filtered. If you just return something hardcoded in your function does the same thing happen? Trying to determine exactly where this behavior is triggered.
December 13, 2018 at 12:48 pm #76389creremParticipantHi,
Let me some tests with hard coded html and will get back to you.
ThanksDecember 13, 2018 at 1:28 pm #76392creremParticipantHi,
The same html markup but hard coded is displayed correct. The php generated markup is not . Looking at my php function i think that. myabe, using the ob_start() and ob_get_contents php function could be the problem. This is the code that generated the output.ob_start(); while ($recent_posts->have_posts()): $recent_posts->the_post(); if($type == 'estate_property'){ get_template_part('templates/property_unit'.$property_card_type_string); } else { if(isset($attributes['align']) && $attributes['align']=='horizontal'){ get_template_part('templates/blog_unit'); }else{ get_template_part('templates/blog_unit2'); } } endwhile; $templates = ob_get_contents(); ob_end_clean();
December 13, 2018 at 1:48 pm #76394creremParticipant🙂 i think i found it, It happens in your 12-dynamic example also.(maybe you can confirm it for me )
If you use get_the_excerpt() (and i think get_the_content() also ) it will trigger the wpautop filter.
For ex
foreach ( $recent_posts as $post ) { $post_id = $post['ID']; $markup .= get_the_excerpt($post_id).sprintf( '<li> <a href="%1$s">%2$s</a> line1 line2 </li>', esc_url( get_permalink( $post_id ) ), esc_html( get_the_title( $post_id ) ) ); } $markup.='</ul>';
Here is a screenshot http://prntscr.com/luhguk
Can you confirm my findings ? Thank you
December 13, 2018 at 4:38 pm #76412Zac GordonKeymasterYup, this makes sense to me that this would be happening anytime you went to get content where that filter gets applied.
December 14, 2018 at 7:43 am #76437creremParticipantSo, is this a bug or expected behavior ? Any solutions for this situation -( removing the filter is not acceptable on places like themeforest ).
Thank you for your help.December 14, 2018 at 2:58 pm #76469Zac GordonKeymasterIf I understand the issue correctly it sounds like you’re getting content and that content has filters applied that you don’t want correct? Specifically wpautop.
I guess so that I can better understand what it is that you are trying to do, why don’t you want the wpautop filter applied? If you really don’t want the filter my two first guesses are you can get the content pre being filtered with the REST API context “edit” and content.raw. However, not sure that’s what you want either.
The other option would be to just remove that filter in the one instance where you are calling it. I’m not sure exactly what conditional check would work here, but that would be the second approach.
Without seeing the code or understanding what you’re trying to do specifically, I’m just kind of guessing here. Sorry if I’m still not understanding the issue..
Let me know!
December 17, 2018 at 8:45 am #76882creremParticipantHi,
I’m doing the plugin for ThemeForrest and in their guidelines they ask to not remove the wpautop filter. The guys that will do the review are not gonna investigate too much the problem – they will just reject it. That’s why i was trying to not use this solution. The alternative will be to useget_post_field( 'post_excerpt',..)
and see if the Gutenberg team will consider this a issue or not.Thank you again for taking the time to look over this.
-
AuthorPosts
- You must be logged in to reply to this topic.