我已成功将 WordPress 与 Codeigniter 集成。我创建了一个插件,它添加了一个可以在编辑器中使用的简码。这也按预期工作。这是我的代码:
函数 get_property_filters($atts) {
foreach ($atts as $key => $value) {
$_POST['property_type'] = $value;
}
返回“用于替换 WP 页面中的简码的东西”;
}
add_shortcode('property_filters', 'get_property_filters');
我需要做的是从我的 WP 插件发送一个 POST
变量到 CI 脚本,因此行:
$_POST['property_type'] = $value;
我知道处理短代码的函数的返回值是为了用页面/帖子中的一些文本或小部件等替换短代码。我打算用空字符串替换简码。但是,在处理短代码的函数中,如何将 POST
变量发送到我的 Codeigniter 脚本?
我已经为此搜索了几个小时。这似乎是一个非常具体的问题。感谢您的帮助。
编辑: 我想过使用 session 变量来保存值,但我似乎无法在 WP 中设置 session 变量并从 CI 访问它。沿着这个思路有什么建议吗?
编辑 2: 我还想到了使用 $wpdb 从 CI 脚本查询 WP 数据库。可以这样做并且已经在某些情况下工作,但是,我无法直接从 WP 数据库获取 post_content
字段,而是获取呈现的文本。即我的简码被替换为单词“land”,但我希望查询返回 WP 页面中使用的简码,而不是替换字符串。
最佳答案
如果您想将 POST 数据直接发送到 CodeIgniter 脚本中,您可以使用 PHP 的 cURL 库(确保它已安装在您的 Web 服务器中)。
重要提示:首先您需要禁用 CodeIgniter 的 CSRF 检查。您可以从整个框架中禁用,或者您可以创建一个预系统 Hook 以在特定 Controller 中禁用 CSRF。
这是您的 WP 脚本中的 cURL 请求示例:
$value = "POST VALUE";
$post_data = array();
$post_data["property_type"] = $value;
$codeigniter_url = "http://example.com/codeigniter/handle_post";
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $codeigniter_url); //The URL to send the request
curl_setopt($post, CURLOPT_POST, count($post_data)); //Amount of POST fields
curl_setopt($post, CURLOPT_POSTFIELDS, $post_data); //The POST data.
curl_setopt($post, CURLOPT_RETURNTRANSFER, TRUE); //Returns the output from the requested script
curl_setopt($post, CURLOPT_SSL_VERIFYPEER, FALSE); //Do not verify the SSL certificate.
//The variable below will have the output from your Controller function in CodeIgniter.
$result = trim(curl_exec($post));
//The variable below will have any possible errors from the cURL request.
$errors = trim(curl_error($post));
//Now you can work with the $result variable which contains the results from your CI
//Controller.
然后,您可以创建 Controller 来处理您在 CodeIgniter 中的发布请求:
class Handle_post extends CI_Controller {
public function index()
{
$property_type = $this->input->post("property_type");
//Your actions here...
echo "RESULT FROM THE CONTROLLER IN CODEIGNITER";
}
}
有关 PHP 的 cURL 库的更多信息,您可以阅读 manual .
最好的问候。
关于php - WordPress/Codeigniter 集成 - 将 WP 简码属性传递给 CI 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27746279/