I have successfully integrated WordPress with Codeigniter. I have created a plugin that adds a shortcode that can be used in the editor. This also works as expected. Here is my code:
function get_property_filters($atts) {
foreach ($atts as $key => $value) {
$_POST['property_type'] = $value;
}
return 'Something to replace shortcode in WP Page';
}
add_shortcode('property_filters', 'get_property_filters');
What I need to do is send a POST variable from my WP plugin to a CI script, hence the line:
$_POST['property_type'] = $value;
I understand that the return value of the function that handles the shortcode is meant to replace the shortcode with some text or widget, etc in the Page/Post. I plan to replace the shortcode with an empty string. But, within the function that handles the shortcode, how can I send a POST variable to my Codeigniter script?
I have searched for hours on this. It seems to be a very specific question. Your help is appreciated.
EDIT: I had a thought about using session variables to save the value, but it doesn't seem like I can set a session variable in WP and access it from CI. Any suggestions along this line of thought?
EDIT 2: I also had the idea to query the WP database from the CI script using $wpdb. It is possible to do this and already works in some scenarios, however, I cannot get the post_content field directly from the WP database, instead I get the rendered text. i.e. My shortcode is replaced with the word "land", but I want the query to return the shortcode that was used in the WP page, not the replacement string.
If you want to send a POST data directly into a CodeIgniter script, you can use PHP's cURL library (make sure it is installed in your web server).
Important: First you will need to disable CodeIgniter's CSRF check. You could disable from the entire framework, or you could create a pre-system hook to disable CSRF in a specific controller.
Here is an example of a cURL request in your WP script:
$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.
Then, you can create your controller to handle your post request in 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";
}
}
For more information about PHP's cURL library, you can read the manual.
Best regards.
Just for addition to your approach regarding retrieval of content from Wordpress DB. You can also make use of REST web-service means then from CI you only need to call the url and which correspondingly provides the required data in json or any format that you like.
And for creating a web-service inside WordPress, you can make use of this plugin :
https://wordpress.org/plugins/json-api/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With