Embed a Cakephp form into WordPress

I have a WordPress blog and I’m developing a backend application using Cakephp.

I want to develop the blog’s contact form using cake (since the entered information will be available from the backend app).

Read More

Right now, I tried including the cake view into wp using ajax. The problem with this approach is that I either use a Js->submit, which makes attaching files to the form quite complicated, or I use a Form->submit, which makes displaying validation errors problematic. Also, it creates problems with the recaptcha plugin not showing up.

Is there a way of integrating the form using php? I don’t need authentication (it is a public form), but I need to be able to show validation errors on the form and upload files on the form.

Related posts

Leave a Reply

2 comments

  1. Actually, you can load any website into a string using CURL, if you load en empty page with a placeholder from your wordpress, you can then use it as your layout

    <?php 
    $url = "http://www.yourdomain.com/emptypage";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $output = curl_exec($curl);
    curl_close($curl)
    
    $placeholder = "{{CONTENT}}"
    $placeholderPos = strpos($output,$placeholder);
    
    $beginning = substr($output,0,$placeholderPos);
    $end = substr($output,$placeholderPos+strlen($placeholder));
    
    echo $beginning;
    
    ////// your form ////// 
    
    echo $end;
    
    ?>
    

    You may have to deal with relative path after that, but this should get you started.

  2. So, I finally found a way I think is the best since it uses cake classes.

    I created a file new_file.php on webroot that is basically a copy of index.php but instead of using a generic request it requests the specific page I’m looking for:

    $Dispatcher = new Dispatcher();
    $Dispatcher->dispatch(new CakeRequest('CONTROLLER/ACTION'), new CakeResponse(array('charset' => Configure::read('App.encoding'))));
    

    Of course, you’ll have to change ‘CONTROLLER/ACTION’ to whatever your controller is.

    After that, you only have to include the file in a WordPress plugin.

    There is only one more change. There are some methods that conflict with WP declarations. PHP will complain when you include the above file. You have to find those method and wrap them into an if to make sure the method is not redeclared. This can break some functionality like localization (function __()) but it is ok if what you are including is not a very complex cake application.

    if (!function_exists('methodName')) {
    

    Hopefully the last issue will be solved once Cake 3 is out with namespaces support.