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

Customize the output of POLL_START_DATE and POLL_END_DATE #157

Closed errotu closed 2 months ago

errotu commented 4 months ago

Hi! First of all: Thank you for this great plugin!

Is there any possibility to customize the output of %POLL_START_DATE% and %POLL_END_DATE%? Currently, it looks rather technical (quote from a German wordpress):

  1. Dezember 2023 00000012 14:51 170169791402Mo, 04 Dez 2023 14:51:54 +0100

I'd like to trim it down to just Day. Month Year (e.g. "4. Dezember 2023") but did not find any option to do so.

Thanks a lot for your help!

oleole39 commented 4 months ago

Hi,

Check this code sample to understand how those dates are generated by the plugin by default:

$poll_start_date = mysql2date(sprintf(__('%s @ %s', 'wp-polls'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $poll_question->pollq_timestamp));

So the output you get is likely to be related to your site's default configuration regarding date and time format.

If you need to keep your default site's settings as they are and would only like the plugin's date and time format to be modified, and if you don't mind writing PHP code, you can achieve that in a fairly easy way by adding 2 custom filtering callback functions to your theme's functions.php (together with their add_filter() call):

The first one would modify targeted values and be hooked to those filters for vote and result forms:

wp_polls_template_voteheader_variables
wp_polls_template_votefooter_variables
wp_polls_template_resultheader_variables
wp_polls_template_resultfooter_variables

The other one would have to search and replace the targeted value in the archive page markup before it gets printed by hooking to that filter:

wp_polls_archive

You will find the structure of the content you need to filter in the file wp-polls.php

Best, oleole39

errotu commented 2 months ago

Hi oleole39, thanks a lot for your detailed explanation, and I'm really sorry for the late reply.

Unfortunately, the plugin does not use my site's default configuration regarding date and time format (I think that would be the desired behavior, see below for my settings). So I guess it's a bug that the plugin ignores this setting?

grafik

oleole39 commented 2 months ago

Hello,

7. Dezember 2023 00000012 14:51 170169791402Mo, 04 Dez 2023 14:51:54 +0100

Can you share the code you use to generate that string (i.e. where you include %POLL_START_DATE% and %POLL_END_DATE%) ?

errotu commented 2 months ago

It's the following:

Umfrage vom %POLL_START_DATE%

(from the "Individual Poll Footer", "Umfrage von" translates to "Poll from")

oleole39 commented 2 months ago

So what is the full string displayed on the user side ? Umfrage vom 7. Dezember 2023 00000012 14:51 170169791402Mo, 04 Dez 2023 14:51:54 +0100 ? Also would you have made a custom translation of the plugin ? Can you please report what is written below the line msgid "%s @ %s" in /wp-content/languages/plugins/wp-polls-de_DE.po (it should be line 1094 or nearby) ?

oleole39 commented 2 months ago

Also would you have made a custom translation of the plugin

I reproduced the issue by switching Wordpress to German locale and reinstalling WP-polls so that it comes with its own German locale. The issue lies in the plugin's German translation string for the code sample I indicated in my first message above (%s @ %s). This has been translated in German by %s um %s Uhr. However this is wrong because each of the letters inumUhr gets interpreted as date format characters leading to the strange output string you noticed. I would recommend to come back to the orignal string instead, unless you have something more adequate in mind. You will need to update those two files:

You may want to do the modification there so that you wouldn't need to do it manually at each plugin's upgrade. Bear in mind that this will impact all users of the plugin's German locale and it should not cover a very specific case. Then you can generate .po/.mo files from that page (see the feature "Export" at the bottom of the page) and rename them accordingly so that they replace your existing WP-Polls translation files at /wp-content/languages/plugins/.

errotu commented 2 months ago

Thanks a lot for the detailed, excellent explanation and pointing me towards the solution!

I fixed the bug by escaping the characters:

%s \u\m %s \U\h\r

In my installation, I also had to update/export the wp-polls-de_DE.l10n.php file.