Using fwrite() and “a” appends multiple times instead of once

You could try putting your code in a WordPress Action or Filter call:

add_action( 'init', 'my_file_function' );

function my_file_function() {
    $content = "some text here\n";
    $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/zerif-lite-child/myText.txt","a");
    fwrite($fp,$content);
    fclose($fp);
}

The flag (‘a’ vs ‘wb’) doesn’t have anything to do with your problem, it just happens to showcase it. For whatever reason, your functinos.php seems to be running more than once. You could start with a fresh copy of the default theme and try your code again, but you should also get in the habit of writing your code inside of an action call.

This helps ensure that the code you want to run is running at the appropriate time. It also allows you to hook in to various places in WordPress and generally makes coding for it much more powerful.

tech