Make WordPress Core

Opened 7 years ago

Closed 2 months ago

Last modified 2 months ago

#45955 closed enhancement (wontfix)

Twenty Nineteen: get_the_archive_title filter issues

Reported by: taskotr's profile taskotr Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Bundled Theme Keywords: has-patch close
Focuses: Cc:

Description

I was testing the theme with a plugin that adds a custom post type and allows to set a custom title for the custom post type archive view using the following filter:

add_filter( 'get_the_archive_title', 'slug_set_the_archive_title' );

While this filter works fine in the previous WordPress themes, such as Twenty Seventeen, Twenty Sixteen and Twenty Fifteen, it does not work in Twenty Nineteen.

It's possible to fix it by setting a priority parameter in the filter. For example:

add_filter( 'get_the_archive_title', 'slug_set_the_archive_title', 99 );

The problem is that it breaks a header style, meaning the title will have the style of the archive view prefix text (grey color and serif font style).

Any idea what is the better way to use this filter in the
Twenty Nineteen theme?

Attachments (1)

45955.diff (610 bytes) - added by mukesh27 7 years ago.
Patch to change theme filter priority

Download all attachments as: .zip

Change History (10)

#1 @mukesh27
7 years ago

  • Keywords 2nd-opinion needs-patch added
  • Type changed from defect (bug) to enhancement

@taskotr Theme already used add_filter( 'get_the_archive_title', 'twentynineteen_get_the_archive_title' ); archive filter with priority of 10 so if you want to override that filter with your plugin functionality then you have to add high priority then 10 then your filter works in Twenty Nineteen.

For ex.

If you have develop plugin with below filter then add below priority.

add_filter( 'get_the_archive_title', 'slug_set_the_archive_title', 99 );

If you have use any third party plugin and it's filter does not work then remove theme filter using below code.

remove_filter( 'get_the_archive_title', 'twentynineteen_get_the_archive_title' );

For better solution, In Twenty Nineteen theme we needs to change filter priority to 1 instead of 10 so plugin can override filter as per there needs.

Version 0, edited 7 years ago by mukesh27 (next)

@mukesh27
7 years ago

Patch to change theme filter priority

#2 @mukesh27
7 years ago

  • Keywords has-patch added; needs-patch removed

Attached patch change filter priority to 1 instead of 10 so plugin can override filter as per there needs.

#3 @karmatosed
23 months ago

  • Keywords needs-testing added; dev-feedback 2nd-opinion removed

This ticket was mentioned in PR #10734 on WordPress/wordpress-develop by @huzaifaalmesbah.


2 months ago
#4

Description
Changes the priority of the get_the_archive_title filter in twentynineteen_get_the_archive_title from 10 to 1. This allows plugins and child themes to override the archive title using the default priority of 10, resolving conflict issues where the theme would forcefully overwrite plugin customizations.

This PR refreshes the initial patch provided by @mukesh27.

I tested the patch and confirmed that everything behaves as expected. The patch just needed a refresh, so I am submitting this PR to update the initial patch.

Tickets: https://core.trac.wordpress.org/ticket/45955

Steps to Reproduce

  1. Install and activate the Twenty Nineteen theme.
  2. Create and activate a helper plugin (or add to child theme's functions.php) with the following code to override the archive title at the default priority (10):
    <?php
            /**
             * Plugin Name: Test Trac 45955
             */
            
            function trac_test_archive_title( $title ) {
                    return 'TEST PASSED: Plugin Override Successful';
            }
            add_filter( 'get_the_archive_title', 'trac_test_archive_title' );
    
  1. Visit any archive page on the frontend (e.g., a Category, Tag, or Author archive).
  2. Observe that the page title is NOT changed to "TEST PASSED...". It remains the default theme title (e.g., "Category Archives: ...").

Test Report

  1. Apply the patch.
  2. Visit the same archive page as above.
  3. Observe that the page title IS now changed to "TEST PASSED: Plugin Override Successful".
  4. This confirms the theme now allows plugins to override the title at the default priority.

#5 @westonruter
2 months ago

If this change is applied to Twenty Nineteen, shouldn't it also be applied to Twenty Twenty? It looks like it has the same issue.

#6 @huzaifaalmesbah
2 months ago

Thanks for the suggestion, @westonruter.

I’ve applied the same change to Twenty Twenty as well, since it had the same issue. The update is included in the latest commit on PR please check it out and let me know if everything looks good now.

#7 @sabernhardt
2 months ago

  • Keywords close added; needs-testing removed

Changing the priority now would cause a problem in any site that uses remove_filter() for these hooks at priority 10.

To remove a hook, the $callback and $priority arguments must match when the hook was added.

Using an earlier priority might have been appropriate before the themes were publicly available, but changing it after more than five years is not good.

Any filters that adjust the themes' output should already use a priority of at least 11, or else the site should remove the default filter at priority 10.
remove_filter( 'get_the_archive_title', 'twentynineteen_get_the_archive_title' );

#8 @westonruter
2 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Good point. Yeah, this will likely break some sites. Better to just require a higher priority to ensure the filter applies as desired.

@westonruter commented on PR #10734:


2 months ago
#9

Closing per https://core.trac.wordpress.org/ticket/45955#comment:7

Thank you for opening a PR!

Note: See TracTickets for help on using tickets.