admin管理员组

文章数量:1410674

I have created a form on my wordpress site, a function to process the data located in functions.php, and an action php file. I have tested the processing function and know it works, but I'm having trouble getting everything to communicate with wordpress.

update_item_meta.php is located in my theme directory.

Form Opening Tag

<form method="post" id="project-info" action="/wp-content/themes/Avada/update_item_meta.php">

update_item_meta.php

<?php
//connect to WP
define('WP_USE_THEMES', false);
require('../../../wp-load.php');

//call function from functions.php to process form data
update_item_meta_data();
?>

Note: wp-load.php is located in the main site root

If someone could help me out I would really appreciate it. If i had to guess it's a problem with either the path to update_item_meta.php in the form action or update_item_meta.php, but I haven't been able to figure it out.

I have created a form on my wordpress site, a function to process the data located in functions.php, and an action php file. I have tested the processing function and know it works, but I'm having trouble getting everything to communicate with wordpress.

update_item_meta.php is located in my theme directory.

Form Opening Tag

<form method="post" id="project-info" action="/wp-content/themes/Avada/update_item_meta.php">

update_item_meta.php

<?php
//connect to WP
define('WP_USE_THEMES', false);
require('../../../wp-load.php');

//call function from functions.php to process form data
update_item_meta_data();
?>

Note: wp-load.php is located in the main site root

If someone could help me out I would really appreciate it. If i had to guess it's a problem with either the path to update_item_meta.php in the form action or update_item_meta.php, but I haven't been able to figure it out.

Share Improve this question asked Feb 20, 2016 at 18:10 nickybnickyb 112 silver badges5 bronze badges 1
  • obviously you processing function do not work, but it is impossible to give any feedback without its code. What debuging have you done, is it even being called? have you checked the php error log? – Mark Kaplun Commented Feb 20, 2016 at 18:36
Add a comment  | 

1 Answer 1

Reset to default 1

Instead of sending the form to a custom PHP script where you must load WordPress manually, you should send the from directly to a WordPress page.

You could use admin_post_{action} hook, it is similar to how admin-ajax.php works, but used to handle GET and POST requests.

The form:

<form action="<?php echo admin_url( 'admin-post.php' ); ?>" method="post">
  <input type="hidden" name="action" value="my_action">
  <input type="submit" value="Submit">
</form>

And the actions:

add_action( 'admin_post_my_action', 'cyb_my_action' );
add_action( 'admin_post_nopriv_my_action', 'cyb_my_action' );
function cyb_my_action() {
   // Process your form here
}

You could also send the form to any other page within WordPress and process the form in init action hook (or any other action you may consider better for your needs, but init is, general, a good action to proccess custom forms).

For example, if the form is embeded in a post of any type, including pages, you could send the form to itself:

<form id="cyb-contact-form" action="<?php echo esc_url(get_the_permalink()); ?>" method="post">
</form>

Them, you can process it on init action hook:

add_action( 'init', 'cyb_process_my_form' );
function cyb_process_my_form() {
   // Process your form here
}

本文标签: phpCustom Form Processing Issue