Support » Plugin: Gutenberg » Check if page is using Gutenberg, or if content is in a block?

  • Resolved Back to Front

    (@traverser11)


    Hello, how could I make this function conditional to only run on the front end if the page isn’t using Gutenberg?

    I just wanted to allow some html tags in get_the_excerpt on the archive pages.
    But this doesn’t work with AB Post Grid Block.

    So,

    
    	function excerpt_allow_markup($text) {
            global $post;
            if ( '' == $text ) {
                    $text = get_the_content('');
                    $text = apply_filters('the_content', $text);
                    $text = str_replace('\]\]\>', ']]>', $text);
                    $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
                    $text = strip_tags($text, '<img>','<p>','<em>','<strong>','<h3>');
                    $excerpt_length = 80;
                    $words = explode(' ', $text, $excerpt_length + 1);
                    if (count($words)> $excerpt_length) {
                            array_pop($words);
                            array_push($words, '[...]');
                            $text = implode(' ', $words);
                    }
            }
            return $text;
    }
    
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'excerpt_allow_markup');
    

    If I ask

    if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) {

    It tells me

    Call to undefined function get_current_screen()

    I guess this function must be intended for the back end?

    I thought if(is_archive()) should work too, but apparently not for this block.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator Marius L. J.

    (@clorith)

    Hiya,

    Yes, those are not intended for use at all really (note the use of gutenberg in the function name, which means they’ll be changed as well).

    What you could do to check the content (until we have an official way to check, I’m hoping for an is_block_editor() conditional, but that’s besides the point) is something like this:

    
    function wp32718934_content_has_blocks() {
        return false !== strpos( $content, '<!-- wp:' );
    }
    

    This would return true if the content has blocks, and should be treated like Gutenberg content.

Viewing 1 replies (of 1 total)
  • The topic ‘Check if page is using Gutenberg, or if content is in a block?’ is closed to new replies.