Fatal error: Cannot declare class WP_Block_Parser_Block, because the name is already in use in \wp-content\plugins\gutenberg\packages\block-serialization-default-parser\parser.php on line 10
I get this on homepage when simply activating WordPress 5.0 beta 1 and Gutenberg 4.1.1 together. My functions.php is using gutenberg_parse_blocks
and generating this.
I just tested with 5.0-beta3-43865 by installing and activating Gutenberg 4.1.1 and I was able to edit posts with Gutenberg and all looks well for me on the home page on the font end. Are you able to try with beta3 to see if the problem is still happening for you there?
My functions.php is using gutenberg_parse_blocks and generating this.
Is this for a custom theme you are building? If yes, are you able to include a more complete code example and would it also be possible to explain the end goal for what you are trying to do?
The error is still there in 5.0-beta3-43865
and Gutenberg 4.1.1.
What completely eliminates the error is commenting out this line require_once dirname( __FILE__ ) . '/../packages/block-serialization-default-parser/parser.php';
in /wp-content/plugins/gutenberg/lib/blocks.php
on line 86. Then the error is gone and everything works.
Maybe class_exists
next to require_once
could help.
I just have a function in functions.php:
function myParseBlocks(){
global $post;
$blocks = gutenberg_parse_blocks( $post->post_content );
}
and calling this function on page.php causes this.
My test blog is running WP 5.0-beta3-43867 with Gutenberg 4.3.0-alpha-0841246 (from Daniel Bachhuber's nightly), and the theme I'm using is Cover2 (Github here), which also appears to exhibit this in single post view.
I've narrowed it down to this function (called from header.php), where the gutenberg_parse_blocks()
call triggers the issue.
I'm not saying I'm 100 % sure it's Gutenberg and not the theme(s) at fault here, but I just thought I'd post this just in case it helps to find out either way.
I tested this with WordPress 5.0-RC1-43946 and Gutenberg 4.5.1 by adding this:
function myParseBlocks(){
global $post;
$blocks = gutenberg_parse_blocks( $post->post_content );
}
to the functions.php
file for the Twenty Seventeen theme and then viewing the home page and I couldn't replicate the problem at this time.
I also tried installing and activating the Cover2 theme (version 1.4.1) and found that I could load the home page normally without any errors.
@manake if you're still having trouble with the latest version of WP5.0-RC (or if I've done anything wrong in my testing steps above), can you please note the latest version you've tested with and re-open this issue?
I can confirm that the issue is not resolved. I can't re-open the ticket.
Do this and the issue is gone:
//
before require_once dirname( __FILE__ ) . '/../packages/block-serialization-default-parser/parser.php';
.5.0-RC3-43968
I'm simply running gutenberg_parse_blocks
in add_filter( 'body_class',
in functions.php and that's it. I also run it in page.php.
You can reproduce this even with Twenty Nineteen, by adding <?php $blocks = gutenberg_parse_blocks( $post->post_content ); ?>
to header.php.
Replacing gutenberg_parse_blocks()
calls with parse_blocks()
seems to work around the issue (for me at least), and the issue here, if any, is apparently just one of confusion: renaming the function broke backwards compatibility, but since gutenberg_parse_blocks()
is still defined, instead of a "Call to undefined function" you get this attempt to redefine WP_Block_Parser
(via inclusion of parser.php), resulting in the "name is already in use".
Yes, I would keep this closed.
gutenberg_parse_blocks()
to parse_blocks()
. Problem solved.
Most helpful comment
Yes, I would keep this closed.
Solution (for anyone looking for one):
Have WordPress 5.0+ and rename
gutenberg_parse_blocks()
toparse_blocks()
. Problem solved.