Restrict filter to run only inside specific function

wp_terms_checklist early trigger a filter ‘wp_terms_checklist_args’ so you can use that filter to add your filter and auto-remove it haver 1st run. This should be enough, however, once hooks are global variables, to be sure is better use some stronger check, a static variable inside a function is a simple and nice trick: function switch_terms_filter( … Read more

Customize the “Registration complete. Please check your e-mail.” message on WP 4.0

If you need an alternative way, you can always hook into the login_init and modify the gettext filter: add_filter( ‘login_init’, function() { add_filter( ‘gettext’, ‘wpse_161709’, 99, 3 ); } ); function wpse_161709( $translated_text, $untranslated_text, $domain ) { $old = “Registration complete. Please check your e-mail.”; $new = “New text here”; if ( $untranslated_text === $old … Read more

How Can I Have A URL Changed Based on the Originating URL?

You can use the page_link filter with the same logic as the page_template filter in the other answer. If the endpoint query var is set, then any link to a page gets the app-view/ appended: function wpd_page_link( $link, $post_id ){ global $wp_query; if( isset( $wp_query->query_vars[‘app-view’] ) ){ return $link . ‘app-view/’; } return $link; } … Read more

Some questions regarding filter

It depends whether you’re trying to hook to an existing filter or hook to one you’ve made, looking at your second code sample suggests you’re asking about making your own filter. And, yes you can add your own, something like this is valid inside your loop. <?php echo apply_filters( ‘my_blah_filter’, ‘<p>Blah blah some text goes … Read more

Can’t change the title tag with wp_title filter

When adding title-tag support in a theme, the title tag can be filtered by several filters, but not wp_title. The reason is that if the theme supports title-tag, WordPress uses wp_get_document_title() instead of wp_title(). For themes with support for title-tag you can use document_title_parts: add_filter( ‘document_title_parts’, ‘filter_document_title_parts’ ); function filter_document_title_parts( $title_parts ) { $title_parts[‘title’] = … Read more

Filter the blog’s title without using global variables

You can encapsulate the logic in a class: class NameSwitch { private $state = false; private $string; public function __construct( $string ) { $this->string = $string; } public function change_state() { $this->state = ! $this->state; return $this->state; } public function replace( $output, $show = NULL ) { if ( ‘name’ !== $show ) return $output; … Read more

Format content value from DB outside of WordPress filters

There are a few filters and actions here that run. You’d need to look at the code and figure out what/how to utilize these. https://codex.wordpress.org/Function_Reference/wpautop https://codex.wordpress.org/Function_Reference/wptexturize https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content https://developer.wordpress.org/reference/functions/do_shortcode/ https://developer.wordpress.org/reference/functions/unescape_invalid_shortcodes/ https://developer.wordpress.org/reference/functions/get_shortcode_regex/

End excerpt at the end of the sentence

Use this function instead. Then put the_excerpt(); in your template. /** * Find the last period in the excerpt and remove everything after it. * If no period is found, just return the entire excerpt. * * @param string $excerpt The post excerpt. */ function end_with_sentence( $excerpt ) { if ( ( $pos = mb_strrpos( … Read more