$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to call WordPress functions from a form processing script|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to call WordPress functions from a form processing script

matteradmin7PV0评论

I'm working on a plugin which submits data via a form from a custom admin page. This is a simplified version of my form:

<form action="<?php echo plugin_dir_path(); ?>/process.php" method="post">
    <input type="text" name="keyName">
    <input type="submit" value="Update">
</form>

The form is inside my main php file for the plugin, so it has access to all the WordPress functions like the plugin_dir_path() I called above.

However when the user clicks the "submit" button, and the $_POST variable is submitted to the "process.php" script, I lose access to all the WordPress functions in that process script.

I searched how to add WordPress functions into external scripts and I saw this question: How can I call WordPress core functions in external scripts?

The answer provided is that I include this line of code at the top of my processing script:

require_once("wp-load.php");

However when I do the "wp-load.php" is appended to the end of the current url which results in a 404 type error. I can't use the "get_site_directory()" function to point to the main WordPress install directory because it's a WordPress function.

How can I make this work? Is there an action hook I should be using to submit the form vs my own custom submit button?

I'm working on a plugin which submits data via a form from a custom admin page. This is a simplified version of my form:

<form action="<?php echo plugin_dir_path(); ?>/process.php" method="post">
    <input type="text" name="keyName">
    <input type="submit" value="Update">
</form>

The form is inside my main php file for the plugin, so it has access to all the WordPress functions like the plugin_dir_path() I called above.

However when the user clicks the "submit" button, and the $_POST variable is submitted to the "process.php" script, I lose access to all the WordPress functions in that process script.

I searched how to add WordPress functions into external scripts and I saw this question: How can I call WordPress core functions in external scripts?

The answer provided is that I include this line of code at the top of my processing script:

require_once("wp-load.php");

However when I do the "wp-load.php" is appended to the end of the current url which results in a 404 type error. I can't use the "get_site_directory()" function to point to the main WordPress install directory because it's a WordPress function.

How can I make this work? Is there an action hook I should be using to submit the form vs my own custom submit button?

Share Improve this question edited Nov 18, 2018 at 9:00 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 18, 2018 at 7:38 YAHsavesYAHsaves 1471 gold badge1 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 8

You should never post anything to plugins files directly. It's almost always a security flaw and it prevents site owner from hardening the site properly (in perfect situation no requests to PHP files inside wp-content should be necessary at all)

Good practice is that you use admin_post actions... (similar to admin_ajax).

So your form should look like so:

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

And then in your plugin you add your action method:

add_action( 'admin_post_my_action', 'prefix_admin_my_action' );
add_action( 'admin_post_nopriv_my_action', 'prefix_admin_add_foobar' );

function prefix_admin_my_action() {
    // Handle request then generate response using echo or leaving PHP and using HTML
}

PS. It's always a good idea to include some nonces inside that form too.

Post a comment

comment list (0)

  1. No comments so far