我正在使用这段代码在WordPress中嵌入要点。它工作得很好,但我的debug.log文件中一直有一个错误:PHP注意:未定义的偏移量:2在/functions.php中的第333行中。
esc_attr($matches[2])**)** (第333行为)
wp_embed_register_handler( 'gist', '/https?:\/\/gist\.github\.com\/([a-z0-9]+)(\?file=.*)?/i', 'embed_handler_gist' );
function embed_handler_gist( $matches, $attr, $url, $rawattr ) {
$embed = sprintf(
'<script src="https://gist.github.com/%1$s.js%2$s"></script>',
esc_attr($matches[1]),
esc_attr($matches[2])
);
return apply_filters( 'embed_gist', $embed, $matches, $attr, $url, $rawattr );
}发布于 2015-10-28 01:59:25
这个错误仅仅意味着$matches[2]不存在。可以通过在尝试访问变量之前检查是否存在此错误来避免此错误。
if( isset($matches[2]) )
esc_attr($matches[2]);或者,如果您希望分配一个默认值:
$value = isset($matches[2]) ? $matches[2] : false;isset()
https://stackoverflow.com/questions/33380683
复制相似问题