Home Forums Backend Issues (wp-admin) Can’t update repeater field via external PHP file


Can’t update repeater field via external PHP file

  • Hi there. I’m trying to update a user repeater field via an external PHP file but am not having any luck. My main file contains this script:

     url: 'http://localhost:8888/wordpress/wp-content/themes/cookable/write-course-to-user.php',data:{"userIDdata":finaluserid, "courseURLdata":tidycourseurlupdate},
     success: function(data) {

    And then write-course-to-user.php contains:

    $userIDfinal = isset($_REQUEST['userIDdata'])?$_REQUEST['userIDdata']:"";
    $courseURLfinal = isset($_REQUEST['courseURLdata'])?$_REQUEST['courseURLdata']:"";
    echo $userIDfinal;
    echo $courseURLfinal;
    echo "user_{$userIDfinal}";
      $field_key = "field_61e9bb8b66765";
      $value = get_field($field_key, "user_{$userIDfinal}");
      echo $value;
      $value[] = array("course_slug" => $courseURLfinal);
      update_field( $field_key, $value, "user_{$userIDfinal}");

    I get an error:

    PHP Fatal error: Uncaught Error: Call to undefined function get_field() in /Applications/MAMP/htdocs/wordpress/wp-content/themes/cookable/write-course-to-user.php:10 Stack trace: #0 {main}

    And I presume this means that the WP/ACF functionality isn’t present in the external PHP file (write-course-to-user.php).

    I tried adding require('http://localhost:8888/wordpress/wp-load.php'); to the beginning of this PHP with the aim of initialising WP functionality but then I received this error:

    Fatal error: require(): Failed opening required ‘http://localhost:8888/wordpress/wp-load.php&#8217; (include_path=’.:/Applications/MAMP/bin/php/php7.4.21/lib/php’) in /Applications/MAMP/htdocs/wordpress/wp-content/themes/cookable/write-course-to-user.php on line 3

    I’m at a bit of a loss as to what to try next and would be incredibly grateful for any clues as to how to get this script running. Thanks and sorry, I’m a total PHP noob.

  • I am assuming that you are calling your script using AJAX. There is a lot that needs to be loaded for WP and ACF to work when using AJAX. If you want to do this then I would suggest searching for “wordpress ajax without admin-ajax.php”

    The most useful documentation on using the correct way to do AJAX with WP is here.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.