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:
Then, you can create your controller to handle your post request 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 :
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:
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.