0


This is my first time designing a plugin, so I would appreciate any assistance that you all can provide.

My plugin has a txt file with datetime objects in it. The goal is to read the txt file from a plugin admin page, allow users to edit it, and then save the file once again.

I have no problem reading the file, but I am unable to save it. When I click the button to save it, the browser redirects me somewhere else and doesn't end up saving my file.

EDIT: I should add - this code worked just fine outside of wordpress (as its own PHP page). I can't seem to get it to work with wordpress.

Here is my code. I would greatly appreciate any advice:

if($_POST['addition']){ 
    $file_open = fopen("dates.txt","w+");
    fwrite($file_open, $_POST['addition']);
    fclose($file_open);
    }

echo '<form action="<?=$PHP_SELF?>" method="POST">
    <textarea name="addition" COLS=50 ROWS=20>';
    $datalines = file ("dates.txt");
        foreach ($datalines as $zz) {
            echo $zz;
        }                   
echo '</textarea><input type="submit" name="button" value="Save Evenings Before Solemnities File"></form>';
            echo '</td>';
2

First of all put these all code inside in init hook, and use plugin_dir_path() func to get plugin dir path.

add_action('init',function(){

    $plugindir = plugin_dir_path( __FILE__ );

    if(isset($_POST['addition'])){ 

        $file_open = fopen($plugindir."dates.txt","w+");
        fwrite($file_open, $_POST['addition']);
        fclose($file_open);
    }
        echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
        <textarea name="addition" COLS=50 ROWS=20>';

        $datalines = file ($plugindir."dates.txt");

        foreach ($datalines as $zz) {
            echo $zz;
        }                   
        echo '</textarea><input type="submit" name="button" value="Save Evenings Before Solemnities File"></form>';
        echo '</td>';
        exit;

});

Try this above code.

3
  • Sure this line will causes problem : echo '<form action="<?php echo $PHP_SELF?>" method="POST"> <textarea name="addition" COLS=50 ROWS=20>';
    – Benoti
    Dec 5 '16 at 8:28
  • i have edited the code please check now
    – GKS
    Dec 5 '16 at 8:30
  • 1
    $_SERVER['PHP_SELF'']
    – Benoti
    Dec 5 '16 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.