admin管理员组

文章数量:1336643

I have a website where users can submit posts (the posts must have a URL of a website). I would like to use a screenshot of that URL to be the featured image. I have used this code to generate the screenshot: / and I found this code to upload the screenshot / but am not sure how to merge the two to do what I need. Any advice? Should I do this in functions.php or as part of the post submission form itself?

I have a submission form (front-end) to add a custom post called Tools (which has a URL) as one field):

<?php $mailer = new PG_Custom_Form_Mailer(); ?>
<?php $mailer->process( array(
        'form_id' => 'submit_tool_form_mailer_id',
        'send_to_email' => true,
        'save_to_post_type' => true,
        'email' => '[email protected]',
        'post_type' => 'tools',            
        'success_message' => 'Thank you for your submission! We will get back to you as soon as possible. ',
        'error_message' => 'Unfortunately, something went wrong. Please try again.',
        'captcha' => true,
        'captcha_key' => 'xxx',
        'captcha_secret' => 'xxx-x'
) ); ?>
<?php if( !$mailer->processed || $mailer->error) : ?>
    <form role="form" action="<?php echo '#submit_tool_form_mailer_id'; ?>" method="post" onsubmit="event.stopImmediatePropagation();event.stopPropagation();"> 
        <div class="form-group"> 

            <?php 
                global $current_user;
                get_currentuserinfo();
                if ( is_user_logged_in() ) { ?>
                    <input type="hidden" class="form-control" id="submit_tool_yourname" placeholder="Your Name" required name="submit_tool_yourname" value="<?php echo $current_user->user_login; ?>">
                <?php } else { ?>     
                    <label class="control-label " for="submit_tool_name">
                        <?php _e( 'Full Name', 'impact_theme' ); ?>
                    </label>                     
                    <input type="text" class="form-control" id="submit_tool_yourname" placeholder="Your Name" required name="submit_tool_yourname" value="<?php echo ( isset( $_POST['submit_tool_yourname'] ) ? $_POST['submit_tool_yourname'] : '' ); ?>">
                <?php } ?>
        </div>
        <div class="form-group"> 

                <?php  if ( is_user_logged_in() ) { ?>                   
                    <input type="hidden" class="form-control" id="submit_tool_email" placeholder="Your email" name="submit_tool_email" value="<?php echo $current_user->user_email; ?>">
                <?php } else { ?>                    
                    <label class="control-label " for="submit_tool_email">
                        <?php _e( 'Your Email address', 'impact_theme' ); ?>
                    </label> 
                    <input type="email" class="form-control" id="submit_tool_email" placeholder="Your email" name="submit_tool_email" value="<?php echo ( isset( $_POST['submit_tool_email'] ) ? $_POST['submit_tool_email'] : '' ); ?>">
                <?php } ?>
        </div>
        <div class="form-group"> 
            <label class="control-label " for="post_title">
                <?php _e( 'Site / Tool Name', 'impact_theme' ); ?>
            </label>                 
            <input type="text" class="form-control" id="post_title" placeholder="Site / Tool Name" required name="post_title" value="<?php echo ( isset( $_POST['post_title'] ) ? $_POST['post_title'] : '' ); ?>"> 
        </div>
        <div class="form-group"> 
            <label class="control-label " for="url">
                <?php _e( 'Site / Tool URL', 'impact_theme' ); ?>
            </label>                 
            <input type="url" class="form-control" id="url" placeholder="Site / Tool URL" required name="url" value="<?php echo ( isset( $_POST['url'] ) ? $_POST['url'] : 'https://' ); ?>"> 
        </div>
        <div class="form-group"> 
            <label class="control-label " for="developers">
                <?php _e( 'Site / Tool Developer Name(s)  (Leave blank if unknown)', 'impact_theme' ); ?>
            </label>                 
            <input type="text" class="form-control" id="developers" placeholder="Site / Tool Developer Name(s)" name="developers" value="<?php echo ( isset( $_POST['developers'] ) ? $_POST['developers'] : '' ); ?>"> 
        </div>  
        <div class="form-group"> 
            <label class="control-label " for="github">
                <?php _e( 'Open Source Code / Github Link (Leave blank if unknown)', 'impact_theme' ); ?>
            </label>                 
            <input type="url" class="form-control" id="github" placeholder="Github Link" name="github" value="<?php echo ( isset( $_POST['github'] ) ? $_POST['github'] : 'https://' ); ?>"> 
        </div>             
        <div class="form-group"> 
            <label class="control-label " for="post_content">
                <?php _e( 'Description of the Tool', 'impact_theme' ); ?>
            </label>                 
            <textarea class="form-control" rows="5" id="post_content" placeholder="Description" name="post_content"><?php echo ( isset( $_POST['post_content'] ) ? $_POST['post_content'] : '' ); ?></textarea>
        </div>
        <button type="submit" class="btn btn-block btn-outlined btn-primary">
            <?php _e( 'Submit', 'impact_theme' ); ?>
        </button>             
        <input type="hidden" name="submit_tool_form_mailer_id" value="1"/>
        <div class="g-recaptcha" style="margin:10px 0;" data-sitekey="<?php echo 'xxxx' ?>"></div>
        <input type="hidden" name="submit_tool_form_mailer_id" value="1"/>
    </form>
