admin管理员组

文章数量:1332978

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.

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.

Share Improve this question asked Jun 16, 2020 at 13:20 Louise FinchLouise Finch 211 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

本文标签: Creating a custom functions page wordpress