Skip to:
Content
Pages
Categories
Search
Top
Bottom

Code Snippets: Storing plugin theme files in the plugin dir (1.2)

  • @mrmaz

    Participant

    This was quite an adventure figuring out how to make this work, so I wanted to share my method. Thanks to Andy for making a couple of bug fixes in TRUNK so this will work properly. You must be using revision 2525 or higher!

    Also thanks to DJPaul for pointing me in the right direction in the beginning.

    First you need a custom locate function that checks the WP stylesheet path first, and then checks your custom theme dir under your plugin dir. If you need to fall back on bp-default templates for some reason, then you will need to check the WP template path as well.

    You can also use this in your templates to load sub-templates.

    /**
    * Check if template exists in style path, then check custom plugin location
    *
    * @param array $template_names
    * @param boolean $load Auto load template if set to true
    * @return string
    */
    function bp_links_locate_template( $template_names, $load = false ) {

    if ( !is_array( $template_names ) )
    return '';

    $located = '';
    foreach($template_names as $template_name) {

    // split template name at the slashes
    $paths = split( '/', $template_name );

    // only filter templates names that match our unique starting path
    if ( !empty( $paths[0] ) && 'bp-links-default' == $paths[0] ) {

    $style_path = STYLESHEETPATH . '/' . $template_name;
    $plugin_path = BP_LINKS_PLUGIN_DIR . "/themes/{$template_name}";

    if ( file_exists( $style_path )) {
    $located = $style_path;
    break;
    } else if ( file_exists( $plugin_path ) ) {
    $located = $plugin_path;
    break;
    }
    }
    }

    if ($load && '' != $located)
    load_template($located);

    return $located;
    }

    Now you need to filter the template locating results of bp_core_catch_uri.

    /**
    * Filter located BP template
    *
    * @see bp_core_load_template()
    * @param string $located_template
    * @param array $template_names
    * @return string
    */
    function bp_links_filter_template( $located_template, $template_names ) {

    // template already located, skip
    if ( !empty( $located_template ) )
    return $located_template;

    // only filter for our component
    if ( $bp->current_component == $bp->links->slug ) {
    return bp_links_locate_template( $template_names );
    }

    return '';
    }
    add_filter( 'bp_located_template', 'bp_links_filter_template', 10, 2 );

    I also created a wrapper around bp_core_load_template in case I need to filter something later.

    /**
    * Use this only inside of screen functions, etc
    *
    * @param string $template
    */
    function bp_links_load_template( $template ) {
    bp_core_load_template( $template );
    }

    There you have it. Let me know if you find any bugs or make any improvements!

