Large, prominently placed images are eye-catchers. WordPress even has a name for it: Featured Image. The only problem is: Starting every post again and again with a ‘featured image’ is tiring. Even if our reader has already decided on an article, we force her to scroll. It would be better to give her directly what she wants: the text. Letting her decide when she wants to see the big picture by using Fancy Boxes is eventually also a matter of readability:
That means that we present the image in a smaller size first — already surrounded by the text she has chosen — and show it to her in a larger size((Featured Images should be of the size 16:9, e.g. 1200x628 order 1280:720 cf. pars pro toto wpastra.com)) when she indicates her wish by a click:
An elegant solution for this is called fancybox
.1
Solution
- Load down the files jquery.fancybox.min.css and jquery.fancybox.min.js from github.com/fancyapps/fancybox and copy them (in)to the directory
lib
. - Add the following code to your
functions.php
:
/*
* Simple short code for inserting a fancy boxed imaged
* (C) 2023 Karsten Reincke
* SPDX-License-Identifier: MIT
*/
function fcbPicCode($atts){
$atts = (array) $atts;
$imid = $atts[0]; // obligatoric parameter
$width = $atts[1]; // obligatoric parameter
$wpsize = ""; // thumbnail, medium, large, or full
$alt=""; // optional parameter
$style="is-style-default"; // optional parameter
$align="alignleft"; // optional parameter
/* IDEA: try to download the best fitting image size be default.
* Download the full sized image if and when the user had said
* she wants to see it by a click (fancy)
*/
if ($width <= 150)
$wpsize = "thumbnail";
elseif ($width <=300)
$wpsize = "medium";
elseif ($width <= 1024)
$wpsize = "large";
else
$wpsize = "full";
/* get the path to the original image */
$fimgp=wp_get_attachment_image_url($imid,"full");
if (!($fimgp)) return ("wrong image data");
/* if it's an svg, there won't be any smaller images */
if (wp_get_image_mime($fimgp) == "svg" )
$simgp=$fimgp;
else {
/* otherwise get the path to the fitting smaller image */
$simgp=wp_get_attachment_image_url($imid,$wpsize);
if (!($simgp)) $simgp=$fimgp;
}
if (array_key_exists(2,$atts)) {
$alt=$atts[2];
if (array_key_exists(3,$atts)) {
$style=$atts[3];
if (array_key_exists(4,$atts))
$align=$atts[4];
}
}
$res=
'<div class="wp-block-image">' .
'<figure class="' . $align . ' size-medium is-resized ' . $style . '">' .
'<a href="' . $fimgp . '" data-fancybox="">' .
'<img src="' . $simgp . '" alt="'. $alt .'" width="' . $width . '" >' .
'</a>' .
'</figure></div>';
return $res;
}
add_shortcode('fcbpic', 'fcbPicCode');
- Add the following lines to your
header.php
, just before the tag</head>
:
<link rel="stylesheet" type="text/css"
href="<?php echo get_stylesheet_directory_uri(); ?>/lib/jquery.fancybox.min.css" media="all">
<script type="text/javascript"
src="<?php echo get_stylesheet_directory_uri();?>/lib/jquery.fancybox.min.js"></script>
Background
Of course, there is also a respective WordPress plugin. It promises that “FancyBox” is “[…] automatically applied to all your image links and galleries”.2 The description says more carefully that the plugin uses “[…] jQuery to apply FancyBox to ANY thumbnails that link directly to an image”.3 But for me, at least, it didn’t help: I want to be able to ‘spruce up’ posts in the first paragraph with a small image and display it to my reader — at a click — in its original size. I don’t want to be limited to thumbnails.
So I switched back to the manual method: Putting the fancy-libs into bootScore my child theme and creating a more comfortable shortcode :4
[fcimg imageID width alt imageStyle imageAlignment]
The example above had been created by
[fcimg 5516 400 "tomcat in a basket" is-style-rounded aligncenter]
And how does this …
… support our migration to bootScore? Well, once a web designer has taken the first general steps and has checked SEO, she will soon turn to a really thick board, her properly clustered and presented menu without getting too fancy. Eventually, she is going to spruce and speed up the display of fixed and scalable images. This post talks about a part of this way.
- But beware of the license modifications that are just now executed [2023.02.22]! To protect you I’ve switched back to the elder version 3.x [↩]
- cf. Fancy Box Installation [↩]
- cf. Fancy Box Details [↩]
- You get the imageId by opening your media library, selecting the respecting image, and evaluating the arisen URL. [↩]