Title: Custom Functions with Variables
Last modified: June 25, 2020

---

# Custom Functions with Variables

 *  [rtvand](https://wordpress.org/support/users/rtvand/)
 * (@rtvand)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/custom-functions-with-variables/)
 * WordPress 5.4.2
 * In PHP, I have created a custom function what requires variables. (it lists the
   files from a directory, very simple, but requires the directory to scan, and 
   method for ordering and if it should display icons.) I use it on several pages,
   multiple times on each page.
 * This function is in an include file, which is include within a Custom HTML section.
 * In another Custom HTML section I would put in the code like:
    [insert_php]getFilesList(‘
   testing/Documentation/’, ‘imageTitle’);[/insert_php]
 * I update the post, view the page and everything looks great. Until I go back 
   to edit the page. Then I get an error:
    Fatal error: Uncaught Error: Call to 
   undefined function getFilesList() in /webroot/prod/public/wordpress/wp-content/
   plugins/insert-php/insert_php.php(48) : eval()’d code:1 Stack trace: #0 /webroot/
   prod/public/wordpress/wp-content/plugins/insert-php/insert_php.php(48): eval()#
   1 /webroot/prod/public/wordpress/wp-includes/class-wp-hook.php(287): will_bontrager_insert_php(‘\
   napply_filters(‘\nprepare_item_for_response(Object(WP_Post), Object(WP_REST_Request))#
   5 /webroot/prod/public/wordpress/wp-includes/rest-api/class-wp-rest-server.php(
   1015): WP_REST_Posts_Controller->get_item(Object(WP_REST_Request)) #6 /webroot/
   prod/pu in /webroot/prod/public/wordpress/wp-content/plugins/insert-php/insert_php.
   php(48) : eval()’d code on line 1
    -  This topic was modified 5 years, 10 months ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).
      Reason: Moved to Fixing WordPress, this is not an Everything else WordPress
      topic

Viewing 1 replies (of 1 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/custom-functions-with-variables/#post-13035515)
 * What you are describing is what shortcodes were made for; also custom blocks.
   
   Put your code in a plugin, and add one little function that is a shortcode handler,
   which gets the variables from the shortcode and then generates the output. (Remember
   that the output is returned, not echoed.) [https://codex.wordpress.org/Shortcode_API](https://codex.wordpress.org/Shortcode_API)

Viewing 1 replies (of 1 total)

The topic ‘Custom Functions with Variables’ is closed to new replies.

## Tags

 * [custom function](https://wordpress.org/support/topic-tag/custom-function/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Joy](https://wordpress.org/support/users/joyously/)
 * Last activity: [5 years, 10 months ago](https://wordpress.org/support/topic/custom-functions-with-variables/#post-13035515)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
