Hàm lấy tên đường dẫn của file ảnh img cực chất (ok)

http://localhost/reset/wp-content/uploads/2021/04/img-icon-cam-bien-lop.png

function wpdocs_replaceALT( $content ) {
 
     if (  is_front_page() ) {
 
        libxml_use_internal_errors( true );
 
        $post = new DOMDocument();
        $post->loadHTML( $content );
 
        $images = $post->getElementsByTagName( 'img' );
 
        foreach ( $images as $image ) {
 
            if ( !empty( $image->getAttribute( 'alt' ) ) ) {
 
                $src = $image->getAttribute( 'src' );
                $alt = pathinfo( $src, PATHINFO_FILENAME );
 
                $image->setAttribute( 'alt', $alt );
                $image->setAttribute( 'data-src', $alt );
 
            }
        }
 
        $content = $post->saveHTML();
 
        return $content;
    }
}
add_filter( 'the_content', 'wpdocs_replaceALT' );

Last updated