• sacconi

    (@sacconi)


    The year in the following code, that is to say “2024”

    $titolo_display_price = '<div class="titolo_listino_prezzi" ID="titolo_listino_prezzi">' . '<h3 class="titolo_affitti">' . esc_html__('Rates 2024 in &euro;/ week:','mysite-plugin') . '</h3>' .'</div>';

    should be tranformed in a variable such as

    $current_year = get_the_author_meta( 'current_year', $user->ID )

    How can I transform the first code adding the variable?

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • S P Pramodh

    (@sppramodh)

    Hello @sacconi,

    To incorporate the $current_year variable into your existing PHP code with gettext, you’ll need to update the string dynamically while ensuring that the translation function still works properly.

    Here’s simple solution to achieve this:

    $titolo_display_price = sprintf(
    '<div class="titolo_listino_prezzi" ID="titolo_listino_prezzi">' .
    '<h3 class="titolo_affitti">' . esc_html__( 'Rates %s in &euro;/ week:', 'mysite-plugin' ) . '</h3>' .
    '</div>',
    esc_html( $current_year )
    );

    WordPress provides wp_kses() for safely including variables in translatable strings, and it can also handle HTML within strings more effectively, without requiring too much concatenation. Try this out

    Let me know if this solution worked for your!

    Thread Starter sacconi

    (@sacconi)

    Thread Starter sacconi

    (@sacconi)

    To tell the truth I just used the code but I dont know how to use https://developer.www.ads-software.com/reference/functions/wp_kses/

    Hello @sacconi,

    No worries! I’ll guide you through to identify the issue.

    It seems that the issue might stem from how you’re trying to retrieve the current_year value using get_the_author_meta(). By default, WordPress doesn’t store a meta field called current_year for users. Unless you have explicitly added this custom meta field (e.g., through a plugin or custom code), the value will likely be empty, which is why the output shows:

    Rates in €/ week:

    If you’re simply trying to display the current year dynamically, you can use PHP’s built-in function date('Y') instead of relying on get_the_author_meta()

    If this works then it confirms the issue is with the meta field. Here’s the updated code:

    $current_year = date('Y'); // This will get the current year, e.g., 2024

    $titolo_display_price = sprintf(
    '<div class="titolo_listino_prezzi" ID="titolo_listino_prezzi">' .
    '<h3 class="titolo_affitti">' . esc_html__( 'Rates %s in &euro;/ week:', 'mysite-plugin' ) . '</h3>' .
    '</div>',
    esc_html( $current_year )
    );

    Let me know if you need further assistance.

    Thread Starter sacconi

    (@sacconi)

    I tryed to use $year instead of $current_year but it’s the same problem. Pls note that the get_the_author_meta() is in a file included in functions, instead the $titolo_display_price is in a custom plugin. No, I dont need to display the current year.

    Thread Starter sacconi

    (@sacconi)

    Do you have any suggestion? thank you

    Thread Starter sacconi

    (@sacconi)

    By the way, I have a custom meta field called current_year

    Thread Starter sacconi

    (@sacconi)

    Ok, it works but with

    esc_html( sprintf( __('Rates %s in &euro;/ week:','mysite-plugin'), $current_year ) );

    And I forgot to declare the variable too ??

    Hello @sacconi

    To dynamically insert the current year into your WordPress code, use PHP’s date() function. Here’s an example:

    $current_year = date(‘Y’);
    $titolo_display_price = '<div class="titolo_listino_prezzi" ID="titolo_listino_prezzi">' . '<h3 class="titolo_affitti">' . esc_html__('Rates ' . $current_year . ' in &euro;/ week:', 'mysite-plugin') . '</h3>' . '</div>';

    try to above example, may be it will help you.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.