Title: Nested function in WP Plugin
Last modified: August 22, 2016

---

# Nested function in WP Plugin

 *  [Hercal](https://wordpress.org/support/users/hercal/)
 * (@hercal)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/nested-function-in-wp-plugin/)
 * I write a php code that read text file and it is working fine with no problem,
   have a look at this code.
 *     ```
       <?php
       function Read($filepath)
       {
       $myfile = fopen($filepath,"r") or die("Unable to open file!");
       $label=fread($myfile,filesize($filepath));
       fclose($myfile);
       echo $label;
       }
       ?>
       ```
   
 * now if i try to use Read function inside below input it works fine
 *  `<input type="text" id="txtname" name="txtname" placeholder="<?php Read("resources/
   name_ar.txt");?>" />`
 * I need to do the same thing using a wordpress plugin but i can’t . have another
   look on below code
 *     ```
       <?php
       /*
       Plugin Name: my plugin
       Description: my plugin
       Version: 4.0
       Author: me
       License: GPL
       */
       ?>
       <?php
       //PHP Function to read resources files.
       function Read($filepath)
       {
       $myfile = fopen($filepath,"r") or die("Unable to open file!");
       $label=fread($myfile,filesize($filepath));
       fclose($myfile);
       echo $label;
       }
       ?>
   
       <?php
       function  form_creation()
       {
           global $wpdb;
           ob_start();
       ?>
       <form action="<?php get_permalink();?>" method="post" id="myform">
       <table>
       <tr>
       <td>
           <h2>Asking Support</h2>
       </td>
       </tr>
       <tr>
       <td> <input type="text" id="txtname" name="txtname" placeholder="<?php Read("resources/name_ar.txt");?>" /> </td>
       </tr>
   
       </table>
       </form>
       <?php return ob_get_clean(); } ?>
       <?php add_shortcode('myshortcode',form_creation); ?>
       ```
   
 * now when i use myshortcode nothing displayed and i think that because read function
   didn’t be accessed , so how can Read function be accessed by form creation function
 * keep in mind , if form_creation() has no nested function , it will work and form
   displayed .

Viewing 1 replies (of 1 total)

 *  Moderator [Marius L. J.](https://wordpress.org/support/users/clorith/)
 * (@clorith)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/nested-function-in-wp-plugin/#post-5617647)
 * Hi,
 * First thing I would do is prefix your functions with your plugin slug (so `form_creation()`
   becomes `my_plugin_form_creation()` and `my_plugin_read()`) as this ensures you
   won’t get any function naming conflicts.
 * Your file path should also be given an absolute path (the relative path you have
   there will resolve back to the WordPress root folder due to how PHP includes 
   function), you’ll want to look into [`plugin_dir_url()`](http://codex.wordpress.org/Function_Reference/plugin_dir_url)
   function for that.
 * After doing these changes, see what happens, does it still crash you’ll have 
   to check what errors it’s outputting where the article at [http://codex.wordpress.org/Debugging_in_WordPress](http://codex.wordpress.org/Debugging_in_WordPress)
   will be quite handy for you.

Viewing 1 replies (of 1 total)

The topic ‘Nested function in WP Plugin’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Marius L. J.](https://wordpress.org/support/users/clorith/)
 * Last activity: [11 years, 3 months ago](https://wordpress.org/support/topic/nested-function-in-wp-plugin/#post-5617647)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
