• Resolved onmas

    (@onmas)


    Hi everyone.

    This is my first post – I hope I’ve asked it correctly.

    I need to make a call from WordPress (using Ajax) out to a script (CALL_SHORTCODE.php below) to retrieve some data from a table. To do this, the 1st script needs to call another script (TEST_SHORTCODE.php) to get the login name of the user from a plugin.

    WP (ajax) –> CALL_SHORTCODE.php –> TEST_SHORTCODE.php

    I’m not getting any data back in CALL_SHORTCODE.php. However, if I just call TEST_SHORTCODE.php without calling it from CALL_SHORTCODE.php I get data I want (eh?). The following code will run.

    [CALL_SHORTCODE.php]

    <?php
      // Call our script to get who's logged in
      $jsonurl = "https://my.url.com/scripts/TEST_SHORTCODE.php";
      $json = file_get_contents($jsonurl);
      var_dump($json);
    ?>

    [TEST_SHORTCODE.php]

    <?php
      // Include WordPress
      // Tried using wp-blog-header.php as well to no avail
      include_once '../wp-load.php';
    
      // Get login name from Memberpress and return it
      $username = do_shortcode("[mepr-account-info field='user_login']");
      $rows[] = array( 'username'=>$username );
      echo json_encode($rows, JSON_NUMERIC_CHECK);
    ?>

    …Output from calling TEST_SHORTCODE.php (valid JSON):

    [{"username":"[email protected]"}]

    …However, the output from calling CALL_SHORTCODE.php is:

    string(17) "[{"username":""}]"

    …As you can see, there is no value in the output of CALL_SHORTCODE.php.

    This one is driving me bonkers ??

    Many thanks in advance
    Jason

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Jason

    Where is call_shortcode.php located?

    Thread Starter onmas

    (@onmas)

    Hi there.

    Thanks for getting back to me. Unfortunately, its behind a password protected server and I can’t provide access (much as I really want to as this is taking up loads of time). But the scripts are in the locations above (albeit with a spurious URL) and produce the output shown when called as:

    https://my.url.com/scripts/TEST_SHORTCODE.php
    https://my.url.com/scripts/CALL_SHORTCODE.php

    Its very strange.

    Jason

    Hi

    What is the value of $json (before the var_dump) in your CALL_SHORTCODE.php?

    Thread Starter onmas

    (@onmas)

    Hi again epicdevspace,

    Thanks for helping on this. Its just:

    [{“username”:””}]

    Jason

    Thread Starter onmas

    (@onmas)

    Hi again epicdevspace,

    I’ve had a response that provides the reason for the problem. When the client calls TEST_SHORTCODE.php directly, MemberPress knows its a client (perhaps via cookie or session variable) and can respond. However, if the client calls the CALL_SHORTCODE.php script and its the script that calls TEST_SHORTCODE.php then the call is coming from the server not the client.

    So I appear to have an explanation but no solution to my problem which will need a rethink.

    Thanks for taking the time to consider it. I’ll mark the question closed.

    Regards,
    -Jason

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Calling a plugin across two scripts’ is closed to new replies.