Okay
  Public Ticket #1316527
Current date placeholder [WORKAROUND]
Closed

Comments

  •  2
    Raul started the conversation

    Hi

    I really need placeholders with current date info. 

    As a suggestion: Please add it :)

    Meanwhile I coded a workaround and I decided to share it. May be someone finds it useful. Better ideas? Better code? Please share :)

    Edit file class.wdttools.php

    After :

            // Shortcode VAR3
            if (strpos($value, '%VAR3%') !== false) {
                $value = str_replace('%VAR3%', $wdtVar3, $value);
            }

    (around line 590)

    Add:

            //Shortcode for date generation. 
            //Use: %CURRENT_DATE%date_format_string% 
            //Example: %CURRENT_DATE%Y-m-d%
            $varname='%CURRENT_DATE%';
            $default_format="Y-m-d"; //in case no format is supplied
            $startpos=0;
            while ($startpos!==false){
                    $startpos=strpos($string, $varname);
                    if ($startpos !== false) {
                            $endpos=strpos($string, '%', $startpos + strlen($varname));
                            if ($endpos!==false){
                                    $placeholder= substr($string,$startpos,$endpos-$startpos+1);
                                    $dateformatstring= substr($string,$startpos + strlen($varname), $endpos - ($startpos + strlen($varname)));
                                    $string = str_replace($placeholder, date($dateformatstring), $string);
                            }else{
                                    $string = str_replace($varname, date($default_format), $string);
                            }
                }
            }
    


    Edit file class.wpdatacolumn.php

    After:

    // Shortcode VAR3
            if (strpos($value, '%VAR3%') !== false) {
                $value = str_replace('%VAR3%', $wdtVar3, $value);
            }

    (around line 590)

    Add:

            //Shortcode for date generation. 
            //Use: %CURRENT_DATE%date_format_string% 
            //Example: %CURRENT_DATE%d/m/Y%
            
            $varname='%CURRENT_DATE%';
            $default_format="d/m/Y"; //in case no format is supplied
            $startpos=0;
            while ($startpos!==false){
                    $startpos=strpos($value, $varname);
                    if ($startpos !== false) {
                            $endpos=strpos($value, '%', $startpos + strlen($varname));
                            if ($endpos!==false){
                                    $placeholder= substr($value,$startpos,$endpos-$startpos+1);
                                    $dateformatstring= substr($value,$startpos + strlen($varname), $endpos - ($startpos + strlen($varname)));
                                    $value = str_replace($placeholder, date($dateformatstring), $value);
                            }else{
                                    $value = str_replace($varname, date($default_format), $value);
                            }
                }
            }
            //This is a workaround for date fields since we cannot write a placeholder in the datetime textbox I use these weird dates as placeholders themselves
                //01/01/1000 - First day of current month
                //15/01/1000 - Current day of current month
                //31/01/1000 - Last day of current month
                $placeholder_first_day_current_month='01/01/1000';
                $placeholder_current_day_current_month='15/01/1000';
                $placeholder_last_day_current_month='31/01/1000';
                if (strpos($value, $placeholder_first_day_current_month) !== false) {
                    $value = str_replace($placeholder_first_day_current_month, date("01/m/Y"), $value);
            }
                if (strpos($value, $placeholder_current_day_current_month) !== false) {
                    $value = str_replace($placeholder_current_day_current_month, date("d/m/Y"), $value);
            }
                if (strpos($value, $placeholder_last_day_current_month) !== false) {
                    $value = str_replace($placeholder_last_day_current_month, date("t/m/Y"), $value);
            }

    I should change also table_settings_block.inc.php so it appears there and lets me set a default value for query, but since I'll probably need to replicate changes on next wpdatatables update, I decided to keep it simple for now.

    Cheers

  •  1,846
    Miloš replied

    Hi Raul,
    Thank you for your purchase.


    Thank you for sharing your solution with us.

    I will definitely add this workaround to our TODO list so we can test it for the future updates.

    Thank you again for sharing your suggestions and ideas with us.


    Best regards.

    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables