I'm working on a custom theme that allows you to upload multiple images on one page. Since I have multiple file inputs, I use this function in myfunctions.php
file (inside a loop for each file input):
register_setting('hero', 'slider_slide_' . $i . '_image',
function($option) use ($i) {
if (!empty($_FILES['slider_slide_' . $i . '_image']['tmp_name'])) {
$_FILES['slider_slide_' . $i . '_image']['name'] = 'slide_' . $i . '.png';
$urls = wp_handle_upload($_FILES['slider_slide_' . $i . '_image'], array('test_form' => FALSE));
$temp = $urls['url'];
return $temp;
}
return $option;
});
The inline function is the only thing I've tried that works for multiple files at a time. Either way, I'm now facing the issue of having my options being overwritten. Once uploaded, the option is set in my database, with the correct link to the image. However, if I change other settings on the page later, since nothing is uploaded, the option is overwritten with a blank string, despite me checking for the file not being empty before updating the option.
Ideally I'd like to allow a user to change anything and everything and check the file upload to check if anything is selected, and if so, upload and update the option.
Only problem is, Wordpress seems to be dealing with the updating of the options...
No errors are being thrown either, so I'm totally stumped right now.
I assume that WP is still receiving your empty file inputs, so you need to return the stored value.
Try something like that.
register_setting('hero', 'slider_slide_' . $i . '_image',
function($value, $option) use ($i) {
if (empty($_FILES['slider_slide_' . $i . '_image']['tmp_name'])) {
return get_option($option);
}
$_FILES['slider_slide_' . $i . '_image']['name'] = 'slide_' . $i . '.png';
$urls = wp_handle_upload($_FILES['slider_slide_' . $i . '_image'], array('test_form' => FALSE));
return $urls['url'];
});
Another way is to fill'slider_slide_' . $i . '_image'
input with the stored value.
Daniel G. above this answer has pretty much solved it, I had to adjust it a little, but for the sake of anyone else who has this problem, here's the code I used to fix it.
register_setting('hero', 'slider_slide_' . $i . '_image',
function($option) use ($i) {
if ($_FILES['slider_slide_' . $i . '_image']['error'] != 0) {
return get_option('slider_slide_' . $i . '_image');
}
$_FILES['slider_slide_' . $i . '_image']['name'] = 'slide_' . $i . '.png';
$urls = wp_handle_upload($_FILES['slider_slide_' . $i . '_image'],
array(
'test_form' => FALSE,
'unique_filename_callback' => 'file_override_overwrite'
)
);
$temp = $urls['url'];
return $temp;
}
);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.