WordPress.org

Ideas

Allow loading template parts outside of theme folder

  1. beysim
    Member

    12345

    Add new filter (e.g. 'locate_template') that can allow loading template parts outside of theme folder. This could be useful for creating theme specific extensions like adding new post display templates with a plugin.

    This could be done modifying the locate_teplate() function.

    Here is the current function code:

    function locate_template($template_names, $load = false, $require_once = true ) {
        $located = '';
        foreach ( (array) $template_names as $template_name ) {
            if ( !$template_name )
                continue;
            if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
                $located = STYLESHEETPATH . '/' . $template_name;
                break;
            } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
                $located = TEMPLATEPATH . '/' . $template_name;
                break;
            } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
                $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
                break;
            }
        }
    
        if ( $load && '' != $located )
            load_template( $located, $require_once );
    
        return $located;
    }

    And here is how it can be modified to allow the suggested feature:

    function locate_template($template_names, $load = false, $require_once = true ) {
        $located = '';
        $paths = array(
            STYLESHEETPATH,
            TEMPLATEPATH,
            ABSPATH . WPINC . '/theme-compat'
        );
    
        $paths = apply_filters( 'locate_template', $paths );
    
        foreach ( (array) $template_names as $template_name ) {
            if ( !$template_name )
                continue;
            foreach ( (array) $paths as $path ) {
                if ( file_exists($path . '/' . $template_name) ) {
                    $located = $path . '/' . $template_name;
                    break 2; // breaks out of both foreach loops
                }
            }
        }
    
        if ( $load && '' != $located )
            load_template( $located, $require_once );
    
        return $located;
    }
    Posted: 1 week ago #
  2. williampatton
    Inactive

    Have a look at the template_include filter which I believe would make this possible without need to modify any core functions :)

    Posted: 1 week ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    1 Vote
  • Status

    This idea is under consideration