Viewing 12 replies - 1 through 12 (of 12 total)
  • @sbrajesh

    Participant

    Thanks MrMaz for sharing, I was looking for the same.

    Going to implement it today and test more :)

    @grosbouff

    Participant

    Hi, nice code !

    Could be very usefull.

    Anyway, I try to use it but it doesn’t load my -loop.php template file.

    I’m on the latest trunk and here’s what I did : http://pastie.org/805189

    Thanks for help !

    [EDIT] Ok it works with

    <?php bp_classifieds_locate_template( array( ‘classifieds/classifieds-loop.php’ ), true ) ?>

    :)

    @mrmaz

    Participant

    @Grosbouff

    Make sure you use a path that is NEVER going to match anything else. It is very important that you use a unique starting path, or you might match some other plugin’s template.

    @grosbouff

    Participant

    @MrMaz :

    could you check at my Classifieds plugin and see how to do this in it ?

    I did try but there’s always templates which do not load…

    Thanks a lot !

    https://wordpress.org/extend/plugins/buddypress-classifieds

    Your functions

    -bp_classifieds_locate_template

    -bp_classifieds_filter_template

    -bp_classifieds_load_template

    are at the bottom of the -templates.php page

    @grosbouff

    Participant

    Also;

    I added this to make it work with latest trunk

    //DUPLICATED FROM CORE TO REPLACE locate_template BY bp_classifieds_locate_template

    function bp_classifieds_object_template_loader() {

    $object = esc_attr( $_POST[‘object’] );

    bp_classifieds_locate_template( array( “$object/$object-loop.php” ), true );

    }

    add_action( ‘wp_ajax_classifieds_filter’, ‘bp_classifieds_object_template_loader’ );

    @grosbouff

    Participant

    I also added this

    function bp_classifieds_enqueue_url($file){

    // split template name at the slashes

    $stylesheet_path = get_stylesheet_directory_uri();

    $suffix = explode($stylesheet_path,$file);

    $suffix_str=$suffix[1];

    $file_path_to_check = BP_CLASSIFIEDS_PLUGIN_DIR . ‘/theme’.$suffix_str;

    $file_url_to_return = BP_CLASSIFIEDS_PLUGIN_URL . ‘/theme’.$suffix_str;

    if ( file_exists($file)) {

    return $file;

    }elseif ( file_exists($file_path_to_check)) {

    return $file_url_to_return;

    }

    }

    add_filter( ‘bp_classifieds_enqueue_url’, ‘bp_classifieds_enqueue_url’ );

    to filter the enqueued styles/scripts when they use get_stylesheet_directory_uri() :

    wp_enqueue_style( ‘bp-classifieds-screen’, apply_filters(‘bp_classifieds_enqueue_url’,get_stylesheet_directory_uri() . ‘/classifieds/style.css’));

    @mrmaz

    Participant

    @Grosbouff

    I’m curious why you are filtering the enqueued files? Do you want people to be able to totally override your plugin’s stylesheet?

    @techkapil

    Participant

    It works…nice one MrMaz…!!! n also looking forward to oEmbed API of BP Links… Gr8 piece of work….!!!

    @techkapil

    Participant

    Template is not loading in case of WordPress single user…but the same code works on WPMU…

    if ( $bp->current_component == BP_EXAMPLE_SLUG && !$bp->is_single_item &&

    !$bp->displayed_user->id){

    $bp->is_directory = true;

    bp_example_load_template( ‘example/index’ );

    }

    and I have implemented bp_example_load_template function as given by MrMaz.

    Anybody having same problem???

    I am using WP 2.9.1 (Single user) with BP1.2 RC2 and WPMU 2.9.1.1 with BP 1.2 RC2

    Thanks :-)

    @djpaul

    Keymaster

    Ok, this particular problem really sucks. You use bp_core_load_template() in a plugin to load a theme file, using the filter ‘bp_located_template’.

    If you’re modelling your plugin on the default BuddyPress components, or even the skeleton component, you could use the above to load an index page for your directory, and use a “loop” file to display each item within your directory index page.

    However, the whole purposes of the ‘bp_located_template’ filter is that it allows theme authors to override your plugin’s theme file by adding it to their WordPress theme directory. The problem is that you cannot use bp_located_template() to include your loop file (henceforth referred to as a “plugin sub-template”).

    Why? Because bp_core_load_template() just sets up the data for the template to load; nothing happens until the WordPress ‘template_redirect’ action is called, which only happens once on page load (.e. when it loads your index page).

    Quick fix? After calling bp_core_load_template( ‘plugin/sub-template.php’ ), just call bp_core_do_catch_uri().

    Proper fix? Working on it.

    bp_core_load_template is not working when trying to load plugin’s sub-templates from within that plugin’s template file(s)

    @djpaul

    Keymaster

    @mrmaz

    Participant

    Here is my two cents that I added to the ticket:

    Its my understanding that bp_core_load_template() was not intended for sub-templates. I believe that all of the core templates use locate_template() to load sub-templates.

    I wrote my own custom locate_template method to check the plugin path instead of the template path, which does not rely on any of the catch uri stuff.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Code Snippets: Storing plugin theme files in the plugin dir (1.2)’ is closed to new replies.
Skip to toolbar