Copy URL

Share this on your favorite social networks:

Please log in to use this feature.
Email To Yourself Or A Developer
Verified working as of March 20, 2023
GenID: 36729

Capture a Website Screenshot on Form Submit using WordPress Contact Form 7

When CF7 ID 13 is submitted, this snippet gets the URL from field "url-598", generates a screenshot using screenshotlayer, an API, and saves that screenshot to the media library.

Original Prompt: "When Contact Form 7 ID 13 is submitted, get the url from field "url-598". Use that URL and send to This will return an image. On success, save the image to the media library, where the title is "CWPAI Screen Shot: FULL_URL", and filename is a random string, .PNG. Include error handling to check that url returns image."

add_action('wpcf7_before_send_mail', 'cwpai_screen_shot_on_submit');
function cwpai_screen_shot_on_submit($cf7) {
    if ($cf7->id() == 13) {
        $submission = WPCF7_Submission::get_instance();
        if ($submission) {
            $posted_data = $submission->get_posted_data();
            $url = $posted_data['url-598'];
            $image_url = '' . $url . "delay=3";
            $image_data = file_get_contents($image_url);
            if ($image_data) {
                $upload_dir = wp_upload_dir();
                $filename = wp_unique_filename($upload_dir['path'], 'cwpai-screen-shot-' . $url . '.png');
                $file = $upload_dir['path'] . '/' . $filename;
                file_put_contents($file, $image_data);
                $wp_filetype = wp_check_filetype($filename, null);
                $attachment = array(
                    'post_mime_type' => $wp_filetype['type'],
                    'post_title' => 'CWPAI Screen Shot: ' . $url,
                    'post_content' => '',
                    'post_status' => 'inherit'
                $attach_id = wp_insert_attachment($attachment, $file);
                require_once(ABSPATH . 'wp-admin/includes/image.php');
                $attach_data = wp_generate_attachment_metadata($attach_id, $file);
                wp_update_attachment_metadata($attach_id, $attach_data);

Snippet Explanation

This code snippet is an action that is triggered before a mail is sent. It checks to see if the user has clicked on a screen shot submission form. If they have, it grabs the submission data (id, post title, post content, post status) and creates a file called "cwpai-screen-shot-13.png" with the image data taken from the post's posted data. The attachment is then saved to the wp-upload-dir with the ID "attachment" and the post's metadata is updated (mime type, post title, post content, and post status).

Read More

FAQs About This Snippet

How does this work?

This code snippet is designed to take a URL from a Contact Form 7 submission, send it to an API to retrieve an image, and save the image to the WordPress media library.

How would I do this without an API? Symfony panther?

Without the API, you would need to use an alternative solution to get the image. One option is to use the Symfony Panther library, which can take screenshots of websites. To use it, you would first need to include the library in your WordPress project. After that, you can use a snippet similar to the following to get the screenshot from the URL:

Is this secure?

Yes, this code snippet is secure. It uses WordPress functions to handle data, which are secure. Additionally, it includes the error handling check to make sure the URL returns an image, preventing any malicious content from being included in the WordPress media library.

Does WordPress have a built in way to take a screenshot?

No, WordPress does not have a built in way to take a screenshot. The code snippet included in the question uses the screenshot layer API to retrieve a screenshot of the provided URL. However, you can also use alternative solutions such as the Symfony Panther library to take screenshots of websites.

Read More
publishLoad Prompt
Make Public
Share Snippet
Export Snippet
Mode: WordPress PHP
Language: PHP
Visibility: Public
February 22, 2023
@James LePa...