• I am creating a plugin MICplugins.php used with just one page. Per Block Editor Handbook/Loading JavaScript instruction, this much works fine, including this snippet:

    function micDitchHealth_enqueue() {
            wp_enqueue_script(
               'micDitchHealth-script', // script name
                // micDitchHealth.js in same folder as this .php file.
                plugins_url( 'MICplugins.js', __FILE__ ),
                array( 'wp-blocks' )
            );
    }
    add_action( 'enqueue_block_editor_assets', 'micDitchHealth_enqueue' );

    Here’s a snippet from the MICplugins.js file the .php file points to. Note the wp_remote_get(…) example URL call:

    console.log( "micDitchHealth-script loaded" );
    $response = wp_remote_get( 'https://www.google.com' );

    At the time a new Post (or page) is created, the MICplugins.js file loads along with the Post, but Inspect / Console (using Chrome browser) reports “Uncaught ReferenceError: wp_remote_get is not defined”. I see that this function is defined in wp-includes/http.php, but obviously I’m missing something to link the definition to a call of it. I presumably will have the same problem with any other js function from the wp core library or related API libraries.

    What am I missing?

    Any help for this newbie is greatly appreciated. Thank you in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi! It seems that you are using a PHP function (wp_remote_get) inside your JavaScript code.

    Thread Starter moabmike579

    (@moabmike579)

    Javier, thanks so much! I now call wp_remote_get($url) directly from MICplugins.php; works fine.

    However, if I may ask another WP 101 question:

    Suppose I have an ordinary text file, local rather than remote, call it parameters.csv, in my Plugin folder along with MICplugins.php itself. I wish to read its contents into a PHP string, then together with wp_remote_get($url)’s contents and date/time, construct my web page’s contents.

    But I can find no PHP method listed in codex.ww.wp.xz.cn/Function_Reference with which to do a local text read into a string, such a common thing. What have I missed?

    I do however see in https://developer.ww.wp.xz.cn/reference/functions/ the method get_file($path) whose sources are found in wp-admin/includes/noop.php. Can or should I use this method? plugins_url() generates the correct path to parameters.csv, but get_file($path) fails; don’t know why.

    Thank you again, Moab Mike D

    > However, if I may ask another WP 101 question

    I’ll try to help you, but could you please open a new topic for your last question? That will help people with the same problem as you in the future. Also, please mark this issue as solved.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Uncaught ReferenceError: wp_remote_get() is not defined’ is closed to new replies.