从img_caption_shortcode()

时间:2017-12-17 21:23:21

标签: wordpress

我试图修改函数img_caption_shortcode()的href输出。但我似乎无法在那里找到 href 。它还能在哪里找到?

现在输出如下

<figure id="attachment_123" class="panel wp-caption alignleft">
    <div class="panel-body">
        <a class="caption-link" href="http://localhost/alpha/wp-content/uploads/ORIGINAL-IMAGE.jpg" data-toggle="lightbox" data-title="Caption Text">
            <img class="alignleft size-tiny wp-image-123" src="http://localhost/alpha/wp-content/uploads/TINY.jpg" alt="" width="200" height="200" />
        </a>
    </div>
<figcaption class="panel-footer caption wp-caption-text">Caption Text</figcaption>
</figure>

我绝望地尝试获得以下输出:

<figure id="attachment_123" class="panel wp-caption alignleft">
    <div class="panel-body">
        <a class="caption-link" href="http://localhost/alpha/wp-content/uploads/LARGE-IMAGE.jpg" data-toggle="lightbox" data-title="Caption Text">
            <img class="alignleft size-tiny wp-image-123" src="http://localhost/alpha/wp-content/uploads/TINY.jpg" alt="" width="200" height="200" />
        </a>
    </div>
<figcaption class="panel-footer caption wp-caption-text">Caption Text</figcaption>
</figure>

Wordpress代码:

function img_caption_shortcode( $attr, $content = null ) {
// New-style shortcode with the caption inside the shortcode with the link and image tags.
if ( ! isset( $attr['caption'] ) ) {
    if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
        $content = $matches[1];
        $attr['caption'] = trim( $matches[2] );
    }
} elseif ( strpos( $attr['caption'], '<' ) !== false ) {
    $attr['caption'] = wp_kses( $attr['caption'], 'post' );
}

href是否隐藏在正则表达式中?

请给我一个正确的方向。我已经谷歌搜索了几个小时,但我似乎无法找到我需要的东西。也许只是给我一个合适的关键词。

1 个答案:

答案 0 :(得分:0)

我现在已经找到了解决问题的方法。抱歉,我提出了一个问题。

但对于遇到同样问题的其他人。以下代码允许我修改figure元素的href输出。

function html5_insert_image( $html, $id, $caption, $title, $align, $url, $size, $alt ) {

    $src  = wp_get_attachment_image_src( $id, $size, false );
    $url_large  = wp_get_attachment_image_src( $id, 'large', false );

    $output  = '<figure id="post-'.$id.' media-'.$id.'" class="panel wp-caption align'.$align.'">'; 
    $output .= '<div class="panel-body">';

    $the_caption = $caption ? $caption : '';

    if ( $url ) {
    $output .= '<a data-toggle="lightbox" data-title="'.$the_caption.'" href="'.$url_large[0].'" class="caption-link"><img src="'.$src[0].'" alt="'.$alt.'" /></a>';
    } else {
        $output .= '<img src="'.$src[0].'" alt="'.$alt.'" />';
     }

    if ( $caption ) {
        $output .= '</div><figcaption class="panel-footer caption wp-caption-text">'.$the_caption.'</figcaption>';
    } else {
        $output .= '</div>';
    }
    $output .= '</figure>';

    return $output;
}

add_filter('image_send_to_editor', 'html5_insert_image', 10, 9);

干杯。