I am building my first custom field type and its not so easy as I first thought. I cant seem to find much information on this subject in the forum, fb etc.
I am basically building a input field file which allows the user to upload a single or multiple images using FileReader (Web API) and output them as base64 so I can pass the data on .
This is a general demo I found which I am basing my code off https://bootsnipp.com/snippets/2eNKz. The demo allows you to upload images and reorder them, but generates a base64 preview of the image locally within the browser.
Example of where I am at is below, any help would be great.
It is correct to enqueue the script file for the field.
Please be aware that the field value saved in the database is the image ID, it is different from the raw code. I think after get the image URL, you can follow this topic to encode the URL https://wordpress.stackexchange.com/questions/288667/need-to-convert-image-url-to-a-base-64-data-url-with-wordpress-function
I'm not exactly sure what you mean, how to I get Metabox to save the input field value? e.g. file1.jpg, file2.jpg, file3.jpg?
forgot to mention, how to I pass the value e.g. file1.jpg to sanitize it before saving the value to the database?
Please follow this documentation to know how to create a custom sanitize callback and use it when creating a field.
Thanks for the information. I have managed to get the sanitize callback working. There is just one more thing I need to do.
I need to be able to upload the images to a custom folder when the user submits the form. Is there a hook or callback I can use to initiate before sanitization?
I'm afraid that there is no hook to upload images to the custom folder. Meta Box supports field type
file to allow the user to upload images to the custom folder. Please read more here https://docs.metabox.io/fields/file/
So I think you can extend this field type to use this feature.
I am not looking to upload the images to WordPress. But to send the base64 data to a 3rd party API after the user submits the form. So I just need to be able to execute a custom function of my own on form submission. Is this possible?
sorry I was not very clear before. Hope the above helps?
If you want to execute a function after the users submit the form, you can refer to these hooks
- for both backend and frontend: https://docs.metabox.io/actions/#rwmb_after_save_post
- for the form on the frontend: https://docs.metabox.io/extensions/mb-frontend-submission/#form-actions
ok thanks, I will use this then https://docs.metabox.io/extensions/mb-frontend-submission/#form-actions