简体   繁体   中英

wordpress shortcode to json

If someone has an experience in creating a regex handler on JavaScript, a help will be much appreciated. I'm stuck at converting Wordpress shortcodes structure like

[container atr1='123' atr2='123']

  [column width="30"]
    [textblock]
        <p>fdsfsdf</p>
    [/textblock]
  [/column]

[/container]

to JSON like

{
'type': 'container'
'content' : [
   {
    'type' : 'column'
    'content' : [
       {
          'type': 'textblock'
          'content': '<p>fdsfsdf</p>'
       }
    ],
    'width': '30'
   }
   ],
'atr1': '123',
'atr2': '123'
}

Please give a help or tutorial for creating handler. I've found one http://ejohn.org/files/htmlparser.js , but it looks very complicated and unclear for my understanding. Thanks for any suggestion!

As of wordpress version 4.9.8 there is a built-in JavaScript function (in //wp-includes/js/shortcode.js):

wp.shortcode.next( shortcode_tag, text_to_parse, index_where_start_search ) 

Not very comfortable but it interprets shortcodes with and without closing tag and attributes..

It returns:

  • index : The index where the match occurred so you can easily match the next shortcode using the matched index plus 1.
  • content : The content actually matched, shortcode tags and all.
  • shortcode : The wp.shortcode object created from the matched shortcode string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM