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.
Related Posts:
- start_lvl Ignored in Custom walker_nav_menu
- Allowed memory size of 268435456 bytes exhausted (tried to allocate 7146491840906359738 bytes)
- How do I make my function add variables/values to the $post object?
- How do I get the author’s page url from their ID?
- Calculate percentage of post by category
- Change comments form title on a page by page basis
- Remove class that has been added by parent theme
- How to check from which page the user is coming to the current page?
- Get the title before comma
- Design view breaking on Pages
- Execute only on a certain page
- How do I know what variables are passed in a filter/action and what their meaning is?
- “Can’t use function return value in write context” error
- Display 4 chronological posts starting with a random post
- Wrap each shortcode in array to div
- Warning: array_pop() expects parameter 1 to be array, boolean given
- Replace comment avatars and links at the same time
- Check if Product is in a Specific Category in Functions.php
- Adding ads code between comments
- Create Logout Link WordPress Admin Menu
- How to Display Image Meta underneath EVERY image in EVERY post
- Limiting woocommerce line_total decimal length
- Run a filter when a walker runs
- Shortcode content filter?
- Swapping wp_dropdown_categories function with wp_category_checklist
- Display fields as values in array from external SQL DB
- Add a variable in functions.php and call it in single.php
- How to display posts by current user in a drop down
- Linking text within textarea of custom meta box
- what is the best practice to add new field to an api route
- Auto-update products after they were published – Woocommerce
- php function to display commenter username or login
- How to change menu order item
- responsive.css in the WordPress should be prioritized
- How to get Post Type from the functions.php file
- How to display login form anywhere, when user isn’t logged in, without redirecting?
- Edit the_category (); for a hierarchical display
- Omit custom post type from wp-sitemap.xml based on meta key using wp_sitemaps_posts_query_args
- Multiple meta_key in one global $wpdb;
- Widget back end radio button issue
- Using data sent via AJAX in multiple functions on a WP plugin
- Utilize Function in Child Theme / Template File
- GET problem in WordPress
- HTML Special Characters in URL string [closed]
- Create page template via functions.php?
- Send a mail to specific address in a custom field when a new comment is made on a specific post
- How to have different site identity logos on each page on Astra Theme [closed]
- CSS change in woo commerce Place Order Text [closed]
- Overide Variable in Child Theme
- child parent styles enqueue order
- Add URL parameter to all internal links using a specific theme
- Warning: call_user_func() expects parameter 1 to be a valid callback, function
- Change MySQL PDO connection to a WPDB connection
- Randomize Color Scheme Selection in Theme
- Hide a div when a custom field is empty
- AJAX function not working [closed]
- Modify WooCommerce used to get all orders in dashboard
- Can’t log in to WordPress wp-admin after adding code to functions.php
- How to change basename url for wp-admin?
- How to exclude meta no index pages from wp_list_pages
- display condition based on post term and status
- Only the first image uploads
- Search by date in WordPress
- Hide a div if the fiels is empty
- Illegal string offset checkbox
- require get_template_directory() . ‘path/to-my/file.php’ BREAKS customize > themes functionality
- removing the standard dashboard widgets in WP so I Can replace with custom
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- Trying to add some custom text into WordPress Post title via function.php
- Edit a function to take different actions based on user role
- How to change order inside foreach using wp_enqueue_script?
- Change header on one page in WordPress
- I got big white screen when i attach main style sheet of Html in functions.php
- Using Javascript On Page – Header and Footer Now Missing
- Add Product Permalink in woocommerce admin order page
- Unzip_file causing Media file upload error
- Cleaner ‘for each’ loop for registering scripts in functions.php
- How do I make comment-reply-button with that takes to comment reply form on click
- How to generate customized filtered output for the gallery
- How to get all users by user_login as a json_encode array?
- Escaping a Single Quote in str_replace for Nav Function
- Allow BBPress participant role to trash topics
- Display Year and Month from custom field + Age Calculator
- insert thumbnail image from php script
- WordPress rating by views [closed]
- Get author meta of all writers
- Trying to see if page is category or single and displaying title with appropriate heading tag
- WordPress causing all code to be displayed on line 1. Receiving multiple errors after cleaning cookies and cache
- Replace TinyMCE with Custom Built Editor
- Particular meta tag – viewport – insertion when dealing with plugin
- WP Fatal error: Call to undefined function get_template_directory_uri()
- Remove default Image Sizes
- A better way to write this php function
- Alternative to php echo within code
- Add multiple HTML attributes to an Elementor button
- Automatically change insurance quantity based on cart total [closed]
- /page/2/ redirect to 404
- How to pass value to add_filter wpcf7_form_tag from another function?
- hiddenQuestions.find is not a function