Last active
November 15, 2023 06:24
-
-
Save hamidrezayazdani/89f8ef51045cf799977bebf7c96c53bc to your computer and use it in GitHub Desktop.
Post reading time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ywp_reading_time() { | |
global $post; | |
if ( empty( $post ) ) { | |
$post = get_the_ID(); | |
} | |
if ( empty( $post ) ) { | |
return; | |
} | |
$post_id = is_numeric( $post ) ? $post : $post->ID; | |
// If yoast estimated the time, skip estimate | |
$yoast_meta = get_post_meta( $post_id, '_yoast_wpseo_estimated-reading-time-minutes', true ); | |
if ( ! empty( $yoast_meta ) ) { | |
return $yoast_meta . ' دقیقه'; | |
} | |
$content = wp_strip_all_tags( get_the_content( $post_id ) ); | |
if ( empty( $content ) ) { | |
return; | |
} | |
// Words count (alphabetic letters, digits, delimited phrases with hyphen/dash) | |
preg_match_all('/[\pL\pN\pPd]+/u', $content, $matches); | |
return ceil( count( $matches[0] ) / 200 ) . ' دقیقه'; | |
} | |
add_shortcode('wpread', 'ywp_reading_time'); | |
// The code goes to your active theme/child theme functions.php file. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment