0

I want to add a filter ... but not if it that filter is called by a specific function. Here's a simplified example:

<?php

add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
function wp_trim_excerpt( $text ) {

    $text = apply_filters( 'the_content', $text );

    return $text;

}

add_filter( 'the_content', 'my_content_filter' );
function my_content_filter( $content ) {

    // my code that modifies $content

    return $content;

}

Is there any way to check if my_content_filter is being initiated by wp_trim_excerpt before running my code?

EDIT: The core function wp_trim_excerpt is in play here, so I'd rather not alter the function if possible. Above I simplified it to the most pertinent part.

2 Answers 2

1

WordPress only holds the hooks information in global $wp_filter so you can check if a hook is registered with some specified function or not. you can also use function has_filter.

But I guess you can not check that, it has been called or not on some specific execution point.

But you can do this with PHP using global variable.

Check this code

add_filter( 'get_the_excerpt', 'my_trim_excerpt' );
function my_trim_excerpt($text) {
    global $filter_applied;
    $filter_applied = true;

    $text = wp_trim_excerpt($text);

    return $text;
}

add_filter( 'the_content', 'my_content_filter' );
function my_content_filter( $content ) {
    global $filter_applied;

    if (isset($filter_applied) && $filter_applied === true) {
        //Your code when wp_trim_excerpt is already called.
    }

    //my code that modifies $content

    return $content;
}

Here I am using a global variable $filter_applied and modifying value on wp_trim_excerpt then checking it on my_content_filter

2
  • Thanks for the ideas! Part of my problem is that a core function wp_trim_excerpt (ref) is being used, so I can't edit it. (I should have clarified that above.) $wp_filter isn't providing enough info to differentiate. May 18, 2015 at 17:31
  • Oh I missed that! Check the updated answer now. Hope it will help @AdamCapriola
    – Sumit
    May 18, 2015 at 17:40
0

Here's what I ended up doing:

<?php

add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
function wp_trim_excerpt( $text ) {

    remove_filter( 'the_content', 'my_content_filter' );
    $text = apply_filters( 'the_content', $text );
    add_filter( 'the_content', 'my_content_filter' );

    return $text;

}

add_filter( 'the_content', 'my_content_filter' );
function my_content_filter( $content ) {

    // my code that modifies $content

    return $content;

}

I didn't want to directly edit the wp_trim_excerpt function because it's a core WordPress function (I simplified it for clarity here). However, I couldn't figure out any other great way to do this. So I removed the original get_the_excerpt filter that's in the WordPress core, then duplicated the function in my theme files, and made the edits then reapplied the filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.