lesterchan / wp-polls

Adds an AJAX poll system to your WordPress blog. You can also easily add a poll into your WordPress's blog post/page.
https://wordpress.org/plugins/wp-polls/
91 stars 78 forks source link

Filters for template variables to add custom variables if needed. #142

Closed Liblastic closed 4 years ago

Liblastic commented 4 years ago

Noticed that translations can't be done with this plugin without modifying the plugin.

I added some filters for the plugin like so.

After this you can register translations in the php side. Here is an example for that. Filter in theme

    /**
     * Localize wp_polls_template_resultfooter_variables.
     *
     * @param array $variables An array of template variables.
     * @return array $variables Modified template variables.
     */
    function wp_polls_template_resultfooter_variables( $variables ) {

        // Add strings.
        $variables['%STR_TOTAL_VOTERS%'] = __( 'Total voters', 'theme-textdomain' );

        return $variables;
    }

// Trigger the filter
\add_filter( 'wp_polls_template_resultfooter_variables', 'wp_polls_template_resultfooter_variables' , 10, 1 );

In the admin side just call the custom variable like so and it works and the variable has been translated.

%STR_TOTAL_VOTERS%'

Maybe it would be better that all the parts would be php-templates but that would need a lot of refactoring. This is maybe the easiest workaround for the translations for now. These filters could be used of course for anything else but are handy also for the translations.

I hope this helps you too. 👍