0

I want to use some of the Wordpress API built in functions such as wp_remote_request(). I have tried using this in a php file in my root Wordpress installation but I am just getting an error:

Fatal error: Call to undefined function wp_remote_request() in /home/pacekuwa/public_html/gf.php on line 23

I have a feeling I am not putting my code in the right place or need to include one or more Wordpress files...

Could anyone tell me where I should be putting my code?

EDIT:

I am trying to create a page that makes a remote API call to my Gravity Forms plugin to retrieve submitted form entries using the Gravity Forms Web API. I was trying to follow their example, but don't know where the code they give should go within my installation to get it to work.

  • Why you are using WordPress function in a separate file ? – Sumit Feb 19 '16 at 11:30
  • Hi Sumit, can you tell me what file I should put the code in, in my Wordpress installation? – Ben Feb 19 '16 at 14:33
  • It's depends on what you are posting ? Please edit your question and describe what you are trying to do with wp_remote_request – Sumit Feb 19 '16 at 15:24
  • @Sumit - I have added a further explanation to my quiestion. – Ben Feb 19 '16 at 18:24
0

You can create a plugin to contain your code. The admin_post_ action can be used to trigger a request handler. WordPress core will be loaded, and the API will work.

<?php
/*
Plugin Name: Ben's GF Plugin
*/

// for logged-in users
add_action( 'admin_post_ben_gf', 'ben_gf_handle_request' );
// for non logged-in users
add_action( 'admin_post_nopriv_ben_gf', 'ben_gf_handle_request' );

function ben_gf_handle_request() {

    // Your processing code.
    // WordPress functions will work here.
    echo 'Your home url: ' . home_url();

    // die() at the end to terminate execution
    die();
}

This will map your request handler to the URL:

http://www.example.com/wp-admin/admin-post.php?action=ben_gf

Use caution if you are allowing non-logged in users to access this URL, you don't want to publicly expose sensitive data.

  • Thank you very much, that is a very helpful explanation, and much appreciated. – Ben Feb 20 '16 at 11:01
1

Wordpress functions are used in theme or plugin files. If you want to use wordpress functions in custom php file you have to include wp-load.php file. require_once("../../../../wp-load.php"); . Number of dots depends where is your custom file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.