I
I
Ivan2018-09-10 23:51:34
WordPress
Ivan, 2018-09-10 23:51:34

How to connect wp functions to your php file?

I created a WP plugin, in the plugin folder I created a second php file that executes a post cURL request and direct access is allowed to it .

require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

$body = array(
    'id' => '1',
    'user_id' => '100'
);

$args = array(
    'body' => $body
);

$response = wp_remote_post( 'https://site.ru/test.php', $args );
$body = wp_remote_retrieve_body( $response );
echo $body;

How to do without connecting the string require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');?
Wrote in WP, I was sent to a page with recommendations:
If you need to have 'pages' directly accessible by an external service, then you must use query_vars and/or rewrite the rules to create a virtual page that calls the function.
Please see the plugins API reference for more information: codex.wordpress.org/Plugin_API
if you are trying to use AJAX please read this: codex.wordpress.org/AJAX_in_Plugins
For other possibilities, or better understand why we don't allow it, read this: ottopress.com/2010/dont-include-wp-load-please

I read it, I feel that there is a solution to my problem, but I did not understand how to implement it. In the example, we are talking about connecting css and js, I couldn’t find out anything useful there, I realized that it require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');’s impossible to connect, because. this loads the entire wp system and the file path may be different for different wp users.
Help to understand, please. How can I change the script so that it doesn't load the wp core all the time?
If the second php file is connected to the main plugin file, a cURL post request is immediately executed and the result of cURL execution is displayed instead of the entire site.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-09-11
@swede2k

Use CURL?

$curl = curl_init();
 
$url = "https://site.ru/test.php";
 
$args = array(
  'id' => '1',
  'user_id' => '100'
);
 
curl_setopt_array($curl,
    array(
  CURLOPT_URL => $url,
  CURLOPT_POST       => true,
        CURLOPT_POSTFIELDS => $args,
        CURLOPT_RETURNTRANSFER     => true,
    )
);
$data = curl_exec($curl);
curl_close($curl);
echo $data;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question