Toggle A Caption in Fancybox 3

March 27, 2018

View Demo on CodePen View Project on GitHub

Since the release of Fancybox 3 and all of its new options, I started looking for a way to incorporate the caption of an image and toggle it.  The default effect of this JQuery lightbox plugin is to hide and show the caption (along with all of the UI) when the lightbox is hovered.  I have adjusted this slightly so there is a toggle button added to the options and it toggles the caption.

There are two sections to creating this effect, the Javascript and the CSS.

Adjusting the CSS

While I don’t recommend adjusting the CSS file you call when implementing Fancybox 3, you can easily add your own CSS lines to override the default styling.

I am adding a little padding, adjusting the transition property to accommodate the toggle-in, and placing it out of the viewport so the user can’t see it.  I am also declaring a property for the class I will add to the Fancybox caption when toggled.  This allows us to control the styling and transition when it is in toggled on and off.

I am also adjusting the width of the custom button I plan on creating so it is not fixed like the others.

Adding Javascript to modify Fancybox 3 and its settings

Thanks to the Fancybox 3 documentation, it is incredibly easy to modify how the plugin animates and controls your content.  Normally, Fancybox 3 will work out of the box by simply adding a data-fancybox  attribute to your HTML element, but in this case we need to go a little further to adjust the settings.

In this case, we will be doing two things –

  1. Declaring a custom button for Fancybox 3
  2. Adding a click event to the button which toggles a CSS class

So, how do we do that?

Well, first, we are going to create the button with this Javascript:

Now, we need to ensure that when Fancybox is called that it understands to use our new button.  Let’s call Fancybox and determine which buttons it should use:

Lastly, we need to add a click event on this button, so it knows what to do!

Here we are saying — if this button is ever clicked, see if the caption already has our toggled class, and if not — add it!

That’s it!  You now have a Fancybox 3 plugin that toggles the caption.

Hopefully with this example, you are able to see the flexibility and customization the developers at FancyApps provide to their already stellar plugin.  You could easily develop a custom user-experience that goes much further than a simple Lightbox effect.  Feel free to comment and send me examples!

 

Embed A Tumblr Feed In WordPress

January 19, 2017

Below is an example showing how to embed multiple Tumblr posts as a feed into a WordPress site.  You will need a little coding experience with PHP and access to your WordPress theme files.  It is also required to have a Tumblr API key.

1. Get A Tumblr API key

This is required to use Tumblr’s API for requests.  In this case, we will be using it to fetch the latest posts from a Tumblr user.  You can visit here to set up an APP and retrieve your API key.

2. Set Up Your Shortcode

A shortcode is a simple WordPress snippet that you can pop into the page/post editor to place some code, make a query, or anything else you could possibly want.  Today, we are going to fetch Tumblr posts with PHP and place them in the site.

Editing the functions.php file of your WordPress theme, place this code at the bottom before the PHP end tag:

Let’s talk a little about what is happening here.

There are 3 variables you need to define at the beginning of the code:

  1. Your API key
  2. The amount of posts you want
  3. The Tumblr URL of the posts you want

The code will then fetch those posts and return them in JSON format.  In your foreach loop, you could cycle through each one of those objects in the array and pull any information you would need from each post.  In this case, I only want the post URL.

I only want the post URL because I am going to use a very handy function from WordPress to do the hard work for me.  Instead of figuring out whether each post is a link, a video, a photo, an embed, and then trying to format the HTML for each one, I will use what WordPress already provides, the oEmbed function.  That functionality takes whatever URL you paste in your WordPress text editor and then tries to find the embed code for it and place it within your content.

WordPress also provides a function to initiate this feature however you please: wp_embed_get();

So for this line of code:

we are just looping through each returned Tumblr post via JSON, and running it through the WordPress embed function wp_embed_get().

3. Place your shortcode where you want the feed

By adding the line below, you will call the function we placed in our functions.php file, and it will place it where the shortcode was used.

This shortcode can be placed anywhere on a page, post, or custom post type in the content editor.

You can customize a lot of the functionality to suit your needs, but hopefully this gets you started working with the Tumblr API and the many ways you can dynamically call Tumblr content on your WordPress site.

Credit

I want to credit @groovycarrot for his post describing how to first work with the Tumblr API in PHP which inspired the idea.

Call A Relative Font, Image, or Javascript File in a Custom Weebly Theme

November 30, 2016

I had the task of creating a custom Weebly theme recently.  One of the simplest, yet most difficult, things I found in creating and uploading a theme was how to properly call javascript files, fonts, images, and anything else relative in the theme.  While I cannot dive into the entire theme structure and requirements, there is a lot of provided documentation from Weebly on how to do this.

View Weebly Documentation

Instead, I want to focus on the document structure of a Weebly theme and where the files should be located when you upload them.  Then, I want to talk about how they can be called.

This is an example of the document structure you would upload to Weebly as a custom theme.  To learn more about the anatomy of a template, visit here.

  • /
    • assets/
      • fonts/
      • images/
      • custom.js
    • main_style.css
    • landing.html

What I am showing here is about the absolute minimum for a Weebly theme.  I have my HTML page (landing.html), my stylesheet (main_style.css), I have my assets directory, and within that directory I have 2 sub-directories (fonts, images) and a javascript file (custom.js).

Calling A Javascript File in the HTML

In our HTML file (in this case landing.html), we can call our javascript file (custom.js) through the following script tag:

You can basically think of it as “/assets/” = “/files/theme/” when it comes to being called.

A couple things I note found:

  • Calling Javascript files worked best at the end of the HTML right before </body>
  • Almost none of the Javascript files worked in Preview/Edit.  Only until I Published did they work.

Calling Images or Fonts in the stylesheet

