dpqaaczn141761 2014-06-26 11:25
浏览 25

在basename wordpress过滤器中不能使用$ 1

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?

  • 写回答

1条回答 默认 最新

  • doulian8742 2014-06-26 11:31
    关注

    Where have you declared $3 and $4? Variable names cannot start with a number. So:

    $3
    $4
    

    Cannot be valid variables. Can you change them to:

    $n3
    $n4
    

    Instead? And try! This could be basic but this WOULD be the reason! :)

    Also consider using the function preg_match_all. Check out Pattern Syntax for more details.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题