• 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] => https://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 6 years, 7 months ago by bcworkz. Reason: code fixed

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    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.