In the stylesheet (main_style.css), you would think it is necessary to call an image with this relative path:

but, you’d be wrong.  The relative path should be:

The same goes for calling fonts within the “/font/” sub-directory of assets:

This is because your stylesheet is moved into your “/assets/” directory when it is uploaded, even though it is in your root when it is uploaded/downloaded.  Even when you download the Weebly provided base theme, your stylesheet is in your root, but it is moved to an “/asset/” directory after upload.

Call An External File in the HTML

Below is an example of how I would call Font Awesome in an HTML file:

Troubleshooting

If you run across any issues, there are a few things you can check or keep in mind:

  • Zip up the contents of your root directory when you upload, not the directory itself!
  • The javascript files rarely work in the Editor, at least, for me.  It wasn’t until I published the site that I could actually see a lot of the javascript working
  • Create an “/assets/” directory because Weebly will create one for you anyway

Hopefully, this helps during the development of a custom theme for a Weebly site.

Animate Elements with CSS Animations & JQuery

October 16, 2016

View Project on CodePen

Today, I wanted to feature a technique I have been using which quickly allows me to animate elements on a page with CSS animations.  It will loop through each element on the page  based on a CSS class and intermittently add an animation to that element.  The method has fall-backs for browsers that do not support CSS animations (in that they just show on page load, but do not animate) so you never have to worry about the content not showing on older browsers.  This method also prevents “glitchy” content blips that tend to occur with JQuery class additions on the document loading.

First, we must start with our HTML content:

It contains 4 paragraphs, all with some Lorem Ipsum.  What’s important is that each element is given a class of “animate”, which we will use as our selector.  The class can be given to any element you were hoping to use a CSS animation for, which opens up the ability to constantly add/remove and dynamically pull elements without ever having to worry about the front-end animation suffering.

This is because of the JQuery you will see below:

Fire this JQuery snippet when your document is ready.  Let’s talk a bit about what is happening here —

First, we use the each() selector to grab each element on the page with the “animate” class.  Then, we are saying: “Every 500 milliseconds, add the class ‘animation’ to that element, wait 500 milliseconds, go onto the next element, add the class to that one, and so on, and so forth.”  This function will go through every element with that class, add the animation class, and do this until they are all done.

I like this method because it delays the animation on each element by 500 milliseconds (which you of course can change the timing of), making a nice presentation no matter how many elements you have on the page.

You can, however, choose to simply queue all of the animations at once with this code instead:

Now that we have these core parts set up, the possibilities become endless with CSS.  I wanted to ensure a fallback in case the CSS animations aren’t supported as well, so we will incorporate the new @supports rule.

I am only relying on the -webkit- vendor prefix and full support, but you could open this up to more browsers by incorporating other vendor prefixes.  I find that this works for a majority of my cases (and will only improve), so it is enough for me.  The best part of this approach is that, worst case scenario, elements are given a class with our JQuery function and nothing happens on the front end.  No content is hidden or lost.

Again, let’s beak this down —

First, we are using the rule declaration and nested a few CSS rules.  If animations are supported, then I want to set the initial state of those elements.  In this case, I am going to fade in these elements, so any element with the animate class is going to need to be hidden:

Next, I want to determine what we do when they are animated.  The class “animation” is being added to each element, so let’s determine what that is:

In this case, the keyframes of the animation are being determined by the “slideIn” animation.  It transforms and fades the element in.  In the CSS class, we are able to determine which animation is used, how long it takes, and the state it is in when the animation is done.

You can change any of these elements and that’s the biggest reason I favor this method over any other.  I can update the CSS constantly, change animations, change how long they animate for, what they do, without ever modifying some of the main core code of HTML and JQuery.

If I ever want to animate another element, I can simply add the “animate” class to it and it will follow the same behavior.  This allows for a robust amount of flexibility.

For example, I can adjust the animation for any element by giving it a new CSS rule without ever touching the HTML or JQuery code.

Say I want to have the first paragraph perpetually loop through its animation.  I can do this with one simple new CSS rule:

And now, the first paragraph will run through the animation after 1 second of delay, run for 2 seconds, and loop infinitely.

View the Demo on CodePen

You can make countless rules and animations for all of the elements on the page, and also determine their state before the animation occurs.

I hope this helps with your front-end development processes going forward.  Feel free to explore this approach.  Hopefully, its ability to scale will really make it a routine method in your process.

 

 

Use Contact Form 7 To Post Rows to Advanced Custom Fields Repeater

April 17, 2015

I was in a particular situation where I needed a task management system that could take fields through a form from Contact Form 7 and post them to rows in the Advanced Custom Fields Repeater field I set up.  The idea was to use a form from the front end of WordPress to post to the back-end of WordPress in a custom field.  You can accomplish this by adding code to your functions.php file in the WordPress theme.

This example assumes that you already have a form set up through Contact Form 7 and you have already purchased the Advanced Custom Fields’ Repeater Field plugin.

1. Get Your Field Key

You can grab your field key from the ACF Repeater Field by selecting ‘Show Field Key‘ from the Screen Options.  You will use this in your function to determine which field to add rows to.

cap1

 

 

2.  Add WPCF7 Before Send Mail Hook

You add the function below to your functions.php file within the theme.  You are hooking into Contact Form 7’s “wpcf7_before_send_mail’ function to grab the post data and post it to a WordPress repeater field as a new row.

Obviously, your fields in the form and the repater field may be different.  The important things to take away from this functions script is that:

You’ve created the hook:

 

You’ve got the URL and ID of the post the form was submitted through:

 

You got the data from the form:

 

And you update the repeater field based on those elements:

Hope that helps!  Don’t forget you can always extend this to a plugin as opposed to a theme function as well.