rankmath / seo-by-rank-math

Rank Math is a revolutionary WordPress SEO Plugin that combines the features of many SEO tools and lets you multiply your traffic in the easiest way possible :bulb: :chart_with_upwards_trend: →
https://rankmath.com
107 stars 52 forks source link

Breadcrumb filter hooks passing original values #270

Open RavanH opened 6 months ago

RavanH commented 6 months ago

Is your feature request related to a problem? Please describe.

The breadcrumb filters rank_math/frontend/breadcrumb/settings and rank_math/frontend/breadcrumb/strings initially pas an empty array, instead of the default settings and strings. This makes them less useful than they could be.

For example, to get translation strings from a plugin like Polylang, we need the original string. If the rank_math/frontend/breadcrumb/strings would pass the original strings (instead of an empty array) then the original string could be used like pll__( $string ); to get the translation dynamically. Without it, the filter must jump to many more hoops to first fetch the original string, then return the translation.

Describe the solution you'd like

If the original values are passed to the filter, then any added filters can use them if needed.

Describe alternatives you've considered

A filter can hardcode translation strings, but if the original value is changed in the Rank Math settings, the filter code must be adapted accordingly.

A filter can do the work to fetch the original values, but will depend on Rank Math specific functions. This makes them less future proof. For instance, the filter might use RankMath\Helper::get_settings( 'general.breadcrumbs_prefix' ) to get the original prefix setting, but if the Helper class is changed at any point in the future, the code will break.

Additional context

An example of a filter that I expected to work but fails, due to an empty array:

add_filter( 'rank_math/frontend/breadcrumb/strings', function( $strings ) { 
    $strings['prefix']    = pll__( $strings['prefix'] ); 
    $strings['home']      = pll__( $strings['home'] ); 
    $strings['home_link'] = pll_home_url( pll_current_language() );
    $strings['error404']  = pll__( $strings['error404']  );

    return $strings;
} );

Instead, the following is needed:

add_filter( 'rank_math/frontend/breadcrumb/strings', function( $strings ) { 
    $strings['prefix']    = pll__( RankMath\Helper::get_settings( 'general.breadcrumbs_prefix' ) ); 
    $strings['home']      = pll__( RankMath\Helper::get_settings( 'general.breadcrumbs_home_label' ) ); 
    $strings['home_link'] = pll_home_url( pll_current_language() );
    $strings['error404']  = pll__( RankMath\Helper::get_settings( 'general.breadcrumbs_404_label' ) );

    return $strings;
} );

Please let filters be filters :)