Title: Codeigniter with rest api
Last modified: July 23, 2018

---

# Codeigniter with rest api

 *  [oa123](https://wordpress.org/support/users/oa123/)
 * (@oa123)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/codeigniter-with-rest-api-2/)
 * Hi , I need to insert posts using WP REST API from a codeigniter website , here
   is my controller:
 *     ```
       public function tst()
           {
               require_once( 'c:/xampp/htdocs/2/wordpress/wp-load.php' );
               $this->load->view('tst');
   
           }
       ```
   
 * And this is the tst.php view content:
 *     ```
           <?php
           defined('BASEPATH') OR exit('No direct script access allowed');
           ?><!DOCTYPE html>
           <html>
   
           <head>
   
           <meta http-equiv="content-type" content="text/html; charset=utf-8" />
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   
           </head>
   
           <body>
          <?php
          // Register the script
           wp_register_script( 'wp-api', 'c:/xampp/htdocs/2/wordpress/wp-includes/js/wp-api.js' );
   
           wp_localize_script( 'wp-api', 'wpApiSettings', array(
               'root' => esc_url_raw( rest_url() ),
               'nonce' => wp_create_nonce( 'wp_rest' )
           ) );
           // Enqueued script with localized data.
           wp_enqueue_script( 'wp-api' );
   
               print_r( array(
                   'root' => esc_url_raw( rest_url() ),
                   'nonce' => wp_create_nonce( 'wp_rest' )
               ) );
   
           ?>
   
           <button type="button" onclick="myFunction()">Retrieve</button>
           <script>
   
           function myFunction() {
   
               $.ajax( {
               url: wpApiSettings.root + 'wp/v2/posts/1',
               method: 'POST',
               beforeSend: function ( xhr ) {
                   xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
               },
               data:{
                   'title' : 'Hello Moon'
               }
           } ).done( function ( response ) {
              alert( response );
           } );
           }
           </script>
           </body>
           </html>
       ```
   
 * This is the output for
 *     ```
       print_r( array(
           'root' => esc_url_raw( rest_url() ),
           'nonce' => wp_create_nonce( 'wp_rest' )
       ) );
       ```
   
 *  Array ( [root] => [http://localhost/2/wordpress/wp-json/](http://localhost/2/wordpress/wp-json/)[
   nonce] => 04864b371a )
 * The error I get is ReferenceError: wpApiSettings is not defined, for this line
   of code:
 *  url: wpApiSettings.root + ‘wp/v2/posts/1’,
 * If wpApiSettings gets a value in the php section code , why isn’t it defined 
   in the javascript section ?
    -  This topic was modified 7 years, 9 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
      Reason: code fixed
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcodeigniter-with-rest-api-2%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/codeigniter-with-rest-api-2/#post-10519898)
 * wp_localize_script() is not called within a callback to “wp_enqueue_scripts” 
   so the necessary output defining wpApiSettings never occurs.
 * You should not load jQuery from googleapis.com, WP has its own local version.
   Load it simply by specifying `[jquery]` as a dependency argument when enqueuing
   you JS script.
 * Please use backticks or the code button to demarcate your code, otherwise it 
   gets corrupted, making it difficult for others to test. I fixed your post for
   you this time.

Viewing 1 replies (of 1 total)

The topic ‘Codeigniter with rest api’ is closed to new replies.

## Tags

 * [CodeIgniter](https://wordpress.org/support/topic-tag/codeigniter/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/codeigniter-with-rest-api-2/#post-10519898)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
