WordPress / Codeigniter integration - pass WP shortcode attribute to CI script

I have successfully integrated WordPress with Codeigniter. I've 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 the variable POST

from my WP plugin to the CI script, hence the line:

$_POST['property_type'] = $value;

I understand that the return value of the function that processes the shortcode is intended to replace the shortcode with some text or widgets, etc. in Page / Post. I am planning to replace the shortcode with an empty string. But in the function that handles the POST

shortcode , how can I send the variable to my Codeigniter script?

I was looking for a watch on this. This seems to be a very specific question. Your help is appreciated.

EDIT: I thought about using session variables to store the value, but it doesn't seem like I can set a session variable in WP and access it from CI. Any suggestions for this line of thought?

EDIT 2: I also had the idea to query the WP database from a CI script using $ wpdb. This can be done and already works in some scenarios, however I cannot get the field post_content

directly from the WP database, instead I get the rendered text. Those. 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.

+3


source to share


3 answers


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

I had to add this feature 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 the following:



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 refreshing the WP page, my WP function saves some information from the shortcode (s) that were entered into the page by the end user into the postmeta table in WP. Then my controller function fetches the information and uses it in the rest of the code. The only problems I still need to solve are the following:

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

I am sure they are easily fixed. As for solving the original question - set the data in WP and extract it in CI - this answer is complete.

+1


source


If you want to send POST data directly to the CodeIgniter script, you can use the PHP cURL library (make sure it is installed on your web server).

Important: First you need to disable CodeIgniter's CSRF validation. You can disable the entire infrastructure or create a preliminary system glitch to disable CSRF on a specific controller.

Here's 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 on the PHP cURL library, you can read the manual .

Sincerely.

+2


source


Just to complement your approach to finding content from Wordpress DB. You can also use a REST webservice and then from the CI you only need to call the url and which will accordingly provide the required data in json or whatever format you like.

And to create a web service inside WordPress, you can use this plugin:

  

https://wordpress.org/plugins/json-api/

  
+2


source







All Articles