Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9053087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:20:23+00:00 2026-06-16T13:20:23+00:00

So this is for WordPress. What I’m trying to do is create a little

  • 0

So this is for WordPress. What I’m trying to do is create a little modification within the functions.php file of my theme. All I want to do is get the ID of the WordPress image and then add a <div> after the image itself IF the image contains the class dd-star-rating.

Notes:

  • The image ID is within the “class” attribute. It will be something like wp-image-X (where X is the ID of the image). I want this stored in a variable called $id

  • I want it to add <div>Star Rating ID = $id</div> AFTER each image that has the class dd-star-rating

Heres an example of what is stored in the $content variable:

<p>Nunc et neque risus. Nam a nisl eu magna rutrum euismod ac in lorem. Aenean varius accumsan ligula tincidunt malesuada. In diam lectus, pharetra quis cursus in, egestas vitae tellus. In neque arcu, aliquet ut egestas in, ultrices id ligula. Suspendisse ut dolor ligula, sit amet placerat ligula. Ut viverra nisi id ante facilisis pharetra. Aenean scelerisque, leo eget accumsan condimentum, odio libero ultrices enim, pretium placerat sapien purus nec odio. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Curabitur bibendum viverra pellentesque. Pellentesque eu diam non eros consectetur semper nec sed nisl.</p>

<p style="text-align: center;"><img class="size-full wp-image-44 aligncenter concept dd-star-rating" alt="concept" src="http://www.mysite.com/wp-content/uploads/2012/12/concept.png" width="720" height="540" /></p>

<p style="text-align: center;"><img class="size-full wp-image-45 aligncenter concept dd-star-rating" alt="concept" src="http://www.mysite.com/wp-content/uploads/2012/12/concept2.png" width="720" height="540" /></p>

<p>Nulla eros ante, lobortis in convallis eget, dignissim ut quam. Proin nisi nunc, iaculis ac auctor eu, tincidunt ac augue. Nunc purus nisi, sollicitudin nec luctus ullamcorper, dictum id magna. Sed quis nisi sagittis sapien placerat semper vitae tempus leo. Suspendisse sem diam, eleifend in blandit sed, pellentesque ac nisl. Morbi tincidunt adipiscing augue in pharetra. Duis dapibus bibendum egestas. Phasellus dictum dapibus quam id fermentum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>

So for those 2 images, since they contain the dd-star-rating class, I want the value of the $content variable to change to this:

<p>Nunc et neque risus. Nam a nisl eu magna rutrum euismod ac in lorem. Aenean varius accumsan ligula tincidunt malesuada. In diam lectus, pharetra quis cursus in, egestas vitae tellus. In neque arcu, aliquet ut egestas in, ultrices id ligula. Suspendisse ut dolor ligula, sit amet placerat ligula. Ut viverra nisi id ante facilisis pharetra. Aenean scelerisque, leo eget accumsan condimentum, odio libero ultrices enim, pretium placerat sapien purus nec odio. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Curabitur bibendum viverra pellentesque. Pellentesque eu diam non eros consectetur semper nec sed nisl.</p>

<p style="text-align: center;"><img class="size-full wp-image-44 aligncenter concept dd-star-rating" alt="concept" src="http://www.mysite.com/wp-content/uploads/2012/12/concept.png" width="720" height="540" /></p>
<div>Star Rating ID = 44</div>

<p style="text-align: center;"><img class="size-full wp-image-45 aligncenter concept dd-star-rating" alt="concept" src="http://www.mysite.com/wp-content/uploads/2012/12/concept2.png" width="720" height="540" /></p>
<div>Star Rating ID = 45</div>

<p>Nulla eros ante, lobortis in convallis eget, dignissim ut quam. Proin nisi nunc, iaculis ac auctor eu, tincidunt ac augue. Nunc purus nisi, sollicitudin nec luctus ullamcorper, dictum id magna. Sed quis nisi sagittis sapien placerat semper vitae tempus leo. Suspendisse sem diam, eleifend in blandit sed, pellentesque ac nisl. Morbi tincidunt adipiscing augue in pharetra. Duis dapibus bibendum egestas. Phasellus dictum dapibus quam id fermentum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>

Now here is my code so far, in my themes functions.php file:

add_filter( 'the_content' , 'dd_wrap_image' );

function dd_wrap_image ( $content )
{    
    $dom = new DOMDocument;
    $dom->loadHTML($content);
    $imgs = $dom->getElementsByTagName('img');

    foreach($imgs as $img)
    {
        if ($img->hasAttribute('class') && strstr($img->getAttribute('class'), 'dd-star-rating'))
        {                
            // get the "wp-image-X" ID and store into $id variable
            // add "<div>Star Rating ID = $id</div>" after image

            $class_array = explode(' ', $img->getAttribute('class'));

            foreach ($class_array as $k => $v)
            {
                if (strstr($v, 'wp-image'))
                {
                    $id = end(explode('-', $v));
                }
            }

            if (!empty($id))
            {
                $img->appendChild("<div>Star Rating ID = $id</div>");
            }            
        }
    }

    $dom->saveHTML();
    return $content;
}

