has_shortcode

The timeline below displays how wordpress function has_shortcode has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Determines whether the passed content contains the specified shortcode.
 *
 * @since 3.6.0
 *
 * @global array $shortcode_tags
 *
 * @param string $content Content to search for shortcodes.
 * @param string $tag     Shortcode tag to check.
 * @return bool Whether the passed content contains the given shortcode.
 */
function has_shortcode($content, $tag)
{
    if (!str_contains($content, '[')) {
        return false;
    }
    if (shortcode_exists($tag)) {
        preg_match_all('/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER);
        if (empty($matches)) {
            return false;
        }
        foreach ($matches as $shortcode) {
            if ($tag === $shortcode[2]) {
                return true;
            } elseif (!empty($shortcode[5]) && has_shortcode($shortcode[5], $tag)) {
                return true;
            }
        }
    }
    return false;
}

WordPress Version: 6.1

/**
 * Determines whether the passed content contains the specified shortcode.
 *
 * @since 3.6.0
 *
 * @global array $shortcode_tags
 *
 * @param string $content Content to search for shortcodes.
 * @param string $tag     Shortcode tag to check.
 * @return bool Whether the passed content contains the given shortcode.
 */
function has_shortcode($content, $tag)
{
    if (false === strpos($content, '[')) {
        return false;
    }
    if (shortcode_exists($tag)) {
        preg_match_all('/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER);
        if (empty($matches)) {
            return false;
        }
        foreach ($matches as $shortcode) {
            if ($tag === $shortcode[2]) {
                return true;
            } elseif (!empty($shortcode[5]) && has_shortcode($shortcode[5], $tag)) {
                return true;
            }
        }
    }
    return false;
}

WordPress Version: 4.4

/**
 * Whether the passed content contains the specified shortcode
 *
 * @since 3.6.0
 *
 * @global array $shortcode_tags
 *
 * @param string $content Content to search for shortcodes.
 * @param string $tag     Shortcode tag to check.
 * @return bool Whether the passed content contains the given shortcode.
 */
function has_shortcode($content, $tag)
{
    if (false === strpos($content, '[')) {
        return false;
    }
    if (shortcode_exists($tag)) {
        preg_match_all('/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER);
        if (empty($matches)) {
            return false;
        }
        foreach ($matches as $shortcode) {
            if ($tag === $shortcode[2]) {
                return true;
            } elseif (!empty($shortcode[5]) && has_shortcode($shortcode[5], $tag)) {
                return true;
            }
        }
    }
    return false;
}

WordPress Version: 4.2

/**
 * Whether the passed content contains the specified shortcode
 *
 * @since 3.6.0
 *
 * @global array $shortcode_tags
 *
 * @param string $content Content to search for shortcodes.
 * @param string $tag     Shortcode tag to check.
 * @return bool Whether the passed content contains the given shortcode.
 */
function has_shortcode($content, $tag)
{
    if (false === strpos($content, '[')) {
        return false;
    }
    if (shortcode_exists($tag)) {
        preg_match_all('/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER);
        if (empty($matches)) {
            return false;
        }
        foreach ($matches as $shortcode) {
            if ($tag === $shortcode[2]) {
                return true;
            } elseif (!empty($shortcode[5]) && has_shortcode($shortcode[5], $tag)) {
                return true;
            }
        }
    }
    return false;
}

WordPress Version: 4.0

/**
 * Whether the passed content contains the specified shortcode
 *
 * @since 3.6.0
 *
 * @global array $shortcode_tags
 * @param string $tag
 * @return boolean
 */
function has_shortcode($content, $tag)
{
    if (false === strpos($content, '[')) {
        return false;
    }
    if (shortcode_exists($tag)) {
        preg_match_all('/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER);
        if (empty($matches)) {
            return false;
        }
        foreach ($matches as $shortcode) {
            if ($tag === $shortcode[2]) {
                return true;
            } elseif (!empty($shortcode[5]) && has_shortcode($shortcode[5], $tag)) {
                return true;
            }
        }
    }
    return false;
}

WordPress Version: 3.9

/**
 * Whether the passed content contains the specified shortcode
 *
 * @since 3.6.0
 *
 * @global array $shortcode_tags
 * @param string $tag
 * @return boolean
 */
function has_shortcode($content, $tag)
{
    if (false === strpos($content, '[')) {
        return false;
    }
    if (shortcode_exists($tag)) {
        preg_match_all('/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER);
        if (empty($matches)) {
            return false;
        }
        foreach ($matches as $shortcode) {
            if ($tag === $shortcode[2]) {
                return true;
            }
        }
    }
    return false;
}

WordPress Version: 3.7

/**
 * Whether the passed content contains the specified shortcode
 *
 * @since 3.6.0
 *
 * @global array $shortcode_tags
 * @param string $tag
 * @return boolean
 */
function has_shortcode($content, $tag)
{
    if (shortcode_exists($tag)) {
        preg_match_all('/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER);
        if (empty($matches)) {
            return false;
        }
        foreach ($matches as $shortcode) {
            if ($tag === $shortcode[2]) {
                return true;
            }
        }
    }
    return false;
}