wordpress - Find element with class and add one more in php string -
i'm trying make own filter audio shortcode in wordpress. find element class , add 1 more. i've got string html code , str_replace doesn't work.
<div id="mep_0" class="mejs-container svg mejs-audio" tabindex="0" role="application" aria-label="audio player" style="width: 416px; height: 30px;">
i add 'test-class' div 'mejs-container'. want add id ('mep_0') not fixed.
you can use xpath purpose.
this simple example:
$dom = '<div id="mep_0" class="mejs-container svg mejs-audio" tabindex="1" role="application" aria-label="audio player" style="width: 416px; height: 30px;"> <div id="mep_1" class="mejs-container svg mejs-audio" tabindex="0" role="application" aria-label="audio player" style="width: 416px; height: 30px;"> <div id="mep_2" class="mejs-container svg mejs-audio" tabindex="0" role="application" aria-label="audio player" style="width: 416px; height: 30px;">'; $xpath = new domxpath($dom); // read xml/html dom manipulate or search stuff $elements = $xpath->query('//div[contains(@class,"mejs-container")]'); // search elements contain "mejs-container" class // loop through each found element foreach($elements $element) { // current class attributes content // give "mejs-container svg mejs-audio" $currentclass = $element->getattribute('class'); // append "test-class" current value , set in dom (mind space between classes) $element->setattribute($currentclass . " test-class"); }
Comments
Post a Comment