• Resolved jannnik

    (@jannnik)


    Hi,

    I wrote a plugin which extends the functionality of Job Manager and made some custom templates, which are actually in my theme folder to override the original templates.
    Now, I would like to add the templates to the plugin folder to make my plugin undependent to the theme. I read the section in your wiki how to add a filter for the template location, but I didn’t managed it to use a custom template location in the plugins folder.

    Could you provide an example how to add a filter to use the plugin folder instead of the theme folder for template overrides?

    https://wordpress.org/plugins/wp-job-manager/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mike Jolley

    (@mikejolley)

    If you’re hoping to override CORE template files via your plugin, the plugin uses a filter which gives you that opportunity.

    return apply_filters( 'job_manager_locate_template', $template, $template_name, $template_path );

    Filter job_manager_locate_template, check what $template_name is being filtered and return the updated path.

    Thread Starter jannnik

    (@jannnik)

    Yes, I want to override core template files from wp job manager. I read this post: https://wordpress.org/support/topic/template-overrides-via-plugin
    , but I really need an example how to write the filter.
    The only thing I need is to have a template folder in my plugin directory which works like the folder in the theme directory.

    Plugin Author Mike Jolley

    (@mikejolley)

    The filter I posted is what you need. If you’ve written a plugin, surely you’re familiar with filter syntax? Basic example:

    add_filter( 'job_manager_locate_template', 'custom_job_manager_locate_template', 10, 3 );
    
    function custom_job_manager_locate_template( $template, $template_name, $template_path ) {
       return 'somenewtemplatepath';
    }
    Thread Starter jannnik

    (@jannnik)

    Finally, I got it to work with this filter (it tries to load each template from my plugin’s folder, but if there is no custom template, it will use the original one.)

    add_filter( 'job_manager_locate_template', 'custom_job_manager_locate_template', 10, 3 );
    function custom_job_manager_locate_template( $template, $template_name, $template_path ) {
    	$plugin_path = dirname(__FILE__) . '/templates/' . $template_name;
    	if (file_exists($plugin_path))
    		return $plugin_path;
    	else
    		return $template;
    }
    Thread Starter jannnik

    (@jannnik)

    But there is a problem again. It seems like this filter only filters the “job-filters”-templates, but not the “content”-templates. (I use the [jobs] shortcode). Could you test my filter and find the reason why it does not work as expected?

    Plugin Author Mike Jolley

    (@mikejolley)

    Those ones are not filtered. Log an issue on github.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Plugin template path.’ is closed to new replies.