jrobinsonc
4/13/2017 - 1:42 PM

Wordpress shortcode parser

Wordpress shortcode parser

<?php

$content = get_the_content();
$shortcodes_data = [];

// This will extract the shortcodes named [coupon_form] and [page_settings].
if (preg_match_all('/\[(\[?)(coupon_form|page_settings)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/s', $content, $matches)
    && array_key_exists(2, $matches))
{
    foreach ($matches[2] as $shortcode_key => $shortcode_name) {
        $shortcodes_data[$shortcode_name] = shortcode_parse_atts($matches[3][$shortcode_key]);
    }
}

// You can access the attributes this way:
echo $shortcodes_data['coupon_form']['title'];