Reputation: 5493
I'm using a custom shortcode to display a Bootstrap Modal inside my content. The problem is, that the <div>
breaks the content. See here: Output content of WordPress Shortcode in text and footer
No2 I want to change the shortcode and display only the link of the Modal and display the modal-<div>
after the content.
To do so, I check if the content field has shortcodes and if that's the case, I display all modals after the content.
Here's the code for that: (Part from here: https://stackoverflow.com/a/18196564/1788961)
$content = get_sub_field("textfield");
//write the begining of the shortcode
$shortcode = 'term';
$check = strpos($content,$shortcode);
if($check=== false) {
//echo '<h1>NO Shortcode</h1>';
} else {
//echo '<h1>HAS Shortcode</h1>';
$str = '[term value="Term Name" id="600"][term value="Another Term" id="609"]';
preg_match_all('~\[term value="(.+?)" id="(.+?)"]~', $str, $matches);
var_dump($matches[2]);
foreach($matches[2] as $match){
echo
'<div class="modal fade" id="termModal_'.$match.'" tabindex="-1" role="dialog" aria-labelledby="termModal_'.$match.'_Title" aria-hidden="true">
(rest of modal)
</div>
';
}
}
Everything works fine so far. But now I need the shortcodes from the content field.
I don't know how to get them. Here's my Shortcode:
[term value="Custom Link Title" id="123"]
I need the id from every Shortcode inside the content and store them in the `$str' variable.
Upvotes: 1
Views: 832
Reputation: 348
This method using the preg_match()
function should work:
$id = 0;
$matches = array();
preg_match('#\[term(.*) id="([0-9]{1,})"(.*)\]#', $content, $matches);
if (count($matches) > 2) {
$id = $matches[2];
}
That will work irrespective of the order the shortcode attribures are in but it assumes that you have double quotes around the id
attribute value and that the value consists of only digits.
Upvotes: 1