How can PHP be integrated effectively with WordPress to maintain the design and formatting of a website when handling form submissions?

When handling form submissions in WordPress, it is important to integrate PHP effectively to maintain the design and formatting of the website. One way to achieve this is by using WordPress hooks and functions to process form data and display messages or errors within the existing design structure.

// Add this code to your theme's functions.php file

// Create a function to handle form submissions
function handle_form_submission() {
    if( isset( $_POST['submit'] ) ) {
        // Process form data here
        // Validate input, sanitize data, etc.

        // Display success or error message within the design
        echo '<div class="message">Form submitted successfully!</div>';
    }
}

// Hook the function to the appropriate action
add_action( 'wp', 'handle_form_submission' );