Extract shortcode parameters in content – WordPress

Think about a post content like below:

[shortcode a="a_param"]
... Some content and shortcodes here
[shortcode b="b_param"]
.. Again some content here
[shortcode c="c_param"]

I have a shortcode that takes 3 or more parameters.
I want to find out how many times the shortcode is used at the content and its parameters in an array like,

Read More
array (
[0] => array(a => a_param, b=> null, c=>null),
[1] => array(a => null, b=> b_param, c=>null),
[2] => array(a => null, b=> null, c=>c_param),
)

I need to do this in the_content filter, wp_head filter or something similar.

How can I do this ?

Thank you,

Related posts

2 comments

  1. In wordpress get_shortcode_regex() function returns regular expression used to search for shortcodes inside posts.

    $pattern = get_shortcode_regex();
    

    Then preg_match the pattern with the post content

    if (   preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) )
    

    If it return true, then extracted shortcode details are saved in $matches variable.

    Try

    global $post;
    $result = array();
    //get shortcode regex pattern wordpress function
    $pattern = get_shortcode_regex();
    
    
    if (   preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) )
    {
        $keys = array();
        $result = array();
        foreach( $matches[0] as $key => $value) {
            // $matches[3] return the shortcode attribute as string
            // replace space with '&' for parse_str() function
            $get = str_replace(" ", "&" , $matches[3][$key] );
            parse_str($get, $output);
    
            //get all shortcode attribute keys
            $keys = array_unique( array_merge(  $keys, array_keys($output)) );
            $result[] = $output;
    
        }
        //var_dump($result);
        if( $keys && $result ) {
            // Loop the result array and add the missing shortcode attribute key
            foreach ($result as $key => $value) {
                // Loop the shortcode attribute key
                foreach ($keys as $attr_key) {
                    $result[$key][$attr_key] = isset( $result[$key][$attr_key] ) ? $result[$key][$attr_key] : NULL;
                }
                //sort the array key
                ksort( $result[$key]);              
            }
        }
    
        //display the result
        print_r($result);
    
    
    }
    
  2. Just to save time, this is the function I made based on @Tamil Selvan C answer :

    function get_shortcode_attributes( $shortcode_tag ) {
            global $post;
            if( has_shortcode( $post->post_content, $shortcode_tag ) ) {
                $output = array();
                //get shortcode regex pattern wordpress function
                $pattern = get_shortcode_regex( [ $shortcode_tag ] );
                if (   preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) )
                {
                    $keys = array();
                    $output = array();
                    foreach( $matches[0] as $key => $value) {
                        // $matches[3] return the shortcode attribute as string
                        // replace space with '&' for parse_str() function
                        $get = str_replace(" ", "&" , trim( $matches[3][$key] ) );
                        $get = str_replace('"', '' , $get );
                        parse_str( $get, $sub_output );
    
                        //get all shortcode attribute keys
                        $keys = array_unique( array_merge(  $keys, array_keys( $sub_output )) );
                        $output[] = $sub_output;
                    }
                    if( $keys && $output ) {
                        // Loop the output array and add the missing shortcode attribute key
                        foreach ($output as $key => $value) {
                            // Loop the shortcode attribute key
                            foreach ($keys as $attr_key) {
                                $output[$key][$attr_key] = isset( $output[$key] )  && isset( $output[$key] ) ? $output[$key][$attr_key] : NULL;
                            }
                            //sort the array key
                            ksort( $output[$key]);
                        }
                    }
                }
                return $output;
            }else{
                return false;
            }
        }
    

Comments are closed.