I have a wordpress plugin that displays videos through a shortcode. My problem is it assigns the div's with ID's such as video-1-player, video-2-player, etc. I need to create a function that searches my content for <div id="video-(any number here)-player"> and adds a class inside the div.

I found this code which searches for the first image and than you can print it. My PHP isn't good enough for me to edit it for how i need. Maybe this will help you?

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  return $first_img;

2 Answers 2


Try the Search and Replace plugin: http://wordpress.org/extend/plugins/search-and-replace/

This searches for strings and will allow you to search for every instance of video-x-player, using a regex for the number. If you don't want to use regex searching on video- will bring up every instance that is already in your database.

If you intend to keep using that video plugin you may wish to edit the plugin to give you the ID's you want or to automatically add class names.

  • I dont need a plugin. I need a simple small function... Commented Jun 10, 2011 at 11:58
  • If it's such a simple, small function, why bother asking for help with it? The best approach is to modify the Video Player Plugin's shortcode function definition, so that it outputs IDs in the format that you want. You're not going to get that with a "simple, small function." Commented Jun 10, 2011 at 17:40
  • The reason I ask help with it, is because my knowledge on PHP is vary minimal. Anyone who really knew PHP would be able to give me a code. Especially with the reference code above. Its basically what I need except instead of 'finding' the first image and putting it in a variable, I need to find an ID and add a class... Commented Jun 11, 2011 at 18:23

Why do you need a class inside the <div>? Are you trying to apply CSS styles to it?

If so, why not just use CSS? For example:

div[id^="video-"] {} 

...will target any DIV whose ID value starts with "video-"

  • It is for CSS, and even tho i didn't think about using the 'starts with' for my style this still wouldn't work. The shortcode outputs 4 nested divs. All having id="video-" in the beginning, (but the ending is whats different). I dont want to style to all the divs. And another note is this needs to work with IE7. Im not sure if this method does... Commented Jun 11, 2011 at 18:27
  • Which div in the nest do you want to style? The first? The last? One in the middle? Commented Jun 11, 2011 at 19:49
  • I would like to get the first div. From there, any of its children are straight forward to get at. I just dont want to write styles for the top div, and than have to write counter-styles for its children. Commented Jun 11, 2011 at 19:53
  • What's the problem with writing "counter" styles for child DIVs? Commented Jun 11, 2011 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.