Last active
September 16, 2018 15:42
-
-
Save sybrew/a04ad70f42de81469c0d242f1b865e8e to your computer and use it in GitHub Desktop.
Work with TSF v3.0.6 or lower. Disable TSF when shortcodes are found in content.
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 | |
//* Don't include the PHP tag. | |
add_action( 'the_seo_framework_init', function() { | |
//! Set shortcodes here. | |
function get_tsf_excluded_shortcodes() { | |
return array( | |
'my-first-shortcode', | |
'my-first-shortcode', | |
'my-second-shortcode', | |
); | |
} | |
//= Disable titles preemptively. | |
add_filter( 'the_seo_framework_overwrite_titles', '__return_false' ); | |
function reenable_tsf_titles() { | |
$tsf = the_seo_framework(); | |
//* Removes all pre_get_document_title filters. | |
\remove_all_filters( 'pre_get_document_title', false ); | |
//* New WordPress 4.4.0 filter. Hurray! It's also much faster :) | |
\add_filter( 'pre_get_document_title', array( $tsf, 'title_from_cache' ), 10 ); | |
//* Override AnsPress Theme Title | |
\add_filter( 'ap_title', array( $tsf, 'title_from_cache' ), 99, 1 ); | |
//* Override Woo Themes Title | |
\add_filter( 'woo_title', array( $tsf, 'title_from_cache' ), 99 ); | |
/** | |
* Applies filters 'the_seo_framework_manipulate_title' : boolean | |
* Disables the title tag manipulation on old themes. | |
* @since 2.4.1 | |
*/ | |
if ( \apply_filters( 'the_seo_framework_manipulate_title', true ) ) { | |
//* Override WordPress Title | |
\add_filter( 'wp_title', array( $tsf, 'title_from_cache' ), 9, 3 ); | |
} | |
} | |
add_action( 'wp', function() { | |
//= Reset if not singular. | |
if ( ! is_singular() ) { | |
reenable_tsf_titles(); | |
return; | |
} | |
global $post; | |
$tsf = the_seo_framework(); | |
//= Reset if something's wrong. | |
if ( ! isset( $post->post_content ) ) { | |
reenable_tsf_titles(); | |
return; | |
} | |
//= Check if shortcodes are found in the content. | |
$exclude = false; | |
foreach ( get_tsf_excluded_shortcodes() as $shortcode ) { | |
$exclude = has_shortcode( $post->post_content, $shortcode ); | |
if ( $exclude ) break; | |
} | |
if ( $exclude ) { | |
remove_action( 'template_redirect', array( $tsf, '_init_custom_field_redirect' ) ); | |
remove_action( 'wp_head', array( $tsf, 'html_output' ), 1 ); | |
} else { | |
//= Reset if shortcodes aren't found. | |
reenable_tsf_titles(); | |
} | |
}, 0 ); | |
}, -1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This no longer works with TSF v3.1+