Denislav Popov
asked 10 years ago

Hello, 
I’m trying to extend functionality by assigning picture to every question wich should be visible on the question list page as thumbnail. 
So far i’m using the wordpress ‘media_handle_upload’ method (
<form id=”featured_upload” method=”post” action=”#” enctype=”multipart/form-data”>
<input type=”file” name=”my_image_upload” id=”my_image_upload” multiple=”false” />
<input type=”hidden” name=”post_id” id=”post_id” value=”55″ />
<?php wp_nonce_field( ‘my_image_upload’, ‘my_image_upload_nonce’ ); ?>
<input id=”submit_my_image_upload” name=”submit_my_image_upload” type=”submit” value=”Upload” />
</form>
<?php
if (
isset( $_POST[‘my_image_upload_nonce’], $_POST[‘post_id’] )
&& wp_verify_nonce( $_POST[‘my_image_upload_nonce’], ‘my_image_upload’ )
&& current_user_can( ‘edit_post’, $_POST[‘post_id’] )
) {
// The nonce was valid and the user has the capabilities, it is safe to continue.
// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . ‘wp-admin/includes/image.php’ );
require_once( ABSPATH . ‘wp-admin/includes/file.php’ );
require_once( ABSPATH . ‘wp-admin/includes/media.php’ );

// Let WordPress handle the upload.
// Remember, ‘my_image_upload’ is the name of our file input in our form above.
$attachment_id = media_handle_upload( ‘my_image_upload’, $_POST[‘post_id’] );

if ( is_wp_error( $attachment_id ) ) {
// There was an error uploading the image.
echo ‘Error uploading’;
} else {
// The image was uploaded successfully!
}
} else {
// The security check failed, maybe show the user an error.
}?>

in the question-submit-form.php and the file upload is successful, but i need guidance how to assign every picture to the relative question and how to display it on the question list page.
Thank you in advance.
Best regards,
Denislav!

4 Answers
DominicStaff
answered 10 years ago

Thanks for posting on our community 🙂 Unfortunately, the DWQA plugin doesn’t support this yet. We will send and notify our technical team your problem and your code snippet  as well. Will let you know as soon as possible.

Denislav Popov
answered 10 years ago

Thank you for answering. However i have some advancement and the  previous code is not good for accomplishing this task. As soon as i realised this is based on a custom post type i registered featured image for every question in the functions.php like trhis:

add_post_type_support('dwqa-question', array('thumbnail'));
add_theme_support('post-thumbnails');

 When featured image is assigned it can be shown to the question list by adding the code below in to the: /dw-question-answer\inc\templates\default\question-list.php + some css. applied to the .topic_image. The result is this: image

<div class="topic_image"><?php the_post_thumbnail('thumbnail');?></div>

However there are still 2 problems: 
1st when filters are used or there are more pages it seems some jquery loads the content and the page should be reloaded in order to question-list.php to be executed again and the images to be shown. I think i can fix this by tweaking ‘dw-question-answer\inc\templates\default\assets\js\dwqa-question-list.js’
2nd bigger problem is how to assign the featured image from the front end. So far i added the code below to ‘dw-question-answer\inc\templates\default\question-submit.php’ but not do the trick the result in front end is: image

<input type="file" name="imageFeatured" id="imageFeeatured"/>
<?php
            if ( ! empty( $_POST[ 'post-thumbnails' ] ) )
    $thumbnail_field = esc_html( $_POST[ 'post-thumbnails' ] );
else
    $thumbnail_field = 'imageFeatured';
if ( ! empty( $_FILES ) ) {
    foreach ( $_FILES as $file => $array )
        $newupload = insert_attachment( $file, $pid, $thumbnail_field );
}

//attachment helper function   
function insert_attachment( $file_handler, $post_id, $set_thumb = false ) {
    if ( UPLOAD_ERR_OK !== $_FILES[ $file_handler ]['error'] )
        return false; 

    require_once ABSPATH . 'wp-admin/includes/image.php';
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';

    $attach_id = media_handle_upload( $file_handler, $post_id );

    //set post thumbnail (featured)
    if ( $attach_id && $set_thumb )
        update_post_meta( $post_id, 'thumbnail', $attach_id );

    return $attach_id;
}   
?>

I’ll be happy if your technical team can figure out how can the featured image to be assigned  from the submit question page.
Regards, Denislav
 

Denislav Popov
replied 10 years ago

1st problem solved: in order to display the images and the filters to work well this code:
<div class="topic_image"><?php the_post_thumbnail('thumbnail');?></div>
should be not in the '\dw-question-answer\inc\templates\default\question-list.php' but in the '\dw-question-answer\inc\templates\default\content-question.php' around line 19.

Now remains only front-end featured image assign on posting question and the functionality will be done.

DominicStaff
answered 9 years ago

Awesome!!! Unfortunately, we are busy with our new projects, so that we can’t help you out right now. I send and notify our technical team and we will consider in the next versions of the plugin. Please stay tuned.

nemaproblema
answered 7 years ago

Can you explain it?

Allen
replied 7 years ago

Hi, at this time, the current version of the plugin can’t do that but it can be custom to have that feature.

Powered by DW Question & Answer Pro