0

GeneratePress, which im using as theme, has the following filter for arhive page post images. It adds a title tag to the a element which im trying to remove.

I'm a bit unsure how to go about this filter, since i have only tried filter that are added to a variable after all functions are run.

The original function:

echo apply_filters( 'generate_featured_image_output', sprintf(
            '<div class="post-image">
                <a href="%1$s" title="%2$s">
                    %3$s
                </a>
            </div>',
            esc_url( get_permalink() ),
            the_title_attribute( 'echo=0' ),
            get_the_post_thumbnail( get_the_ID(), apply_filters( 'generate_page_header_default_size', 'full' ), array('itemprop' => 'image') )
        ));

What i have tried:

add_filter( 'generate_featured_image_output' , 'remove_image_title_archive');
function remove_image_title_archive($text){

        $text = preg_replace('/(title=".*")/', '', $text);
        return $text;


}

Which does nothing.

I have also tried:

add_filter( 'generate_featured_image_output' , 'remove_image_title_archive');
function remove_image_title_archive($text){
    return sprintf(
        '<div class="post-image">
            <a href="%1$s">
                %2$s
            </a>
        </div>',
        esc_url( get_permalink() ),
        get_the_post_thumbnail( get_the_ID(), apply_filters( 'generate_page_header_default_size', 'full' ), array('itemprop' => 'image') )
    );
}

I have also tried setting the priority higher on both, but no effect.

Whats the procedure when filtering a sprintf function like this one?

  • In your first try with preg_replace, you have mixed up your variable names ($test vs $text). Other than that, it doesn't look wrong to me, and sprintf just returns the string, so for all intents and purposes, it shouldn't matter to the filter if sprintf was used or a hardcoded string. – janh Nov 6 '17 at 9:33
  • I noticed the typo with $test and $text right after posting. Didn't change anything though. I also dont see why i shouldnt be able to return an entirely different sprintf function. Yet it doesn't work. – Emil Østervig Nov 6 '17 at 10:33
0

This should have worked (the part of the sprintf at least as I don't have access to the original code.

Try give a higher number:

add_filter( 'generate_featured_image_output' , 'remove_image_title_archive',100);

Another solution will be removing the filter before applying yours, like this:

remove_filter( 'generate_featured_image_output', 'remove_image_title_archive',10 );

Important: To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

  • I already tried giving it 99 and 999 priority. No effect. remove_image_title_archive is my own function. The first snippet in my post is the original code from the theme. It's all there is in the function. Is it possible to use remove_filter an unnamed filter? – Emil Østervig Nov 6 '17 at 10:35
  • @EmilØstervig you are correct you need the function name, so you can not call the remove_filter. Where do you insert the filter? – Drupalizeme Nov 6 '17 at 11:13
  • I'm adding my own add_filter in my childtheme functions.php. The original function where the filter is applied with sprintf is in file for the mothertheme – Emil Østervig Nov 6 '17 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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