Question

  • Creator
    Topic
  • #4210339

    Problem running DB queries using PHP in WordPress

    Locked

    by apn72 ·

    Is it possible to run PHP code in a WordPress HTML block? Or does WordPress only allow PHP via the use of snippet plugins?
    I want to save form data to the WP Db on clicking a HTML submit button (and can’t get it to work or even show an error message) is that something that should be achievable?

All Answers

  • Author
    Replies
    • #4210340

      Executing PHP in WordPress HTML Blocks and Saving Form Data to Database

      by smithraaj33 ·

      In reply to Problem running DB queries using PHP in WordPress

      WordPress does not allow direct execution of PHP code within HTML blocks. To save form data to the WordPress database on a button click, use a combination of JavaScript and AJAX or consider using custom plugins or functions.php for PHP-based solutions.

    • #4210437

      Implementing PHP in WordPress for Form Submission and Database Integration

      by matrixenzo843 ·

      In reply to Problem running DB queries using PHP in WordPress

      Yes, it is possible to run PHP code in WordPress, but not directly within an HTML block in the post or page editor. WordPress separates PHP code from content for security and simplicity. To execute PHP code, you typically use one of the following methods:

      PHP in Theme Files: You can add PHP code to your theme’s files (like functions.php). This is a common way to add functionalities like handling form submissions.

      Custom Plugins: Writing a custom plugin is another way to execute PHP code. This is more scalable and safer, especially if the functionality you’re adding is complex.

      Snippets Plugins: Plugins like “Code Snippets” allow you to add PHP code snippets that can be executed. These are easier for those who don’t want to edit theme files directly.

      Shortcodes: You can create a custom shortcode in your theme’s functions.php file or a custom plugin. The shortcode can be placed in the content, and when rendered on the page, it will execute the PHP code associated with it.

      Regarding saving form data to the WordPress database when clicking an HTML submit button, this is definitely achievable. Here’s a basic outline of how you might approach it:

      Create the Form: You can create an HTML form in your post/page or in a template file.

      Handle Form Submission: Write a PHP function to handle the form submission. This function should be placed in your theme’s functions.php file or in a custom plugin. The function will process the form data and save it to the database.

      Use WordPress Functions: Use WordPress functions like wp_insert_post, update_post_meta, or direct database manipulation functions like $wpdb->insert to save data to the database.

      Error Handling: Ensure proper validation and error handling in your PHP code. This will help in debugging if something doesn’t work.

      Security: Make sure to secure your form against common threats like SQL injection and Cross-Site Scripting (XSS).

      Ajax (Optional): For a more seamless experience, you can use Ajax to submit the form data without reloading the page. This involves a bit of JavaScript along with your PHP handling script.

      If you’re not seeing any error messages when the form submission fails, it might be due to how errors are handled in your PHP code or WordPress configuration. Ensure that debugging is enabled in WordPress during development to see error messages. This can be done by setting WP_DEBUG to true in your wp-config.php file. Remember to turn debugging off in a

      • #4210939

        Reply To: Problem running DB queries using PHP in WordPress

        by smithraaj33 ·

        In reply to Implementing PHP in WordPress for Form Submission and Database Integration

        Yes, it is possible to run PHP code in WordPress, but not directly within an HTML block in the post or page editor. WordPress separates PHP code from content for security and simplicity. To execute PHP code, you typically use one of the following methods:

        PHP in Theme Files: You can add PHP code to your theme’s files (like functions.php). This is a common way to add functionalities like handling form submissions.

        Custom Plugins: Writing a custom plugin is another way to execute PHP code. This is more scalable and safer, especially if the functionality you’re adding is complex.

        Note: irrelevant link removed by moderator.

        • This reply was modified 4 months, 1 week ago by Avatar photokees_b.
    • #4214635

      Reply To: Problem running DB queries using PHP in WordPress

      by sehartalokar ·

      In reply to Problem running DB queries using PHP in WordPress

      While you can’t directly embed PHP code within a WordPress HTML block, you have several options to achieve your goal of saving form data to the database:
      Popular plugins like Contact Form 7, Gravity Forms, and Ninja Forms handle form creation, validation, submission, and database integration seamlessly. or Add the shortcode [your_shortcode_name] within a regular WordPress block to display the form.

Viewing 2 reply threads