<?php endif; ?>
<?php if( $mailer->processed ) : ?>
    <?php echo $mailer->message; ?>
<?php endif; ?>

and a php function to handle the form submissions (back-end):

class PG_Custom_Form_Mailer {

public $processed = false;
public $error = true;
public $message = 'The form was not submitted';
public $text = null;
public $html = null;

public function process( $arg_options = array() ) {

    $admin_email = get_option('admin_email');        

    $options = array(
        'form_id' => 'submit_tool_form_mailer_id',
        'send_to_email' => true,
        'email' => $admin_email,
        'title' => 'New Tool Submission',
        'intro' => 'We received a new tool submission:',
        'save_to_post_type' => true,
        'post_type' => 'tools',
        'captcha' => false,
        'captcha_key' => null,
        'captcha_secret' => null,
        'log_ip' => true,
        'success_message' => 'Thank you for letting us know!',
        'error_message' => 'There was a problem submitting this form. Please contact us directly.'
    );

    //merge options
    foreach($arg_options as $key => $value) {
        $options[ $key ] = $value;
    }

    if( !empty($_POST[$options['form_id']]) ) {
        //the form was submitted
        //we assume the browser did the validation
        $lf = "\n\r";

        $ignore_fields = array($options['form_id'], 'g-recaptcha-response');

        $text = $options['intro'].$lf.$lf;
        $html = "<p>{$options['intro']}</p>";

        $from_email = null;

        $this->processed = true;

        // code for email removed

        $postID = null;
        $content = null;
        $content = sanitize_textarea_field($_POST['post_content']). '<br> [snapshot url="'.esc_url_raw($_POST['url']).'" width="800" height="600"]';

        if($options['save_to_post_type']) {
            if($postID = wp_insert_post( array(  
                'post_title' => sanitize_text_field( $_POST['post_title']),
                'post_content' => $content,                    
                'post_type' => 'tools',                 
                'post_status' => 'draft'
            ) )) {
                $saved = true;
                update_field('url', esc_url_raw($_POST['url']), $postID); // update ACF fields
                update_field('site_name', sanitize_text_field($_POST['post_title']), $postID);
                update_field('developers', sanitize_text_field($_POST['developers']), $postID);                         
                update_field('github', esc_url_raw($_POST['github']), $postID);    


                    // Add Featured Image to Post            
                    $src = '/' . urlencode( $_POST['url'] ) . '?w=800&h=600' ;

                    $image_url        = $src; // Define the image URL here
                    $image_name       = sanitize_text_field($_POST['post_title']);
                    $upload_dir       = wp_upload_dir(); // Set upload folder
                    $image_data       = file_get_contents($image_url); // Get image data
                    $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
                    $filename         = basename( $unique_file_name ); // Create image file name

                    // Check folder permission and define file location
                    if( wp_mkdir_p( $upload_dir['path'] ) ) {
                        $file = $upload_dir['path'] . '/' . $filename;
                    } else {
                        $file = $upload_dir['basedir'] . '/' . $filename;
                    }

                    // Create the image  file on the server
                    file_put_contents( $file, $image_data );

                    // Check image file type
                    $wp_filetype = wp_check_filetype( $filename, null );

                    // Set attachment data
                    $attachment = array(
                        'post_mime_type' => $wp_filetype['type'],
                        'post_title'     => sanitize_file_name( $filename ),
                        'post_content'   => '',
                        'post_status'    => 'inherit'
                    );

                    // Create the attachment
                    $attach_id = wp_insert_attachment( $attachment, $file, $post_id );

                    // Include image.php
                    require_once(ABSPATH . 'wp-admin/includes/image.php');

                    // Define attachment metadata
                    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );

                    // Assign metadata to attachment
                    wp_update_attachment_metadata( $attach_id, $attach_data );

                    // And finally assign featured image to post
                    set_post_thumbnail( $post_id, $attach_id );


            } else {
                $saved = false;                    
            }                    

        }

        if((!$emailed && !$saved) || $emailed === false || $saved === false) {
            $this->error = true;
            $this->message = $options['error_message'];
        } else {
            $this->error = false;
            $this->message = $options['success_message'];
        }

        return true;
    } else {
        //the form was not submitted
        $this->processed = false;
        $this->error = false;
        return false;
    }

}

public function validate_rechapcha($response, $secret)
{
    // irrelevant code
}

}

I think that I am supposed to adapt the code from / to my last function above in the area "if($options['save_to_post_type'])" but I'm not quite sure how to do it. Any advice to point me in the right direction so I can try a few things and update what worked / what did not?

本文标签: phpGenerate and upload screenshot as featured image for usersubmitted post