Home>
Problem question

Media uploeader was created in the user submission form of the plugin "Anspress". You can select and upload images here, but the values ​​are not saved in the posted article.
The following functions.php is a problem, and other than that (upload.js and anslist.php) are all right.

I am using "Anspress".
With this code I can upload the image but can not save it.
The problem is [functions.php] part.

Code Code

▼ [functions.php]

// Select image
function my_media_script () {
   wp_enqueue_media ();
}
add_action ('wp_enqueue_scripts', 'my_media_script');
// Media uploeader @ Anspress askform
function my_custom_question_field ($form) {
$form ['fields'] ['image'] = array (
  'html' =>'<div></div>
  <div method = "post">
    <div>
        <div>
            <img src = "http://sample.com/wp-content/uploads/2018/08/first-img.jpg">
        </div>
        <input name = "" type = "button" value = "select image">
        <input type = "hidden" name = "image1" value = "">
    </div>
  </div>',
);
return $form;
}
add_filter ('ap_question_form_fields', 'my_custom_question_field');
// Save
function question_mysave ($post_id, $post) {
global $validate;
if (empty ($validate)) {
return;
}
$fields = $validate->get_sanitized_fields ();
update_post_meta ($post_id, 'image', $fields ['image']);
}
add_action ('ap_processed_new_question', array ($this, 'question_mysave'), 0, 2);
add_action ('ap_processed_update_question', array ($this, 'question_mysave'), 0, 2);


▼ 【upload.js】

jQuery (document) .ready (function ($) {
    // Uploading files
    var file_frame;
    var wp_media_post_id = wp.media.model.settings.post.id;// Store the old id
    var set_to_post_id = '<? php echo $my_saved_attachment_post_id;?>';// Set this
    jQuery ('. upload_image_button'). on ('click', function (event) {
        event.preventDefault ();
        // If the media frame already exists, reopen it.
        btn_id = $(this) .attr ('id');btn_no = btn_id.replace ('upload_image_button-', '');
        if (file_frame) {
            // Set the post ID to what we want
            //file_frame.uploader.uploader.param ('post_id', set_to_post_id);
            // Open frame
            file_frame.open ();
            return;
        } else {
            // Set the wp.media post id so the uploader grabs the ID we want when initialised
            //wp.media.model.settings.post.id = set_to_post_id;
        }
        // Create the media frame.
        file_frame = wp.media.frames.file_frame = wp.media ({
            title: 'Select a image to upload',
            button: {
                text: 'Use this image',
            },
            multiple: false // Set to true to allow multiple files to be selected
        });
        // When an image is selected, run a callback.
        file_frame.on ('select', function () {
            // We set multiple to false so only get one image from the uploader
            attachment = file_frame.state (). get ('selection'). first (). toJSON ();
            // Do something with attachment.id and/or attachment.url here
            $('# image-preview-' + btn_no) .attr ('src', attachment.sizes.thumbnail.url) .css ('width', 100);
            $('#image_attachment_id-' + btn_no) .val (attachment.id);
            // Restore the main post ID
            //wp.media.model.settings.post.id = wp_media_post_id;
        });
            // Finally, open the modal
            file_frame.open ();
    });
    // Restore the main ID when the add media button is pressed
    jQuery ('a.add_media') .on ('click', function () {
        //wp.media.model.settings.post.id = wp_media_post_id;
    });
});


▼ 【anslist.php】

<figure>
    <? php
    $imageid = get_post_meta ($post->ID, 'image1', true);
    $url = wp_get_attachment_url ($imageid);
    echo '<img src = "'. $url. '" alt = "">';
    ?>
</figure>
Reference

Plugin page
https://en.wordpress.org/plugins/anspress-question -answer/

A page about this issue
https://anspress.io/resources/faq/anspress-form-and-validation-api/

  • Answer # 1

    $fields ['image']below looks like a mistake for$fields ['image1'].

    update_post_meta ($post_id, 'image', $fields ['image']);

    Since this may still cause validation errors, I think the following code is also necessary.

    function my_ap_ask_fields_validation ($args) {
        $args ['image1'] = array (
            'sanitize' =>array ('only_int'),
            'validate' =>array ('required'),
        );
        return $args;
    }
    add_action ('ap_ask_fields_validation', 'my_ap_ask_fields_validation');