Validating fields in Gravity Forms

Despite CAPTCHA, spammers have been submitting spam forms on a few of our client’s websites. The submissions usually have the same first and last name, and often contain a website address in the message. I was able to add a little code to the theme’s functions.php to prevent this type of thing.

add_filter( 'gform_validation_1', 'validate_contact_fields_1' );
function validate_contact_fields_1( $validation_result ) {
    $form = $validation_result['form'];
    // first and last name can't be the same
    if ( rgpost( 'input_1' ) == rgpost( 'input_2' ) ) {
        // set the form validation to false
        $validation_result['is_valid'] = false;
        //finding Field with ID of 1 and marking it as failed validation
        foreach( $form['fields'] as &$field ) {
            //NOTE: replace 1 with the field you would like to validate
            if ( $field->id == '1' ) {
                $field->failed_validation = true;
                $field->validation_message = 'First and Last Name can not be the same.';
    // Message can't contain a URL
    $nourl_pattern = '(http|https)';
    if ( preg_match( $nourl_pattern, rgpost( 'input_4' ) ) ) {

		$validation_result['is_valid'] = false;
        foreach( $form['fields'] as &$field ) {
            if ( $field->id == '4' ) {
                $field->failed_validation = true;
                $field->validation_message = 'Message can not contain website addresses.';
    //Assign modified $form object back to the validation result
    $validation_result['form'] = $form;
    return $validation_result;
}  // end function validate_contact_fields_1

Fixing anchor links that scroll too far down the page

For the second time, I needed to adjust the CSS for a site that is using anchor links. The problem was, the page would scroll down too far because of a fixed menu at the top of the page, and the anchor link area ended up underneath the menu.

The fix is to add this to the site’s CSS:

:target:before {
height:55px; /* fixed header height*/
margin:-55px 0 0; /* negative fixed header height */

Change the height and margin to match the height of the fixed menu. Sometimes the display setting needs a little tweaking, too. Woo!

Dejunking WordPress options

While investigating a slow WordPress installation, I came across this post on how to check if autoloaded options are a factor in the slowdown.

How To Remove Unwanted Autoloaded Data on a WordPress Website

I had no idea that plugins would leave so much junk behind! Here are the two queries to investigate. One to see how big the total options are, and the second to find the worst offenders.

SELECT SUM(LENGTH(option_value)) as autoload_size FROM wp_options WHERE autoload=’yes’;

SELECT LENGTH(option_value),option_name FROM wp_options WHERE autoload=’yes’ ORDER BY length(option_value) DESC LIMIT 15;

For the site I was working on, I set the unnecessary options to autoload=’no’ … just in case it would affect the site.

Adding stuff after the body tag in WordPress

Today I had to add some retargeting code to a site, and my research revealed that I needed to add it to the functions.php of the theme. No problem.

function jr_add_floodlight_retargeting() {
// Added 4/14/21 by EKB to put code on landing page for Floodlight campaign 
   if ( isset( $_GET['utm_source'] ) ) {
      if($_GET['utm_source'] == "ww") {
         ... javascript code here ...
} // end function jr_add_floodlight_retargeting
add_action( 'wp_body_open', 'jr_add_floodlight_retargeting' );

But then, my code wasn’t firing properly, and I discovered that the child theme was old enough that it didn’t have the necessary code in header.php. I found out from this article:

WordPress 5.2 action that every theme should use

Added that code to the theme’s header file, and away I went! Yay.

Spam comments on media attachment pages

One of our clients was getting spam comments on her media attachment pages, so I found this handy little plugin to make it so the attachment pages are no longer available for those kinds of shenanigans. It solves the problem nicely by redirecting each page to the media file itself.

Disable Media Pages

A similar feature is available in Yoast SEO, so if you have that plugin, you can go to SEO -> Search Appearance -> Media tab and turn on the option to Redirect attachment URLs. Sweet!


A-PDF Thumbnailer

A-PDF Thumbnailer

We’ve been working on a project with lots of PDF files, and the download manager plugin makes use of featured images for the downloads. In the past, I’ve struggled with creating thumbnails of the first page… it had been a very time-intensive process.

Then, I found A-PDF Thumbnailer!! It made creation of the thumbnails really easy. After fiddling with it for a few minutes, I was able to create high quality, properly-sized thumbnails for a big batch of PDFs. And it’s quite a bargain at $27… it saved me hours of work.

You can download the trial version to see it in action before buying. It does even more than just creating thumbnails, but I haven’t needed to do that yet. Maybe someday. For Windows only.