据我所知,可以覆盖wordpress中的核心功能,而不必触及核心本身,这显然是有益的。
现在,某些函数是“可插入的”,但并非所有函数都是。
我正在尝试将管理面板中输出特色图像缩略图的功能改写为自定义大小,在核心中这样做并不困难。当我尝试将其放入函数中时,问题就出现了。php作为重命名函数。
这是核心功能,已编辑。我要做的就是重命名它并将其放入函数中。php,但如何让它覆盖“核心”函数本身呢?
function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
global $content_width, $_wp_additional_image_sizes;
$post = get_post( $post );
$upload_iframe_src = esc_url( get_upload_iframe_src(\'image\', $post->ID ) );
$set_thumbnail_link = \'<p class="hide-if-no-js"><a title="\' . esc_attr__( \'Set featured image\' ) . \'" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>\';
$content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( \'Set featured image\' ) );
if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
$old_content_width = $content_width;
$content_width = 700;
if ( !isset( $_wp_additional_image_sizes[\'post-thumbnail\'] ) )
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
else
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, \'post-thumbnail\' );
if ( !empty( $thumbnail_html ) ) {
$ajax_nonce = wp_create_nonce( \'set_post_thumbnail-\' . $post->ID );
$content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
$content .= \'<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\\\'\' . $ajax_nonce . \'\\\');return false;">\' . esc_html__( \'Remove featured image\' ) . \'</a></p>\';
}
$content_width = $old_content_width;
}
return apply_filters( \'admin_post_thumbnail_html\', $content, $post->ID );
}
SO网友:Mark Kaplun
您不需要为此重写核心函数,您需要做的是挂接admin_post_thumbnail_html
滤器
add_filter(\'admin_post_thumbnail_html\', \'wpse107096_wp_post_thumbnail_html\',10, 2);
function wpse107096_wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
global $content_width, $_wp_additional_image_sizes;
$post = get_post( $post );
$upload_iframe_src = esc_url( get_upload_iframe_src(\'image\', $post->ID ) );
$set_thumbnail_link = \'<p class="hide-if-no-js"><a title="\' . esc_attr__( \'Set featured image\' ) . \'" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>\';
$content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( \'Set featured image\' ) );
if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
$old_content_width = $content_width;
$content_width = 700;
if ( !isset( $_wp_additional_image_sizes[\'post-thumbnail\'] ) )
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
else
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, \'post-thumbnail\' );
if ( !empty( $thumbnail_html ) ) {
$ajax_nonce = wp_create_nonce( \'set_post_thumbnail-\' . $post->ID );
$content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
$content .= \'<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\\\'\' . $ajax_nonce . \'\\\');return false;">\' . esc_html__( \'Remove featured image\' ) . \'</a></p>\';
}
$content_width = $old_content_width;
}
return $content;
}
是的,由于重复计算,您将浪费一些CPU周期,但这是正确的方法。如果它困扰着你,那么你应该寻找一种更聪明的方法来设置适当的所需内容大小。