0

I am trying to call a function to save changes from a submit button, but I am not accustomed to PHP programming, as I have just started. I tried Googling around a bit, but didn't find anything too useful or applicable to my situation.

At the bottom of my script, I have this function which creates a dashboard page:

function blank_add_pages() {



// anyone can see the menu for the Blank Plugin
  add_menu_page('Development Log','Dev Log Plugin', 'read', 'development_log', 'blank_overview', 'dashicons-book');
  // http://codex.wordpress.org/Function_Reference/add_menu_page

  // this is just a brief introduction
  add_submenu_page('blank_overview', 'Overview for the Blank Plugin', 'Overview', 'read', 'blank_overview', 'blank_intro');
  // http://codex.wordpress.org/Function_Reference/add_submenu_page

}

function blank_overview() {

$logText = wp_remote_fopen('http://com.areonline.co.uk/wp-content/plugins/blank/devlog.txt');

?>
<style>
#devlog-content {
    width: 100%;
}
</style>
<div class="wrap"><h2>Labyrith Development Log</h2>
<p>A simple overview of our work.</p>
<textarea id="devlog-content"><?php echo $logText ?></textarea>
<?php submit_button(); ?>
</div>
<?php
exit;
}

?>

but I am unsure of how to trigger a save call to save the contents in the textarea (the changes) back to the file located at the link on line 27-ish.

Does anyone know how to do this?

  • 1
    You're pretty confused. Google "admin ajax + WordPress" and follow some tutorials - you need to have your JavaScript make a request to your WordPress PHP backend. – editor Apr 23 '16 at 20:08
1

You need to have a <form> element. By not specifying a form action attribute the form posts to the same page you are on which makes this easy to process.

Also an id on the textarea is not enough you need to add a name attribute to catch with $_POST for processing. Also adding a nonce field is good practice, and checking permissions to be safe. Try something like this:

 function blank_overview() {

     $logPath = dirname(__FILE__).'/devlog.txt';
     $logText = file_get_contents($logPath);

     if (isset($_POST['devlog_content'])) {
         if ( (wp_verify_nonce($_POST['devlog_nonce_field'],'devlog_nonce_action'))
           && (current_user_can('manage_options')) ) {
              $logText = $_POST['devlog_content'];
              file_put_contents($logPath, $logText);
              echo "Dev Log Saved.<br><br>";
          } else {echo "Warning: Dev Log NOT Saved.<br><br>";}
     }
?>

<style>#devlog-content {width: 100%;}</style>
<div class="wrap"><h2>Labyrith Development Log</h2>
<p>A simple overview of our work.</p>
<form method="post">
<?php wp_nonce_field('devlog_nonce_action','devlog_nonce_field'); ?>
<textarea name="devlog_content" id="devlog-content"><?php echo $logText ?></textarea>
<input type="submit" class="button-primary" value="Save Dev Log">
</form>
</div>
<?php } ?>

Also you will not I have removed the exit. It is not needed here and can prevent admin footer scripts and suchlike from completing because it stops further output. The function will automatically return and allow other things to complete without it.

| improve this answer | |

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.