Support » Plugins » Hacks » shortcode_parse_atts() and PHP Versions

  • I’m trying to parse a shortcode string in a custom ‘shortcode helper’ TinyMCE editor popup.

    Rather than write my own function from scratch, I thought I’d use the existing WordPress shortcode-parser, ‘shortcode_parse_atts()’ in wp-includes/shortcodes.php. I’m getting odd results though.

    I’m currently testing the site offline, using MAMP, which gives me the option to change the PHP version between 5.2.17 and 5.4.4 (I don’t know why just those particular versions).

    In PHP v.5.4.4, everything appears to work fine. The function returns an associative array of key=>value pairs from the shortcode string, as expected.

    However, in the older PHP version, shortcode_parse_atts() returns each shortcode attribute key/value pair as a single string, in an unkeyed array, which breaks the rest of my code.

    Anyone know why this is happening? Other shortcodes appear work as they should otherwise, in both PHP versions, so I’m stumped why the same function that is (I presume) used by WordPress itself doesn’t work in the context of my popup panel.

    Anyone any idea why this might be happening, and how I might go about fixing it?

    For reference, here is the function in question:

    function shortcode_parse_atts($text) {
    	$atts = array();
    	$pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
    	$text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
    	if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
    		foreach ($match as $m) {
    			if (!empty($m[1]))
    				$atts[strtolower($m[1])] = stripcslashes($m[2]);
    			elseif (!empty($m[3]))
    				$atts[strtolower($m[3])] = stripcslashes($m[4]);
    			elseif (!empty($m[5]))
    				$atts[strtolower($m[5])] = stripcslashes($m[6]);
    			elseif (isset($m[7]) and strlen($m[7]))
    				$atts[] = stripcslashes($m[7]);
    			elseif (isset($m[8]))
    				$atts[] = stripcslashes($m[8]);
    		}
    	} else {
    		$atts = ltrim($text);
    	}
    	return $atts;
    }

    Cheers,

    a|x

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘shortcode_parse_atts() and PHP Versions’ is closed to new replies.