Title: How to insert PHP code into widgets?
Last modified: October 4, 2023

---

# How to insert PHP code into widgets?

 *  Resolved [madleine](https://wordpress.org/support/users/madleine/)
 * (@madleine)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/how-to-insert-php-code-into-widgets/)
 * Hi,
   I am adding HTML and PHP code together in widgets using HTML custom blocks
   but the PHP code is not rendered to the page, an example of which is below
 *     ```wp-block-code
       <p>&copy;2015- <?php echo Date('Y'); ?> Footer Area</p>
       ```
   
 * How to add PHP code into widgets ? 
   I would prefer not to use a plugin and to
   achieve this through coding

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

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/how-to-insert-php-code-into-widgets/#post-17100981)
 * As you’ve found, you can’t add PHP code through HTML blocks.
 * To add that, you would need to create your own widget code, and add it to a child
   theme or a custom plugin. If you don’t have the ability to do that you really
   should look at a plugin that can do this as it’s really going to do what you 
   want with pretty much the same sort of coding, just done by someone else.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/how-to-insert-php-code-into-widgets/#post-17100991)
 * The Custom HTML block is for HTML, not PHP 🙂 As you’ve discovered. You could
   develop your own [classic widget](https://codex.wordpress.org/Widgets_API), or
   [a shortcode](https://codex.wordpress.org/Shortcode_API) which you can add to
   other content, or your own [custom block](https://developer.wordpress.org/block-editor/getting-started/create-block/)
   which can be used as a widget.
 *  Thread Starter [madleine](https://wordpress.org/support/users/madleine/)
 * (@madleine)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/how-to-insert-php-code-into-widgets/#post-17104876)
 * In case someone else wanted to achieve the same result as me, this is how I achieved
   it 🙂
   [@assist7833](https://wordpress.org/support/users/assist7833/) gave an 
   answer in this thread (that is deleted now) but it worked perfectly
 * in functions.php of my child theme I added the following code with few tweaks
   replacing “Footer Area” with the actual text I want rendered there:
 *     ```wp-block-code
       function custom_footer_year() {
           ob_start();
           echo '&copy;' . date('Y') . ' Footer Area';
           return ob_get_clean();
       }
       add_shortcode('footer_year', 'custom_footer_year');
       ```
   
 * and then inside the footer widget I created, instead of the PHP code to display
   the current year for the copyright validity, I put this shortcode
 *     ```wp-block-code
       [footer_year]
       ```
   
 * Thank you also to [@catacaustic](https://wordpress.org/support/users/catacaustic/)
   for the suggestions and thanks for the links [@bcworkz](https://wordpress.org/support/users/bcworkz/)

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

The topic ‘How to insert PHP code into widgets?’ is closed to new replies.

## Tags

 * [widget](https://wordpress.org/support/topic-tag/widget/)
 * [wordpress widgets](https://wordpress.org/support/topic-tag/wordpress-widgets/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 4 participants
 * Last reply from: [madleine](https://wordpress.org/support/users/madleine/)
 * Last activity: [2 years, 7 months ago](https://wordpress.org/support/topic/how-to-insert-php-code-into-widgets/#post-17104876)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
