janizde / WP-Opening-Hours

Opening Hours Plugin for WordPress
GNU General Public License v2.0
55 stars 16 forks source link

Is the op_shortcode_template filter broken? No success on trying to override template #135

Open monecchi opened 4 years ago

monecchi commented 4 years ago

@dudgemaster I'm wondering if the ´op_shortcode_template´ filter is broken?

I'm placing the function bellow on my theme's functions.php, I'm not sure though if the path format is valid. I've copied the is-open.php shortcode template from /views/shortcode/is-open.php to my theme's include folder under /inc/shortcodes/wp-opening-hours/

/**
 * WP-Opening-Hours
 * Specify own shortcode template for IsOpen
 */

// use OpeningHours\Module\Shortcode\IsOpen;

add_filter('op_shortcode_template', function ($template, $shortcode) {

    $shortcodeTemplatePath = get_theme_file_path() . '/inc/shortcodes/wp-opening-hours/';

    // echo $shortcodeTemplatePath; // prints /var/www/htdocs/wp-content/themes/mytheme/inc/shortcodes/wp-opening-hours/

    // If the Shortcode is the IsOpen shortcode return a custom template
    if ($shortcode instanceof \OpeningHours\Module\Shortcode\IsOpen)
            return $shortcodeTemplatePath .'/is-open.php';
            //return '/path/to/template.php';

    // If it is any other type of Shortcode keep $template unchanged
    return $template;

}, 10, 2);

I'm really interested on changing the shortcodes markup, any help regarding this issue is appreciated. Thanks for the great plugin!

janizde commented 4 years ago

Hi @monecchi , I looked at the source and it turns out the $templatePath should be relative to the views directory of the plugin: https://github.com/janizde/WP-Opening-Hours/blob/c604366d3f13541747c485d57878e9f489c65ef0/classes/OpeningHours/Module/Shortcode/AbstractShortcode.php#L122-L151

This is certainly documented wrong and is actually not such a sensible API I must admit. Can you please try to return a path like ../../../mytheme/inc/shortcodes/wp-opening-hours/is-open.php ?

I can't really change the API of the filter in a patch, but maybe I will add an additional filter op_shortcode_template_absolute or so.

c0smic commented 3 years ago

Any working solution? I´m not a php programmer - just being able to get by. If i paste echo $templatePath; after line 129 it shows the correct path to child-theme. If i paste it after line 147 path is not correct - it shows the pluginpath and chlidthemepath in one line. In my understanding it means to override the op_plugin_path(). But how to do that? Or is it the false conclusion? thank you in advance