我正在创建一个新插件。我有一个表单,将帖子发送到不同的页面。一旦动作完成。我希望页面重定向回调用脚本的表单。
我试过wp_redirect函数。但我得到一个错误,说调用未定义的函数。
有谁能告诉我。哪个文件将包含在整个路径的操作文件中。或任何其他方法。这样我就可以了。
先谢谢。
答案 0 :(得分:1)
Wordpress有一个标准的表单处理程序,您可以在这些情况下使用wp-admin/admin-post.php
。
在为插件设置from时,请确保使用以下HTML -
<form action="admin-post.php" method="post"> // Add a name/ID if you wish, but it's not required
<?php wp_nonce_field('nonce-to-check'); ?>
<input type="hidden" name="action" value="save_my-plugin" />
/** Output your plugin page here */
</form>
现在,您可以添加操作,以便在用户点击提交时,admin-post.php
将以安全的方式调用该操作(即包括所有WP优点,包括wp_redirect
。
add_action('admin_post_save_my-plugin', array(&$this, 'on_save_changes'));
我建议在创建插件时使用Class,因为它允许更容易的结构化。
您提到您正在从前端重定向。使用admin-post.php
仍然可以做到这一点(忽略我的评论,我说它不是,我错了),你只需要引用wp-admin\admin-post.php
并使用nopriv
动作。< / p>
在前端,您可以包含您的表单 -
$example_plugin = new Example_Plugin();
$example_plugin->output_form();
以下是插件的更新完整示例,该插件将在“设置”菜单中添加一个按钮,以演示来自管理区域的正确WP重定向。
注意 - 当您在管理区域时可用的某些功能在前端不可用,因此需要不同的输出/工作方式。为此,我添加了$is_admin
属性。
<?php
/**
* Plugin Name: Example Plugin
* Description: An example plugin to show how they work, and how simple they are to set up.
* Author: David Gard
* Version: 2.0.1
*/
$example_plugin = new Example_Plugin();
class Example_Plugin{
private $user_logged_in;
private $is_admin;
/**
* Constructor
*/
function __construct(){
$this->user_logged_in = is_user_logged_in(); // Whether or not a user is logged in
$this->is_admin = is_admin(); // Whether or not the user is in the admin area
add_action('admin_menu', array(&$this, 'on_admin_menu'));
add_action('admin_post_save_my-plugin', array(&$this, 'on_save_changes'));
add_action('admin_post_nopriv_save_my-plugin', array(&$this, 'on_save_changes'));
}
/**
* Add a button the the Settings menu
*/
function on_admin_menu(){
$this->pagehook = add_submenu_page('options-general.php', __('Example Plugin'), __('Example Plugin'), 'manage_options', 'my-plugin', array(&$this, 'on_show_page'));
}
/**
* Action any changes and save them if required (admin area)
*/
function on_save_changes(){
/** Checks user permisisons */
if($this->user_logged_in && !current_user_can('manage_options')) :
$warning = new Invalid_Action('permission');
wp_die($warning, 'Cheatin’ uh?', array('back_link' => true));
endif;
/** Validate the securiy nonce (I.e. make sure the user is coming here from a valid location) */
check_admin_referer('nonce-to-check'); // The should match the value of 'wp_nonce_field()'
/** Set a default status */
$status = 99;
/** Do your stuff here and then set the necessary status...*/
$status = 1;
/** Set the correct status (so that the correct splash message is shown */
$_POST['_wp_http_referer'] = add_query_arg('status', $status, $_POST['_wp_http_referer']);
/** Redirect the user back to where they came from */
wp_redirect($_POST['_wp_http_referer']);
}
/**
* Render the page
*/
function on_show_page(){
?>
<div id="example-plugin-id" class="wrap">
<h2>Example Plugin</h2>
<?php $this->splash_message() ?>
<?php $this->output_form(); ?>
</div>
<?php
}
/**
* Out put the user form
*/
function output_form(){
$action = ($this->is_admin) ? 'admin-post.php' : 'wp-admin/admin-post.php';
?>
<form action="<?php echo $action; ?>" method="post">
<?php wp_nonce_field('nonce-to-check'); ?>
<input type="hidden" name="action" value="save_my-plugin" />
<div id="example-plugin-id-metabox" class="metabox-holder">
<p>This is a basic example plugin.</p>
<p>Click the button below to see how redirection works (it uses the 'on_save_changes()' function)</p>
<?php
if($this->is_admin) :
submit_button(__('Test me out'));
else :
echo '<input type="submit" value="Submit"></input>';
endif;
?>
</div>
</form>
<?php
}
/**
* Write a custom message at the top of an admin options page (if necessary)
*/
private function splash_message(){
/** Check that there is a status for a splash message to be displayed */
if(!$_REQUEST['status']) :
return false;
endif;
/** Work out the class of the splash message */
$message_classes[1] = 'updated';
$message_classes[99] = 'error';
$message_class = $message_classes[$_REQUEST['status']];
$this->set_splash_messages();
$message = $this->messages_splash[$_REQUEST['status']];
/** Display the message splash */
echo '<div id="message" class="'.$message_class.' below-h2">';
echo '<p>'.$message.'</p>';
echo '</div>';
}
/**
* Set the splash messages available for this plugin
*/
private function set_splash_messages(){
$this->messages_splash = array(
0 => '', // Unused. Messages start at index 1.
1 => __('This test works just fine.'),
99 => __('An unknown error occured, please try again.')
);
}
}
?>