Localize %currentdate% in Meta Title

#583501
  • Resolved Jacquas
    Rank Math free

    Hi, I want to localize the %currentdate% as per my server region and language. For example, if my website is in Turkish and I want to show the current date in my Meta Title and use %currentdate% then it shows the date like 3 June 2022 but it must show the 03 Haziran 2022 (the translated date as per language selected in setting and in localized format).

    Here is a special article of WordPress officially on the localization of date format (https://wordpress.org/documentation/article/customize-date-and-time-format/). They encourage to use of function
    $date = wp_date( 'F j, Y' ); for localization of the date instead of,
    $date = date( 'F j, Y' );

    I think Rank Math is not using wp_date for the localization of the date format. So, what is the solution of the issue and how it can be solved?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Jacquas
    Rank Math free

    Is anyone even here to provide support to the users? It has been more than 30 hours without any response from your side.

    Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. We deeply apologize for the delay in response.

    You can use the custom variable %current_Turkish_date% to show the Turkish date on your meta title and description. To do that, please add the following code to your site.

    add_action( 'rank_math/vars/register_extra_replacements', function(){
        rank_math_register_var_replacement(
            'current_Turkish_date',
            [
            'name'        => esc_html__( 'Current Turkish Date.', 'rank-math' ),
            'description' => esc_html__( current_Turkish_date_callback(), 'rank-math' ),
            'variable'    => 'current_Turkish_date',
            'example'     => current_Turkish_date_callback(),
            ],
            'current_Turkish_date_callback'
        );
    });
    
    function current_Turkish_date_callback(){
        setlocale(LC_ALL, 'tr_TR.UTF-8');
        $dateMonthYear = strftime("%d %B");
        $year = date('Y');
        return $dateMonthYear." ".$year;
    }

    Here’s how you can add filters/hooks to your WordPress site:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Note: If you just want to switch the language, keep the variable name as it is and change the tr_TR from the code to the desired one. The changes will take effect everywhere.

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Jacquas
    Rank Math free

    If I want to switch the language like if I want to localize %currentdate% to other language other than Turkish (for example Finnish) then should I just change tr_TR in ‘tr_TR.UTF-8’ to that specific language or should change “current_Turkish_date_callback” with the name of that specific language as well?

    Hello,

    It’s completely up to you.

    If you want to create a different variable for different languages, then create a new same code by changing the variable name and the language’s local code.

    If you want to change language in the the existing one, then just change the language’s local code.

    For say, you used the %current_Turkish_date% in all of your posts and now you want to change the language without editing all of your posts. You can simply edit the code and replace the tr_TR in tr_TR.UTF-8 to replace the language everywhere.

    Example code:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
        rank_math_register_var_replacement(
            'current_Turkish_date',
            [
            'name'        => esc_html__( 'Current Turkish Date.', 'rank-math' ),
            'description' => esc_html__( current_Turkish_date_callback(), 'rank-math' ),
            'variable'    => 'current_Turkish_date',
            'example'     => current_Turkish_date_callback(),
            ],
            'current_Turkish_date_callback'
        );
    });
    
    function current_Turkish_date_callback(){
        setlocale(LC_ALL, 'fi_FI.UTF-8');
        $dateMonthYear = strftime("%d %B");
        $year = date('Y');
        return $dateMonthYear." ".$year;
    }

    The variable will be the same: %current_Turkish_date%, but the output will change to the Finnish language.

    You can also change the variable name when creating a new one. Simply replace the word Turkish in the code with any other word you want.

    Here’s the code for the Finnish language:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
        rank_math_register_var_replacement(
            'current_Finnish_date', 
            [
            'name'        => esc_html__( 'Current Finnish Date.', 'rank-math' ),
            'description' => esc_html__( current_Finnish_date_callback(), 'rank-math' ),
            'variable'    => 'current_Finnish_date',
            'example'     => current_Finnish_date_callback(),
            ],
            'current_Finnish_date_callback'
        );
    });
    
    function current_Finnish_date_callback(){
        setlocale(LC_ALL, 'fi_FI.UTF-8');
        $dateMonthYear = strftime("%d %B");
        $year = date('Y');
        return $dateMonthYear." ".$year;
    }

    The variable will be: %current_Finnish_date%

    Here you can find all the language codes:
    https://www.webenfo.com/list-of-php-language-locale-code.html

    Hope that helps, and please do not hesitate to let us know if you need our assistance with anything else.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)

The ticket ‘Localize %currentdate% in Meta Title’ is closed to new replies.