Six custom functions I use on every WordPress plugin project

Abstract Coding Image
License: Adobe Stock

Throughout my life as a plugin author, I tend to write little utilities for myself. These six functions help me out quite a bit, especially when dealing with multisite and asset directories.

Read on for six functions I likely could not do without.

is_multisite – Check if a plugin is active on a multisite network

I needed a check to see if a plugin is installed on multisite and was active for a network. Also a good replacement for core function is_multisite.

By passing true to the $network_admin, you can check if you’re in the network admin section.

/**
 * Checks if the plugin is installed and activated on a multisite install.
 *
 * @since 1.0.0
 *
 * @param bool   $network_admin Check if in network admin.
 * @param string $slug Plugin slug.
 *
 * @return true if multisite, false if not.
 */
public static function is_multisite( $network_admin = false, $slug = 'alerts-dlx' ) {
	if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
		require_once ABSPATH . '/wp-admin/includes/plugin.php';
	}
	$is_network_admin = false;

	// Check if we're in network admin and the plugin slug is network activated.
	if ( $network_admin ) {
		if ( is_network_admin() ) {
			if ( is_multisite() && is_plugin_active_for_network( $slug ) ) {
				return true;
			}
		} else {
			return false;
		}
	}

	// Check if the plugin is network-activated on multisite.
	if ( is_multisite() && is_plugin_active_for_network( $slug ) ) {
		return true;
	}
	return false;
}
Code language: PHP (php)

is_activated – Check if a plugin is active

Basically a wrapper to is_multisite above, this can check to see if another plugin is active or not.

This can be used when developing add-ons and you need to check that a dependency is installed already.

/**
 * Checks to see if an asset is activated or not.
 *
 * @since 1.0.0
 *
 * @param string $path Path to the asset.
 * @param string $type Type to check if it is activated or not.
 *
 * @return bool true if activated, false if not.
 */
public static function is_activated( $path, $type = 'plugin' ) {

	// Gets all active plugins on the current site.
	$active_plugins = self::is_multisite() ? get_site_option( 'active_sitewide_plugins' ) : get_option( 'active_plugins', array() );
	if ( in_array( $path, $active_plugins, true ) ) {
		return true;
	}
	return false;
}
Code language: PHP (php)

sanitize_attribute – Read in block options and sanitize passed attributes

This next one is for those who deal with server-side rendering of their Gutenberg blocks. Just simply pass the type of data it is, and it will attempt to sanitize all instances, or return an error if the option can’t be sanitized.

/**
 * Sanitize an attribute based on type.
 *
 * @param array  $attributes Array of attributes.
 * @param string $attribute  The attribute to sanitize.
 * @param string $type       The type of sanitization you need (values can be integer, text, float, boolean, url).
 *
 * @return mixed Sanitized attribute. wp_error on failure.
 */
public static function sanitize_attribute( $attributes, $attribute, $type = 'text' ) {
	if ( isset( $attributes[ $attribute ] ) ) {
		switch ( $type ) {
			case 'raw':
				return $attributes[ $attribute ];
			case 'post_text':
			case 'post':
				return wp_kses_post( $attributes[ $attribute ] );
			case 'string':
			case 'text':
				return sanitize_text_field( $attributes[ $attribute ] );
			case 'bool':
			case 'boolean':
				return filter_var( $attributes[ $attribute ], FILTER_VALIDATE_BOOLEAN );
			case 'int':
			case 'integer':
				return absint( $attributes[ $attribute ] );
			case 'float':
				if ( is_float( $attributes[ $attribute ] ) ) {
					return $attributes[ $attribute ];
				}
				return 0;
			case 'url':
				return esc_url( $attributes[ $attribute ] );
			case 'default':
				return new \WP_Error( 'alerts_dlx_unknown_type', __( 'Unknown type.', 'alerts-dlx' ) );
		}
	}
	return new \WP_Error( 'alerts_dlx_attribute_not_found', __( 'Attribute not found.', 'alerts-dlx' ) );
}
Code language: PHP (php)

get_kses_allowed_html – Read in valid HTML, including SVG code

This next one allows you to make sure that only valid HTML is returned, and adds in a check for SVGs.

/**
 * Returns appropriate html for KSES.
 *
 * @param bool $svg Whether to add SVG data to KSES.
 */
public static function get_kses_allowed_html( $svg = true ) {
	$allowed_tags = wp_kses_allowed_html();

	$allowed_tags['nav']        = array(
		'class' => array(),
	);
	$allowed_tags['a']['class'] = array();

	if ( ! $svg ) {
		return $allowed_tags;
	}
	$allowed_tags['svg'] = array(
		'xmlns'       => array(),
		'fill'        => array(),
		'viewbox'     => array(),
		'role'        => array(),
		'aria-hidden' => array(),
		'focusable'   => array(),
		'class'       => array(),
	);

	$allowed_tags['path'] = array(
		'd'       => array(),
		'fill'    => array(),
		'opacity' => array(),
	);

	$allowed_tags['g'] = array();

	$allowed_tags['use'] = array(
		'xlink:href' => array(),
	);

	$allowed_tags['symbol'] = array(
		'aria-hidden' => array(),
		'viewBox'     => array(),
		'id'          => array(),
		'xmls'        => array(),
	);

	return $allowed_tags;
}
Code language: PHP (php)

get_plugin_url and get_plugin_dir – Get an absolute path, or URL to a plugin asset

For these next ones, I always need a way to get a URL or full path of a plugin asset. This works very similar to how plugins_url works, but is a bit more flexible.

/**
 * Get the plugin directory for a path.
 *
 * @param string $path The path to the file.
 *
 * @return string The new path.
 */
public static function get_plugin_dir( $path = '' ) {
	$dir = rtrim( plugin_dir_path( __FILE__ ), '/' );
	if ( ! empty( $path ) && is_string( $path ) ) {
		$dir .= '/' . ltrim( $path, '/' );
	}
	return $dir;
}

/**
 * Return a plugin URL path.
 *
 * @param string $path Path to the file.
 *
 * @return string URL to to the file.
 */
public static function get_plugin_url( $path = '' ) {
	$dir = rtrim( plugin_dir_url( __FILE__ ), '/' );
	if ( ! empty( $path ) && is_string( $path ) ) {
		$dir .= '/' . ltrim( $path, '/' );
	}
	return $dir;
}
Code language: PHP (php)
Ronald Huereca
By: Ronald Huereca
Published On: on September 12, 2022

Ronald Huereca founded DLX Plugins in 2022 with the goal of providing deluxe plugins available for download. Find out more about DLX Plugins, check out some tutorials, and check out our plugins.

Leave Your Valuable Feedback

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

Shopping Cart
  • Your cart is empty.
Scroll to Top