WordPress/Codeigniter Integration - Pass WP shortcode attribute to CI script WordPress/Codeigniter Integration - Pass WP shortcode attribute to CI script codeigniter codeigniter

WordPress/Codeigniter Integration - Pass WP shortcode attribute to CI script


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 requestcurl_setopt($post, CURLOPT_POST, count($post_data)); //Amount of POST fieldscurl_setopt($post, CURLOPT_POSTFIELDS, $post_data); //The POST data.curl_setopt($post, CURLOPT_RETURNTRANSFER, TRUE); //Returns the output from the requested scriptcurl_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/


I had to solve this using a completely different solution than any of the ones I mentioned above. The code below still needs to be tweaked, but you can get the idea.

I had to add this function to my WP plugin:

function save_shortcodes_to_db($content) { global $post; $postID = $post->ID; global $wpdb; if (has_shortcode($content, 'property_filters')) { $filterArr = array('area=', 'prop_type=', 'agent=', 'office='); foreach ($filterArr as $filter) { $filterpos = strpos($content,$filter); //63 if ($filterpos !== false) { $filterstrlen = strlen($filter); //10 $filterendpos = $filterpos + $filterstrlen - 1; $offset = $filterendpos; $valuestartpos = $filterendpos + 1; $endbracket = ']'; $endbracketpos = strpos($content,$endbracket,$offset); $valuelen = $endbracketpos - $valuestartpos; $meta_value = substr($content,$valuestartpos,$valuelen); $meta_key = 'rc_'.rtrim($filter,'='); $data = array('post_id' => $postID, 'meta_key' => $meta_key, 'meta_value' => $meta_value); $wpdb->insert('wp_postmeta', $data); } } } return $content;}add_filter( 'content_save_pre' , 'save_shortcodes_to_db' , 10, 1);

Then, in my CI controller index function, I added this:

global $wpdb; if ($this->session->userdata('referer')) { $pageurl = $this->session->userdata('referer'); $pos = strpos($pageurl,'listings'); if ($pos !== false) { $page_by_path = get_page_by_path($pageurl); $postid = $page_by_path->ID; $content = $wpdb->get_col("SELECT post_content FROM $wpdb->posts WHERE ID=".$postid.";"); $prop_type_meta_value = $wpdb->get_col("SELECT meta_value FROM $wpdb->postmeta WHERE post_id=".$postid." AND meta_key = 'rc_prop_type';"); $data['wp_content'] = $content[0]; } }

Basically, before the WP page is updated, my WP function saves some information from the shortcode(s) that were entered into the page by the end user to the postmeta table in WP. Then, my controller function retrieves the information, and uses it in the rest of the code. The only issues I still need to address are:

  • when the WP page is new, there will not be a post_id
  • I need to replace values in the WP postmeta table that have already been set for the page

I am sure these are easily fixed. As for a solution to the original question - set data in WP and retrieve it in CI - this answer is complete.