• 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.

    (@clorith)

    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() 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 will be quite handy for you.

Viewing 1 replies (of 1 total)

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