As you can see, I need some help inside of the conditional statement. Is a regular expression match best for figuring out the ID with a preg_match()? What is the best way to add the <div> after each image? Use str_replace()?

EDIT:

Updated my code. But now getting error Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, string given

EDIT AGAIN:

Thanks alot hakre! However, what if I wanted to add some larger code like the following after each image?

Lets say the following code is stored in a variable called $star_code, how would I insert this after the image using DOM?

<div class="dd-star-poll" id="img-$id">

    <ul class="dd-star-poll-options">
    <li><a href='#' title='1 star out of 5' class='one-star'>1</a></li>
    <li><a href='#' title='2 stars out of 5' class='two-stars'>2</a></li>
    <li><a href='#' title='3 stars out of 5' class='three-stars'>3</a></li>
    <li><a href='#' title='4 stars out of 5' class='four-stars'>4</a></li>
    <li><a href='#' title='5 stars out of 5' class='five-stars'>5</a></li>    
    </ul>

    <div class="dd-star-poll-results">
    Currently 3.5/5 Stars.
    </div>

</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:20:25+00:00Added an answer on June 16, 2026 at 1:20 pm

    The final problem you got is because you do not add the new <div> element by string but by an object. For example:

    $class->appendChild(new DOMElement('div', "Star Rating ID = $id"));
    

    However I would also suggest you to make use of XPath in your case. It does allow you to select only those img elements that have the classes as strings you’re looking for. At least at the same level as you wrote in your PHP code. So you can spare some code.

    An Example selecting all attributes that do match, then parsing them (with a regular expression) to obtain the ID:

    $doc = new DOMDocument;
    $doc->loadHTML("<body>$html</body>");
    
    $xp = new DOMXPath($doc);
    $images = $xp->query(
        '//img[@class
               and contains(@class, "dd-star-rating") 
               and contains(@class, "wp-image-")
           ]/@class'
    );
    
    foreach ($images as $class) {
        $id = preg_match('/(?:^|\s)wp-image-(\d+)(?:$|\s)/', $class->nodeValue, $matches) ? $matches[1] : NULL;
        $class->appendChild(new DOMElement('div', "Star Rating ID = $id"));
    }
    

    Result:

    <p>Nunc et neque risus. Nam a nisl eu magna rutrum euismod ac in lorem. Aenean varius accumsan ligula tincidunt malesuada. In diam lectus, pharetra quis cursus in, egestas vitae tellus. In neque arcu, aliquet ut egestas in, ultrices id ligula. Suspendisse ut dolor ligula, sit amet placerat ligula. Ut viverra nisi id ante facilisis pharetra. Aenean scelerisque, leo eget accumsan condimentum, odio libero ultrices enim, pretium placerat sapien purus nec odio. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Curabitur bibendum viverra pellentesque. Pellentesque eu diam non eros consectetur semper nec sed nisl.</p>
    
    <p style="text-align: center;"><img class="size-full wp-image-44 aligncenter concept dd-star-rating" alt="concept" src="http://www.mysite.com/wp-content/uploads/2012/12/concept.png" width="720" height="540"></p>
    <div>Star Rating ID = 44</div>
    
    <p style="text-align: center;"><img class="size-full wp-image-45 aligncenter concept dd-star-rating" alt="concept" src="http://www.mysite.com/wp-content/uploads/2012/12/concept2.png" width="720" height="540"></p>
    <div>Star Rating ID = 45</div>
    
    <p>Nulla eros ante, lobortis in convallis eget, dignissim ut quam. Proin nisi nunc, iaculis ac auctor eu, tincidunt ac augue. Nunc purus nisi, sollicitudin nec luctus ullamcorper, dictum id magna. Sed quis nisi sagittis sapien placerat semper vitae tempus leo. Suspendisse sem diam, eleifend in blandit sed, pellentesque ac nisl. Morbi tincidunt adipiscing augue in pharetra. Duis dapibus bibendum egestas. Phasellus dictum dapibus quam id fermentum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
    

    Adding more elements works by working with the return value of DOMElelemnt::appendChild. Example:

    $prefix = 'dd-star-poll';
    
    $divOuter = $node->appendChild(new DOMElement('div'));
    $divOuter->setAttribute('class', $prefix);
    $divOuter->setAttribute('id', "img-$id");
    
    $ul = $divOuter->appendChild(new DOMElement('ul'));
    $ul->setAttribute('class', "$prefix-options");
    
    ...
    

    You just continue to add the elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to mess with this wordpress theme and can't figure out why the sidebar
I posted this earlier on wordpress.stackexchange.com. However, never got a reply. Hence, trying my
I'm working on this website in WordPress and I want the content with the
I have this wordpress plugin for shorcodes. I´m trying to implement this in my
This is the anatomy of a wordpress plugin: .twitter-plugin |-images/ |-index.php +-README The directory
....maybe not so simple. First, I am trying to code this in Wordpress, not
I want to create a network of sites on the WordPress platform. Apartment Therapy
I have this WordPress theme installed. it works perfectly on a domain. When I
please check this wordpress script for me, it is not working. I want to
So I've run into a wall trying to crack why this WordPress install hogs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.