Code Snippets

Recent Code Snippets

Detect if DIVI Builder is Running

The code checks for DIVI Builder in PHP and JavaScript. In PHP, it enqueues a script only if the ‘et_fb’ parameter is empty, and in JavaScript, it performs an action if the body element does not have the ‘et_fb’ class.

January 10, 2024

Only One Login Session

This PHP function, “hlabs_login_one_user_instance,” ensures that only one instance of a user is logged in at a time. It utilizes WP_Session+Tokens to destroy other user sessions upon login, allowing only the latest session for a particular username. This prevents simultaneous logins and enhances security.

January 5, 2024

Install Node.js and npm using Homebrew on OS X and macOS

To install Node.js and npm using Homebrew, first install Homebrew using the command /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”. Then, run brew update and brew doctor for system readiness. Add Homebrew’s location to your $PATH. Finally, install Node with brew install node and test npm by installing Grunt.

December 6, 2023

WordPress Update URLS

This post contains SQL queries for updating URLs in a WordPress database. It includes commands for replacing URLs in options, posts, content, metadata, links, image links, user data, and comments. The purpose is to update the website’s URL from an old domain to a new one.

January 30, 2023

Add Extra Media Type Filters to the WordPress Media Manager

This code snippet adds extra media type filters to the WordPress Media Manager for PDFs. It defines the label values for the PDF mime type and adds them to the post mime types array using the add_filter function. This allows for better management and categorization of PDF files within the media manager.

January 30, 2023

Improve WP Query performance

The PHP code snippet initializes a WP_Query object with specified arguments. The ‘no_found_rows’ parameter disables post count for pagination, ‘update_post_term_cache’ omits term retrieval, and ‘update_post_meta_cache’ excludes post metadata retrieval. This configuration optimizes query performance by fetching only required data.

January 30, 2023

Limit Tags to 3 per Post

The code limits the number of tags displayed to three per post in PHP using the `add_filter` function and a custom `hlabs_limit_post_tags` function. It utilizes the `array_slice` function to achieve this limitation.

January 30, 2023

Youtube Schema Jetpack

The PHP code checks if the URL path is ‘/videoseries’ or if the ‘list’ query argument is set. It then generates HTML code for embedding a YouTube video playlist or a single video accordingly. The resulting HTML includes metadata for the video, such as name, description, upload date, thumbnail URL, and dimensions.

January 30, 2023

Jetpack Custom Fallback Image

The provided PHP function “hlabs_custom_image” sets a fallback image if the media is empty. It uses the Jetpack feature to return a custom image with a permalink. The function is hooked to “jetpack_images_get_images” filter.

January 30, 2023

Jetpack: Add Related Posts to your RSS feed

This PHP function adds Jetpack Related Posts to the RSS feed. It checks if Jetpack and Related Posts are active, retrieves related posts, and then appends them to the post content in the RSS feed. It also includes an optional headline based on Jetpack settings.

January 30, 2023