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) {
$('#result3').html(data);
}
});
And then write-course-to-user.php contains:
<?php
$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’ (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.