Giter VIP home page Giter VIP logo

Comments (3)

Robbertdk avatar Robbertdk commented on July 24, 2024 1

@quentin-th Thanks for your input. I've copied the idea of your code and simplified it a bit. I've also changes the filter hook name to ajax_filter_posts_template_name so it's more consistent with the other hook names. See https://github.com/Robbertdk/wordpress-ajax-filter-posts/tree/feature/overwrite-template-file#overwriting-template-files

from wordpress-ajax-filter-posts.

Robbertdk avatar Robbertdk commented on July 24, 2024

Hi,

This is indeed not implemented, but that wouldn't be hard, because the plugin uses a custom template loader.

You'll be able to overwrite the template with an apply_filter function before returning the template in that function.

I'm quite busy the upcoming weeks. Can you write a PR for that?

from wordpress-ajax-filter-posts.

quentin-th avatar quentin-th commented on July 24, 2024

Hi,

Thanks for the informations !
I'm not sure if and how I could write a PR directly from Giuthub, I didn't clone the repo (sorry I don't use git much).

However I managed to create the filter, so here the code I wrote :

/**
   * Locate template.
   *
   * Locate the called template.
   * Search Order:
   * 1. /themes/theme/ajax-posts-filters/$template_name
   * 2. /3rd-party-plugin-dir-path/ajax-posts-filters/templates/$template_name
   * 3. /plugins/ajax-filter-posts/templates/$template_name.
   *
   * @since 0.3.0
   *
   * @param   string  $template_name          Template to load.
   * @return  string                          Path to the template file.
   */
  public function get_local_template($template_name, $template = '') {

    if (empty($template_name)) return false;

    $template = locate_template('ajax-filter-posts/' . $template_name);
    
    // Allow 3rd party plugin filter template file from their plugin.
    $filter_template = apply_filters( 'get_local_template', $template_name, $template );
    
    // if path is from a 3rd party plugin
    if ( $filter_template !== $template ) {
      if ( !file_exists( $filter_template ) ) {
        _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $filter_template ), '4.6.0' );
        return;
      }
      $template = $filter_template;
    } 
    
    // If template not in theme and or not in 3rd party plugin, get plugins template file.
    if ( !$template && !$filter_template ) {
      $template = plugin_dir_path( __FILE__ ) . 'templates/' . $template_name;
    } 

    if ( !file_exists( $template ) ) {
      _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $template ), '4.6.0' );
      return;
    }
   

    return $template;
  }

and if it can help someone, here the add_filter function :

add_filter( 'get_local_template', 'intercept_afp_template', 10, 3 );
/**
* Filter ajax filter posts templates.
*
* @param string $template_name  Template file name.
* @param string $template       Template file path.
*
* @return string The new Template file path.
*/
function intercept_afp_template( $template_name, $template ) {
    
    $template_directory = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'ajax-posts-filters/templates/';
    $path = $template_directory . $template_name;

    return file_exists( $path ) ? $path : $template;    
}

from wordpress-ajax-filter-posts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.