0

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()

1 Answer 1

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();
    }
}
?>
1
  • Thank you, this worked Commented Aug 10, 2023 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.