Custom Field Generate URL Screenshot
- This topic has 4 replies, 2 voices, and was last updated 5 years, 11 months ago by
@mindspark.
-
AuthorPosts
-
December 10, 2018 at 6:42 AM #12566
@mindspark
ParticipantAnh - I found this code snippet and wondered if this could be modified to work with MB? Turns out that there is a third-party ACF addon that does the same thing...
add_shortcode('ss_screenshot', 'ss_screenshot_shortcode'); function ss_screenshot_shortcode($atts){ $width = intval($atts['width']); $width = (100 <= $width && $width <= 300) ? $width : 200; $site = trim($atts['site']); if ($site != ''){ $query_url = 'http://s.wordpress.com/mshots/v1/' . urlencode($site) . '?w=' . $width; $image_tag = '<img alt="' . $site . '" width="' . $width . '" src="' . $query_url . '" />'; echo '<a href="' . $site . '">' . $image_tag . '</a>'; }else{ echo 'Bad screenshot URL!'; } }
My guess is I would first have to create a special field type for this to work, so I would appreciate help with that too if this is possible.
December 10, 2018 at 9:08 AM #12569Anh Tran
KeymasterHi Neil,
This snippet creates a shortcode to include the screenshot of a site. It works anywhere, with any plugin. What do you want to do with that?
May 16, 2019 at 5:40 AM #14578@mindspark
ParticipantHi Anh - it's been a while since opened this topic so I hope it's not too late to add to the thread...
the goal of the code snippet is to generate a screenshot from a URL entered into a form field.
Scenario - user enters data into a form that populates a custom template. the custom template displays the data collected in the form. one of the form fields is a URL field. in the custom template, instead of displaying only the URL field value (ex - https://google.com) as a text value, an actual screenshot of the URL is captured and displayed in the template.
here is the equivalent feature available for ACF Pro plugin - https://codecanyon.net/item/acf-website-screenshot/6280665
so, I'm hoping that this same feature can be worked into MB. thanks!
May 17, 2019 at 4:23 PM #14609Anh Tran
KeymasterHi Neil,
I think your code snippet in the first reply works pretty well. I modify it a little bit to creates a shortcode for showing website screenshot. It takes URL from a field.
add_action( 'init', function() { add_shortcode( 'screenshot', function( $atts ) { $atts = shortcode_atts( [ 'width' => 300, 'height' => 300, 'field' => 'website_url', ], $atts ); $url = rwmb_meta( $atts['field'] ); if ( ! $url ) { return ''; } $query_url = 'http://s.wordpress.com/mshots/v1/' . urlencode( $url ) . "?w={$width}&h={$height}"; $image_tag = "<img width='$width' height='$height' src='$query_url'>"; return "<a href='$url'>$image_tag</a>"; } ); } );
May 18, 2019 at 12:28 AM #14620@mindspark
Participantthanks!
-
AuthorPosts
- You must be logged in to reply to this topic.