Decode HTML entities in QR codes that only consist of a URL

Code

Filename: gpqr-decode-html-entities-in-url.php

<?php
/**
 * Gravity Perks // QR Code // Decode HTML entities in QR codes that only consist of a URL
 * https://gravitywiz.com/documentation/gravity-forms-qr-code/
 *
 * Instructions: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 */
add_filter( 'gpqr_content_pre_generate', function ( $content ) {
	// Only modify QR codes that only consist of a URL.
	if ( ! preg_match( '/^https?:///', $content ) ) {
		return $content;
	}

	// Prevent infinite loop.
	if ( html_entity_decode( $content ) === $content ) {
		return $content;
	}

	// Use while loop in case there are recursively encoded HTML entities (e.g. &amp;amp;).
	while ( html_entity_decode( $content ) !== $content ) {
		$content = html_entity_decode( $content );
	}

	return $content;
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.