• Resolved webdevcat

    (@webdevcat)


    Love this plugin!

    I’m trying to customize the autocomplete dropdown by modifying the autocomplete.php template as specified in the Customization section of the Customize the Autocomplete dropdown documentation.

    The problem is that I am not using the WordPress theming system. I am using the Oxygen builder, which disables it completely. Thus I do not have an active theme and cannot add the templates folder to an active theme.

    I use a custom plugin (WP_PLUGIN_DIR.'/custom-functions/') for code that I’d typically put in a theme’s functions.php. I would like to load the templates from the plugin instead of from a theme. The templates are within the algolia folder within my custom plugin (WP_PLUGIN_DIR.'/custom-functions/algolia/').

    I (incorrectly) attempt to load these templates using the following code:

    
    // Load custom algolia templates
    function algolia_page_templates($template)
    {
    	if ('instantsearch.php' == basename($template)) {
    		$template = WP_PLUGIN_DIR . '/custom-functions/algolia/instantsearch.php';
    	} else if ('autocomplete.php' == basename($template)) {
    		$template = WP_PLUGIN_DIR . '/custom-functions/algolia/autocomplete.php';
    	}
    	return $template;
    }
    add_filter('page_template', 'algolia_page_templates');
    

    But this does not result in the loading of my customized templates; there is no noticeable change.

    I used these template names because that’s what they are named in the class-algolia-template-loader.php file within this WP Search with Alolia plugin.

    Please let me know what I can do to load my custom templates from a plugin instead of through a theme.

    Thank you ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    @webdevcat @tw2113

    I can confirm that a custom template cannot be loaded by a plugin, but only by a theme (I currently use version 2.1.0).

    The plugin uses the WordPress “locate_template()” function to load the template (https://github.com/WebDevStudios/wp-search-with-algolia/blob/57716727e6fb5047c7b07be28dfe927bad8341c4/includes/utilities/class-algolia-template-utils.php#L198), then searches inside the folder of the active theme to load a custom template file.

    It might be useful if the plugin can support custom templates from third party plugins.

    I hope this can help.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    @lanzoninicola we also have this filter right above it https://github.com/WebDevStudios/wp-search-with-algolia/blob/57716727e6fb5047c7b07be28dfe927bad8341c4/includes/utilities/class-algolia-template-utils.php#L182-L196 which should allow for adding your own custom locations that once found in one of those spots, gets used. So that part is already available.

    Thanks @tw2113 for your reply.

    I’m currently using that filter.

    I put the template file inside my plugin, I tried to set the full path of template file

    
    add_filter( 'algolia_template_locations', array( $this, 'load_custom_template' ), 10, 2 );
    
    public function load_custom_template( array $locations, $file )
    	{
    	if ( $file === 'instantsearch.php' ) {
    		$locations[] = WEBIGO_PLUGIN_PATH . '/modules/algolia-search-results/search/instantsearch.php';
    		}
    
    return $locations;
    }

    The $locations returned by the apply_filters('algolia_template_locations' ... ) ROW 192 returns correctly the path I set as “custom path”.

    But after that, the variable is passed to the locate_template() function ROW 198

    $template = locate_template( array_unique( $locations ) );

    and it returns an empty string.

    At the end of the function there is a condition,

    return (string) $template ? $template : $filtered_default;

    in my case the var $template is empty so the function returns the default path and not mine.

    I’m probably wrong but I wouldn’t know where I’m making the mistake.
    Thanks

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    @lanzoninicola I’m curious if you maybe want to use array_unshift to put your path at the start of the array so that it’s looked for there first.

    https://www.php.net/manual/en/function.array-unshift.php

    Worth a quick test at least.

    I changed my code using the array_unshift():

    if ( $file === 'instantsearch.php' ) {
      // $locations[] = WEBIGO_PLUGIN_PATH . 'modules/algolia-search-results/search/instantsearch.php';
     array_unshift( $locations, WEBIGO_PLUGIN_PATH . 'modules/algolia-search-results/search/instantsearch.php' );
    		}

    but nothing…

    The $template variable is still empty.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Doing some more digging in, and I am seeing the same behavior, and upon closer look, the locate_template function from WP core is only looking in the theme folders, so us providing the entire server path and whatnot is not doing us any good.

    Checking on some other details around this topic, stay tuned.

    Thanks @tw2113.

    Maybe you can check first if the file passed by the user filter exists, if true that file is loaded, otherwise the locate_template() function is called.

    I’ll stay tuned

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    So, the get_filtered_default_template() from the algolia plugin also has a filter available to it, which looks to likely be your best bet at the moment.

    return (string) apply_filters(
    	'algolia_default_template',
    	self::get_default_template( $file ),
    	$file
    );
    

    All the same parameters by the looks of it, but you’d want to return the string path to your custom file location.

    the get_default_template() method here returns this:

    ALGOLIA_PATH . self::get_plugin_templates_dirname() . $file;
    

    which points to the plugin folder.

    Hope this helps push this to working order.

    I tried.

    Now the file is loaded by my plugin, but there is a problem with rendering: I tried to print a simple header and it is rendered twice.

    I tried with my theme (I’m using Bricks as front-end theme) and Astra theme, I saved an image in the following path (if you want to take a look):

    https://drive.google.com/drive/folders/1Zi9Xh293Yu5qkVzy2YDRLPcuRjEWxGGi?usp=sharing

    Thanks.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Just on the hopeful whim of success. Perhaps in your instantsearch.php template, remove the get_header() and get_footer() calls. If anything, see what happens with that.

    @tw2113

    I followed your suggestion, but it doesn’t work correctly.

    By removing the get_header() and get_footer(), the page is rendered without the head tag filled in correctly (it’s empty) and in the same way as the footer.

    Yes, it fixed the specific problem mentioned, but it breaks all the others :-).

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hmm. Not sure about this detail at the moment then, and the rest is more around what to do with the 3rd party loaded template more than getting it loaded in the first place.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Customing templates when theming is disabled’ is closed to new replies.