post_excerpt ) ? $p->post_excerpt : $p->post_content; $text = trim( preg_replace( '/\s+/u', ' ', wp_strip_all_tags( strip_shortcodes( excerpt_remove_blocks( $text ) ) ) ) ); if ( function_exists( 'mb_strlen' ) && mb_strlen( $text ) > 155 ) { $cut = mb_substr( $text, 0, 154 ); $pos = mb_strrpos( $cut, ' ' ); $text = rtrim( false !== $pos && $pos > 100 ? mb_substr( $cut, 0, $pos ) : $cut, " ,.;:–-" ) . '…'; } return $text; } /** * Fill an empty Yoast description on single sermons. * * @param string $desc Description from Yoast. * @return string */ function fegp_seo_filter_desc( $desc ) { if ( '' !== trim( (string) $desc ) || ! is_singular( 'sermons' ) ) { return $desc; } $own = fegp_seo_description( get_queried_object_id() ); return '' !== $own ? $own : $desc; } /** * Sermon taxonomies kept out of the index. * * @return string[] */ function fegp_seo_taxonomies() { return array( 'sermon_speaker', 'sermon_topics', 'sermon_book', 'sermon_series' ); } /** * Whether the current request is one of these taxonomy archives. * * @return bool */ function fegp_seo_is_sermon_tax() { return is_tax( fegp_seo_taxonomies() ); } /** * Robots as array (Yoast 14+). * * @param array $robots Robots values. * @return array */ function fegp_seo_robots_array( $robots ) { if ( fegp_seo_is_sermon_tax() ) { $robots['index'] = 'noindex'; $robots['follow'] = 'follow'; } return $robots; } /** * Robots as string (older Yoast versions). * * @param string $robots Robots string. * @return string */ function fegp_seo_robots( $robots ) { if ( ! fegp_seo_is_sermon_tax() ) { return $robots; } $parts = array_filter( array_map( 'trim', explode( ',', (string) $robots ) ) ); $parts = array_diff( $parts, array( 'index', 'noindex', 'follow', 'nofollow' ) ); return implode( ', ', array_merge( array( 'noindex', 'follow' ), $parts ) ); } /** * Leave sermon taxonomies out of the Yoast sitemap. * * @param bool $exclude Excluded. * @param string $taxonomy Taxonomy. * @return bool */ function fegp_seo_sitemap_tax( $exclude, $taxonomy ) { return in_array( $taxonomy, fegp_seo_taxonomies(), true ) ? true : $exclude; } /** * Leave the 'archive_pages' post type (Advanced Sermons) out of the Yoast sitemap. * * @param bool $exclude Excluded. * @param string $post_type Post type. * @return bool */ function fegp_seo_sitemap_post_type( $exclude, $post_type ) { return 'archive_pages' === $post_type ? true : $exclude; } /** * Register the filters only when Yoast SEO is active. */ function fegp_seo_init() { if ( ! defined( 'WPSEO_VERSION' ) ) { return; } add_filter( 'wpseo_metadesc', 'fegp_seo_filter_desc' ); add_filter( 'wpseo_opengraph_desc', 'fegp_seo_filter_desc' ); $o = fegp_options(); if ( ! empty( $o['seo_noindex_tax'] ) ) { add_filter( 'wpseo_robots_array', 'fegp_seo_robots_array' ); add_filter( 'wpseo_robots', 'fegp_seo_robots' ); add_filter( 'wpseo_sitemap_exclude_taxonomy', 'fegp_seo_sitemap_tax', 10, 2 ); add_filter( 'wpseo_sitemap_exclude_post_type', 'fegp_seo_sitemap_post_type', 10, 2 ); } } add_action( 'plugins_loaded', 'fegp_seo_init', 20 );