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

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');

Read More

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.

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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/

  3. 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.