skip to Main Content

I’m trying to replace file_get_contents() function by wp_remote_get() function, so my program works perfectly with file_get_contents(), but when I replace it with wp_remote_get() it’s not working any longer, and I get this error :

Fatal error: Uncaught TypeError: base64_encode(): Argument #1 ($string) must be of type string, array given

I tried other solutions that I found online, but it didn’t work with my case.
So how can I fix that ?
Here’s my php code :

define( 'ICON_PATH', plugins_url( 'imgsh-logo.svg', __FILE__ ) );
//define( 'BASE64_PLUGIN_ICON', base64_encode( file_get_contents(ICON_PATH) );
define( 'BASE64_PLUGIN_ICON', base64_encode( wp_remote_get(ICON_PATH)) );

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    Here's the answer :

    define( 'ICON_PATH', plugins_url( 'imgsh-logo.svg', __FILE__ ) );
    define( 'BASE64_PLUGIN_ICON', base64_encode(wp_remote_retrieve_body(wp_remote_get(ICON_PATH))) );
    

  2. The issue you’re encountering arises because wp_remote_get() returns an array, not a string like file_get_contents().

    To fix this, you need to extract the response body from the array returned by wp_remote_get().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search