admin管理员组

文章数量:1134556

I have a custom block which I've created with the following code:

<?php include get_template_directory() . '/template-parts/blocks/form/from/functions.php';

if(!isset($_POST['firstFormDisplayed'])) {
// we haven't yet displayed the form asking for numbers, so do that now.
displayFirstForm();
} else {
// we've got the numbers, but not the details, so do that now!
displaySecondForm();
} ?>

I have this added to a page and that page allows me to edit it with no problems. If I edit it as a Post, it's showing an error:

Fatal error: Cannot redeclare displayFirstForm() (previously declared in 

/public_html/wp-content/themes/my-theme/template-parts/blocks/form/from/functions.php:5) in 

/public_html/wp-content/themes/my-theme/template-parts/blocks/form/from/functions.php on line 5

I don't need to edit this block in the WordPress editor, I've tried adding a "is_admin()" but it's still showing as an error.

Line 5 in functions.php is:

function displayFirstForm()

I have a custom block which I've created with the following code:

<?php include get_template_directory() . '/template-parts/blocks/form/from/functions.php';

if(!isset($_POST['firstFormDisplayed'])) {
// we haven't yet displayed the form asking for numbers, so do that now.
displayFirstForm();
} else {
// we've got the numbers, but not the details, so do that now!
displaySecondForm();
} ?>

I have this added to a page and that page allows me to edit it with no problems. If I edit it as a Post, it's showing an error:

Fatal error: Cannot redeclare displayFirstForm() (previously declared in 

/public_html/wp-content/themes/my-theme/template-parts/blocks/form/from/functions.php:5) in 

/public_html/wp-content/themes/my-theme/template-parts/blocks/form/from/functions.php on line 5

I don't need to edit this block in the WordPress editor, I've tried adding a "is_admin()" but it's still showing as an error.

Line 5 in functions.php is:

function displayFirstForm()
Share Improve this question asked Aug 10, 2023 at 10:27 RustyInglesRustyIngles 1133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This should work:

<?php
if ( is_user_logged_in() ) {
    echo "This isn't editable in here"; 
} else { 
    include get_template_directory() . '/template-parts/blocks/form/from/functions.php';

    if ( ! isset( $_POST['firstFormDisplayed'] ) ) {
        // we haven't yet displayed the form asking for numbers, so do that now.
        displayFirstForm();
    } else {
        // we've got the numbers, but not the details, so do that now!
        displaySecondForm();
    }
}
?>

本文标签: WordPress custom Block showing error in Posts editor but working in Pages editor