Closed cfab closed 4 months ago
Try adding something like this in top of people.php:
use function App\myFunction;
Then you can just use that function anywhere in people.php, like this:
myFunction()
Note, if you´re gonna use a Class instead of just a function, you write it like this instead:
use App\myClass;
Good luck!
It works indeed, I must have something yesterday because I was thinking I tried that. but it doesn't work all functions... For example this one works if situated in api.php with use function...
function load_json_file($file)
{
// Define the path of the JSON file.
$json_file = ABSPATH.'wp-content/uploads/mme/apitest/'.$file.'.json';
// Check if the file exists.
if (file_exists($json_file)) {
// Get the file contents.
$file_contents = file_get_contents($json_file);
// Attempt to decode the contents as JSON.
// $data = json_decode($file_contents, true); // true to return an array
$data = json_decode($file_contents); // false to return an object
if (json_last_error() === JSON_ERROR_NONE) {
// If decoding was successful, $data now contains the data from the JSON file.
return $data;
} else {
// If the JSON was invalid, handle the error.
echo 'Error: Invalid JSON in file.';
return false;
}
} else {
// Handle the case where the file does not exist.
echo 'Error: File does not exist.';
return false;
}
}
but the next one which I need to build a select options only works if situated in the people.php file with $this-> in front
public function createArrayForSelect($type)
{
$items = load_json_file($type);
$array = [];
foreach ($items as $item) {
$array['id_'. $item->id] = $item->name;
}
return $array;
}
More precisely it only doesn't work here:
->addRepeater('curated_people')
->addSelect('person', [
'label' => 'Choisir une personne',
'return_format' => 'array',
'choices' => createArrayForSelect('people'),
])
->endRepeater()
but if I comment the choices line and add it here it returns correctly...
public function people_collection()
{
$people_collection = [
'choices'=>createArrayForSelect('people'),
'title' => get_field('title') ?: $this->example['title'],
];
return $people_collection;
}
Any help welcome !
I suggest asking ChatGPT about some of this stuff as it boils down to properly using classes/functions. Otherwise, you can use functions/classes inside of Composers as you would any other file.
Not an issue here but a question ! Within a block file create with "wp acorn" ( wp-content/themes/simply/app/Blocks/people.php) the generated namespace is App\Blocks. The block is working but with a lot of duplicated code that I already have in a file called api.php that I have created next to filters.php, setup.php called api.php (I'm building a root sage 10 based theme). The functions in this api.php file works perfectly when used into some ressources/views/files (usage like this: App\myFunction() ). Those functions would be very useful in my people.php file. The namespace of this api.php file is App (like setup.php and filters.php). I've tried to use them in people.php with App\myFunction() or \App\myFunction() or with use function App\myFunction without success ! Is it possible ? how can I use those functions in my /Blocks/people.php file ? Thank you !