I'm creating a new field type with the Adavanced Custom Fields, which enables the user to choose an image from the Image Library and define a point (X,Y coordinates) in this image. After defining the coordinates, the user can place an overlay video in this point. But my question is, how do I Get and Set an X and Y value in the database with the javascript API?
-
I think you should save an JSON array with the file identifier, x position and y position.Kaperto– Kaperto2019-12-08 00:21:15 +00:00Commented Dec 8, 2019 at 0:21
-
your problem is getting the X,Y or saving to DB ?Obmerk Kronen– Obmerk Kronen2019-12-08 03:11:47 +00:00Commented Dec 8, 2019 at 3:11
-
@ObmerkKronen yes, I've made the JS code for defining the X/Y coordinates and now my question is, how do I save them to the database ... and how do I retrieve them when accessing the field again.Kasper Gantzhorn– Kasper Gantzhorn2019-12-08 08:37:46 +00:00Commented Dec 8, 2019 at 8:37
-
well, if you use ACF tan you just populate the fields, it will be saved with all the other data and will be retrieved with field name..Obmerk Kronen– Obmerk Kronen2019-12-08 10:18:32 +00:00Commented Dec 8, 2019 at 10:18
-
1@ObmerkKronen and I tried acf.set("posX", "123"); It works fine, but it does not save it in the database.Kasper Gantzhorn– Kasper Gantzhorn2019-12-08 16:12:14 +00:00Commented Dec 8, 2019 at 16:12
|
Show 2 more comments
1 Answer
Okay, so @ObmerKronen suggested that I posted some code to demostrate what I'm trying to do ... makes sense:)
I've added the image in the new ACF field php file (class-tas-v5.php)
$imgID = get_field('image_url');
$img = wp_get_attachment_image( $imgID, "full", "", array( "class" => "overlay-point-img" ) );
echo '<div class="overlay-point-img-container">' . $img . '</div>';
Then I'm defining the X and Y coordinates in the input.js file
function initialize_field($field) {
var overlay_point_container = $field.context;
var img = $(overlay_point_container).find(".overlay-point-img-container");
var circle = '<div class="overlay-point-circle"></div>';
img.prepend(circle);
$(img).on("mousemove", function(event) {
var relX = event.pageX - $(this).offset().left;
var relY = event.pageY - $(this).offset().top;
$(circle).css({ left: relX, top: relY });
});
// Tried these two functions, but none of the values are saved in the database
// acf.set("posX", "123");
// $field.val("posX");
}
I've tried to save it with dummy data like this
acf.set("posX", "123");
$field.val("posX");
1 Comment
Pete Harrison
Did you resolve this? I'm trying to achieve something like this to document people in an image.