Skip to content

Instantly share code, notes, and snippets.

@sybrew
Last active September 16, 2018 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sybrew/a04ad70f42de81469c0d242f1b865e8e to your computer and use it in GitHub Desktop.
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.
<?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 );
@sybrew
Copy link
Author

sybrew commented Sep 16, 2018

This no longer works with TSF v3.1+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment