Ok this is hard to explain in the heading. But I’ve made this function in my functions.php

Where i add a title tag to all image links with the image caption. By finding the filename and look it up in the DB, finding the ID and by that getting the correct caption.

Here is the function

add_filter('the_content', 'addshadowboxrel', 12);
add_filter('get_comment_text', 'addshadowboxrel');
function addshadowboxrel ($content) {
  global $wpdb;
    $pattern = "/<a(.*?)href=('|")([^>]*).(bmp|gif|jpeg|jpg|png)('|")(.*?)>(.*?)</a>/i";
    $path = '$3.$4';
    $attachment_url = basename($path);  
    $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );
    $attachment = get_post( $attachment_id );
    $replacement = '<a$1href=$2$3.$4$5 title="'.$attachment->post_excerpt.'" rel="lightbox"$6>$7</a>';
    $content = preg_replace($pattern, $replacement, $content);
    return $content;
}

Everything works: If I replace the $path with the actual path insted of ‘$3.$4′.

But the basename function dont work as it is here?

Read more here: Can’t use $1 in basename wordpress filter


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, ,

Wordpress related questions and answers: