Custom Field Generate URL Screenshot

Support General Custom Field Generate URL ScreenshotResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #12566
    @mindspark@mindspark
    Participant

    Anh - 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.

    #12569
    Anh TranAnh Tran
    Keymaster

    Hi 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?

    #14578
    @mindspark@mindspark
    Participant

    Hi 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!

    #14609
    Anh TranAnh Tran
    Keymaster

    Hi 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>";
        } );
    } );
    #14620
    @mindspark@mindspark
    Participant

    thanks!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.