Title: Adding a variable to some PHP code with gettext
Last modified: October 14, 2024

---

# Adding a variable to some PHP code with gettext

 *  [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/)
 * The year in the following code, that is to say “2024”
 *     ```wp-block-code
       $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
 *     ```wp-block-code
       $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](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fadding-a-variable-to-some-php-code-with-gettext%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  [S P Pramodh](https://wordpress.org/support/users/sppramodh/)
 * (@sppramodh)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18072575)
 * Hello [@sacconi](https://wordpress.org/support/users/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:
 *     ```wp-block-code
       $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](https://developer.wordpress.org/reference/functions/wp_kses/)
   out 
   Let me know if this solution worked for your!
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18072616)
 * I copied the code but it doesnt work [https://test.sacconicase.com/forte-dei-marmi-appartamento-in-villa-per-6-persone-con-giardino/#titolo_listino_prezzi](https://test.sacconicase.com/forte-dei-marmi-appartamento-in-villa-per-6-persone-con-giardino/#titolo_listino_prezzi)
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18072639)
 * To tell the truth I just used the code but I dont know how to use [https://developer.wordpress.org/reference/functions/wp_kses/](https://developer.wordpress.org/reference/functions/wp_kses/)
 *  [S P Pramodh](https://wordpress.org/support/users/sppramodh/)
 * (@sppramodh)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18074721)
 * Hello [@sacconi](https://wordpress.org/support/users/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:
 *     ```wp-block-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](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18075929)
 * 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](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18079139)
 * Do you have any suggestion? thank you
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18082863)
 * By the way, I have a custom meta field called `current_year`
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18083047)
 * Ok, it works but with
 *     ```wp-block-code
       esc_html( sprintf( __('Rates %s in &euro;/ week:','mysite-plugin'), $current_year ) );
       ```
   
 * And I forgot to declare the variable too 🙁
 *  [Dilip Modhavadiya](https://wordpress.org/support/users/dilip2615/)
 * (@dilip2615)
 * [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18083728)
 * Hello [@sacconi](https://wordpress.org/support/users/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)

The topic ‘Adding a variable to some PHP code with gettext’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [Themes](https://wordpress.org/support/topic-tag/themes/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 9 replies
 * 3 participants
 * Last reply from: [Dilip Modhavadiya](https://wordpress.org/support/users/dilip2615/)
 * Last activity: [1 year, 5 months ago](https://wordpress.org/support/topic/adding-a-variable-to-some-php-code-with-gettext/#post-18083728)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
