How can I extract or parse data from post contents’ shortcodes into an array?
Not really a WordPress question, but you can use the PHP function preg_match_all
to extract all the text wrapped in [cs_text]
tags like so:
function wpse_265295_extract_cs_text( $subject ) {
$pattern = "#\[cs_text\](.*?)\[/cs_text\]#s";
preg_match_all( $pattern, $subject, $matches );
return isset( $matches[1] ) ? $matches[1] : [];
}
If you’re looking to extract arbitrary shortcodes:
if( ! function_exists( 'extract_shortcodes' ) ) :
function extract_shortcodes( $subject, $shortcode ) {
$pattern = "#\[$shortcode\](.*?)\[/$shortcode\]#s";
preg_match_all( $pattern, $subject, $matches );
return isset( $matches[1] ) ? $matches[1] : [];
}
endif;
Related Posts:
- How to parse shortcodes within returned content
- Parse Error: unexpected ‘}’ while creating Shortcode with Loop [closed]
- Checking post content for a shortcode, but content is being returned as empty
- Problem parsing long parameter in shortcode
- Gallery shortcode is empty but magically creates a gallery
- current_shortcode() – detect currently used shortcode
- Get shortcode name from within it’s callback function? [duplicate]
- Tinymce – How to hook before or after live shortcodes rendering?
- How to customize a shortcode using the customizer
- how to have the gallery shortcode output one single UL list instead of several DL?
- Stray closing paragraph tag when using shortcodes
- WordPress transients for a shortcode
- How to handle valueless attributes in shortcodes?
- How execute shortcode with javascript
- The result of a shortcode appear BEFORE page content
- Getting the count of a shortcode that is nested
- How to render complicated shortcodes
- Get shortcode attributes outside shortcode function
- Formatting post content to exclude gallery
- My shortcode is not working in Contact Form 7 Message Body
- Get Shortcode Attributes
- Shortcode doesn’t work with classes
- Get shortcode from the content and display it in other place (in sidebar, for example)
- Video size and border radius using a shortcode
- Adding shortcode to the main menu
- Inserting PHP inside do_shortcode
- wp_editor returns the shortcode and not render the output
- Prevent wordpress from rendering shortcode in post
- Executing a shortcode from a “normal” text/hyperlink
- Can a shortcode return its own name?
- Two different inner shortcode under shortcodes or multiple nesting of inner shortcodes
- Extra paragraph tags in an enclosing shortcode
- Create a shortcode that creates a form for users to edit their information
- How to split text text text into array
- WordPress Vue Js Shortcodes
- Shortcode multiple values
- How do you stop a shortcode from firing in the editor?
- Documentation for adding a tinyMCE view for visual live preview of custom shortcode?
- Gravity forms – get shortcode attribute from post content
- Login form from shortcode doesn’t redirect after successful login
- How to get the $atts of a shortcode
- Getting the count of a shortcode that is nested
- do_shortcode via ajax
- How to list users by custom field?
- Form Shortcode not saving data to WP database
- Code is providing a row of data but not formatting it as table
- Gutenberg Shortcode Fail
- How to add “get_theme_mod” inside a shortcode?
- How to create shortcode to display perticular word from page title
- Echo HTML in custom shortcode
- passing markup thought a shortcode attribute
- Echoing Shortcode content inside the loop
- Shortcode argument multiple values
- How can I collect and output all attributes of all executed ShortCodes?
- Short code inside the page not working in WordPress
- Add inline style to pages where shortcode was used
- Why ‘do_shortcode’ doesn’t work in a REST request?
- Basic custom Shortcode not working
- Theme Icons not visible on http://domain.com, works on http://ipaddress
- Add variable in between do_shortcode
- How to pass an array as attribute of shortcode to work properly shortcode parser?
- How to put custom admin setting field into a shortcode
- shortcode – I need to throw a message or load a different page
- How to add site tags in alphabetical order to shortcode?
- WordPress post shortcode pagination not working
- Using shortcodes in the Site Title
- Use atributes of shortcode in get template part
- How to display content if user meta data isn’t empty with shortcode
- How to display total user count by specific role in WordPress as statistics?
- User name in referral URLs
- Is there any way to set the Featured image in wordpress post editor, using a Getty images embed code
- Add Style to text in a PHP block
- How I can add placeholder in shortcode
- I’m using rtMedia and Youzer. Can Shortcodes be used to display a video gallery?
- Help with a function to create a shortcode
- How to use strip_shortcodes() from a spawned process?
- Content included from pagination
- Term Description Echo or Return – display problems
- Same Shortcode not executing second time
- Check if Page=current user page via shortcode
- Using shortcode to stop post content from displaying
- Shortcode Attribute contains clone of all shortcode attributes
- How to echo an array returned by a function
- Custom Login in Shortcode got error
- Creating custom shortcode
- What is short code for following?
- Created a shortcode, using it twice (but differently) on a page but its just outputting the same one twice
- Access Shortcode in ajax code?
- Automate post creation in wordpress
- WordPress Not rendering short code properly?
- Shortcode or placeholder for theme options page?
- How to print [embed] code in wordpress [duplicate]
- How to attach sidebar to shortcode’s output?
- Shortcode append to the the_content()
- IFrame Shortcode plugin – issue
- How to call shortcode on button click
- How can you alter the name of attributes in a shortcode?
- Wrapping a function in html tag and return?
- How to exclude some cats from wp_list_categories using shortcut
- How can I create a shortcode that shows a list of categories on the single product page?