presscustomizr / hueman

Hueman WordPress theme is one of the best rated theme for blogs and magazines on WordPress.org. Powers 70k+ websites around the world.
https://presscustomizr.com/hueman/
GNU General Public License v3.0
570 stars 166 forks source link

The function hu_is_authorized_tmpl is not pluggable and can't be rewritten in the child theme #990

Open Arskrigitsioniets opened 10 months ago

Arskrigitsioniets commented 10 months ago

The function hu_is_authorized_tmpl located in (functions/init-front.php:47) isn't pluggable.

if( ! function_exists('hu_is_authorized_tmpl') ) {
  function hu_is_authorized_tmpl( $tmpl ) {
    $ct_map = apply_filters(
        'hu_content_map',
        array( 'tmpl/index-tmpl', 'tmpl/archive-tmpl', 'tmpl/page-tmpl', 'tmpl/single-tmpl', 'tmpl/search-tmpl', 'tmpl/404-tmpl' )
    );
    //Are we good after filtering ?
    if ( ! is_array( $ct_map ) || ! is_string( $tmpl ) )
      return;
    return in_array( $tmpl, $ct_map );
}
}

Because this function define the list of allowed template files that Hueman can read with hu_get_content function, it doesn't allow creating custom templates, for example, for custom type posts. To make Hueman read my custom template from /tmpl, I should add this file it to the "authorized list", but it works only if I do it in the parent theme. Child theme ignores this function. So, I should update this file after each Hueman update manually if I have custom tmpl files.

The solution is simple: make the hu_is_authorized_tmpl pluggable.

iamwebrocker commented 7 months ago

Thank you for this issue. I really felt stupid, since in the functions.php of the theme the use of a child theme is encouraged, and then one of the (to me) simplest use case -- create a custom page template to include output coming from some AFC fields -- just didn't work. I pulled several hairs, until I found this and the other closed issue #610 with the discussion.

Now, after looking at this function, I was able to add "my" template via the filter hu_content_map in the function.php of my child theme:

function hueman_child_content_map() {
    return array( 
       // the original templates:
       'tmpl/index-tmpl', 
       'tmpl/archive-tmpl', 
       'tmpl/page-tmpl', 
       'tmpl/single-tmpl', 
       'tmpl/search-tmpl', 
       'tmpl/404-tmpl',
       // my template:
       'tmpl/page-custom-tmpl' 
     );
}
add_filter('hu_content_map','hueman_child_content_map');

The solution is simple: make the hu_is_authorized_tmpl pluggable.

Since the filter works, I don't think a change of the original function is needed for that template use case. But, maybe the filter should'nt replace the original templates array, but merge with whatever one wants to add to that. Currently using the filter without also stating the original templates would remove those, which isn't such a good idea, I'm arfraid.

Cheers!