0

I'm looking to create a custom function page which is a non WordPress PHP file. I have created a template with a form in my theme and I am looking to post the form to this custom function PHP file. The function is to add a new row into the WordPress database.

I have the this working via the main functions file of my theme however I want to create my own custom file to call that has the function within it. Please the below my code of the working with the main functions file were i use isset to tigger to when submit button is selected in form..

function register_user() {

  $name = $_POST['name'];
  $email = $_POST['email'];
  $password = $_POST['password'];  

  
  $options = [
    'cost' => 11,
  ];
  $passwordFromPost = $_POST['password'];
  $hash = password_hash($passwordFromPost, PASSWORD_BCRYPT, $options);

  global $wpdb; 
  $table_name = $wpdb->prefix . "yuenergy_users"; 

  $wpdb->insert($table_name, array(
                            'name' => $name, 
                            'email' => $email,
                            'password' => $hash
                            ),array(
                            '%s',
                            '%s',
                            '%s') 
    );
  }


if( isset($_POST['registerSubmit']) ) register_user();

Instead of using the main functions file to put ALL my functions in i have create a registerProcess.php in my theme in same directory as functions and use to wp-load.php to be able to use this file however no error appeaers but form isnt being post to database.

<?php
 require_once("../../..wp-load.php");

 function register_user() {

  $name = $_POST['name'];
  $email = $_POST['email'];
  $password = $_POST['password'];  

  $options = [
    'cost' => 11,
  ];
  $passwordFromPost = $_POST['password'];
  $hash = password_hash($passwordFromPost, PASSWORD_BCRYPT, $options);

  global $wpdb; 
  $table_name = $wpdb->prefix . "yuenergy_users"; 

  $wpdb->insert($table_name, array(
                            'name' => $name, 
                            'email' => $email,
                            'password' => $hash
                            ),array(
                            '%s',
                            '%s',
                            '%s') 
    );
  }


if( isset($_POST['registerSubmit']) ) register_user();

I'm new to wordpress and trying tohave a better understanding of how this all works.. go easy on me please.

0

I don't think Wordpress knows to load or run anything from your registerProcess.php file, so what I would do is leave the register_user() function in that file, and then require_once it from functions.php and do the active part of the code - the if .. register_user() inside functions.php where it will be run. (This is probably a bad way to do this, but if it works for you that's ok)

So functions.php should look like:

require_once('registerProcess.php');
if( isset($_POST['registerSubmit']) ) register_user();

And registerProcess.php should look the same, just remove the if from the last line

2
  • this works for me thank you :) id be interested to see if there is a better approach but thank you i will use this method for now! – Louise Finch Jun 16 '20 at 13:58
  • cool :) upvotes appreciated ;-) . Usually it's best to try and hook into the Wordpress way of doing things by fiding a hook or action which you can connect to rather than looking at $_POST directly yourself, which might interfere with something else that WP is doing. With structuring your code it is a great idea to move as much stuff out into separate PHP files and leave functions.php as clean as possible. – mozboz Jun 16 '20 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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