Support » Developing with WordPress » How do I make form action attribute go to themes function.php

  • Hi, I’m having issues running some code in my wordpress website’s theme functions.php.

    I created a page which has a form, and the action attribute of the form is supposed to submit the data to the themes functions.php

    On submit of the form, the page redirects to the functions.php file but does not do anything.

    Here is the code for the form

    <div>
       <form name="formSenda" method="POST" action="../wp-content/themes/storefront/functions.php">
          <input type="text" placeholder="name" id="nameSenda" name="nameSenda">
          <input type="email" placeholder="emailSenda" id="emailSenda" name="emailSenda">
          <input type="submit" value="Check" name="submitSenda">
       </form>
    </div>

    Here is the code in functions.php

    function inSenda(&$na,&$em){
       $message = "Hi $na, is your email $em?";
       $headers = "From Fly Gadgets \r\n";
       mail($em, "Invoice", $message, $headers);
       echo "<script type='text/javascript'> document.location = 'categories'; </script>";
    };
    
    $nameSenda = $_POST['nameSenda'];
    $emailSenda = $_POST['emailSenda'];
    if(!empty($nameSenda) && !empty($emailSenda)){
        inSenda($nameSenda, $emailSenda);
    }

    Thanks for your time.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Using the theme’s functions file in the action attribute won’t ever work, because the file needs WordPress loaded.
    Forms should be handled in a plugin. If your code doesn’t need WoredPress, put the absolute URL of the file into the action attribute. Otherwise, leave it empty.
    Code that is directly in the PHP file (not in a function) is executed when the file is loaded, which in WordPress is before everything is initialized.
    https://codex.wordpress.org/Plugin_API/Action_Reference

    Make the action the same page or to the categories page:

    <div>
       <form name="formSenda" method="POST" action="../categories/">
          <input type="text" placeholder="name" id="nameSenda" name="nameSenda">
          <input type="email" placeholder="emailSenda" id="emailSenda" name="emailSenda">
          <input type="submit" value="Check" name="submitSenda">
       </form>
    </div>

    Then in your functions file, check to see if the form has been submitted and then process the info:

    if($_REQUEST['submitSenda'] == 'Check') {
    
       $nameSenda = $_POST['nameSenda'];
       $emailSenda = $_POST['emailSenda'];
       if(!empty($nameSenda) && !empty($emailSenda)){
         $message = "Hi $nameSenda, is your email $emailSenda?";
         $headers = "From Fly Gadgets \r\n";
         mail($emailSenda, "Invoice", $message, $headers);
       }
    }
    Thread Starter tammyabra007

    (@tammyabra007)

    @wputler Thanks a lot, it worked… But now I would like to use wp_mail() because mail() sends the email using my hosting providers email…

    I have setup SMTP for my email using WP mail SMTP plugin but it only works with wp_mail() and wp_mail() is not working.

    I’m glad that worked.

    First, before going any further, you should read up on validating and sanitizing input data to avoid creating a security problem with your site.
    https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data

    For the wp_mail:
    Have you sent a test email from the plugin? Does that work?

    Do you get any errors on the screen, in the php error log or have you tried turning on wp_debug in config and seeing what it says?

    If it’s not executing wp_mail, it’s likely that it can’t find it, or it doesn’t have permission to run it. See what the error logs and messages say.

    PS: remember to turn off debug before taking the site live.

    Thread Starter tammyabra007

    (@tammyabra007)

    @wputler Yes, the test worked just fine.
    I don’t see any errors on the screen… I added wp_debug, turned it on and tested it, did not create any debug.log file in wp-content, so i’m guessing no error was logged…

    I think you may have your headers set incorrectly.

    Try and see if this line works instead of the mail line:
    wp_mail(“yourname@email.com”, “Invoice”, “Test”);

    Change yourname@email.com to your actual email. If this works, then you know the wp_mail function is working.

    Read this about using headers:
    https://developer.wordpress.org/reference/functions/wp_mail/#using-headers-to-set-from-cc-and-bcc-parameters

    Thread Starter tammyabra007

    (@tammyabra007)

    @wputler It didn’t work…
    Some articles say I should call WP-load.php in the themes function.php

    But it doesn’t seem to work too, I’m guessing I didn’t call it the right way, what do you think is the correct way to call WP-load.php in themes function.php

    There is no correct way to call wp-load.php. Your code should not load WP. If you want to use wp_mail you need to let WP load your code and hook into the appropriate action to execute your code when WP loads. As I said, this code should not be in the theme’s functions.php file.

    @tammyabra007 as @joyously said, the absolute best way to do a form like this on a live site would be to use a form plugin. There are several free ones available and you can use them to create a simple form and send an email notification to the user and administrator. The plugin authors keep the code updated and secure. And the setup will be much faster than what you’ve gone through so far.

    That said, if you are just trying to learn WordPress coding and are playing around, you could call an action to execute code on a page:

    add_action( ‘wp_loaded’, ‘myprefix_sendform’, 15 );
    function myprefix_sendform() {

    if($_REQUEST[‘submitSenda’] == ‘Check’) {

    $nameSenda = $_POST[‘nameSenda’];
    $emailSenda = $_POST[’emailSenda’];
    if(!empty($nameSenda) && !empty($emailSenda)){
    $message = “Hi $nameSenda, is your email $emailSenda?”;
    $headers = “From Fly Gadgets \r\n”;
    wp_mail($emailSenda, “Invoice”, $message);
    }
    }
    }

    The action you select depends on what you want to do. If you want to show a message that says “thanks”, you might want to hook into “loop_start” or “pre_get_posts”

    What you have now is DEFINITELY not safe enough to put on a live site.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How do I make form action attribute go to themes function.php’ is closed to new replies.