When I attempted something similar I experienced the same issue where the HTML code of the page I was generating the CSV from would be included within the CSV mixed with the data I actually wanted.
The two things I figured out, after searching and reading and reading and searching was to use ob_end_clean(); before I start outputting the content of the CSV. ob_end_clean() is ‘Output Buffers End/Clean` – https://www.php.net/manual/en/function.ob-end-clean.php
I also added die() at the end of the CSV output code to, well, exit the process. You can also use exit().
function generate_pdf( $array ) {
ob_end_clean(); //ADD THIS LINE
header( 'Content-Type: text/csv' );
header('Content-Disposition: attachment; filename="sample.csv"');
$fp = fopen( 'php://output', 'wb' );
foreach( $array as $line ) {
$val = explode( ',', $line );
fputcsv( $fp, $val );
}
fclose( $fp );
die(); //add this as well
}
Related Posts:
- What are the differences between WPINC and ABSPATH?
- Making plugin unique to not conflict with plugins with the same name
- How to find out if option exists but is empty?
- How to pass JavaScript variable to PHP in wordpress widget?
- Unable to add admin notice on plugin activation
- How To Ignore a Filter On Applying Filter the Content In a Function
- How can I include PHP-Code to my post?
- Errors while using ajax from external wordpress page
- shortcode doesn’t work
- Scope for PHP Variables Assigned in functions.php or a plugin
- Where should I save an API key of my user when installing my plugin?
- Custom form action to handle data inside a plugin
- How to periodically scrape and cache strings from remote txt files. – My First Plugin
- WP Post Template – Templates in own folder
- wordpress plugin is not activating from widget
- Using a custom plugin to capture input data via Ajax and PHP
- Image upload and download from front-end
- How to output CMB2 select options from repeated groups select elements?
- Ajax call returning 0
- How to deal with WordPress and Pocket API to automate content curation on my hosted wordpress blog? [closed]
- WP All Import – Execute Imports
- Conditional attributes and logic per product category
- Why namespaces are rarely used in WordPress plugins?
- Redirect to another page using contact form 7? [closed]
- Create a plugin from within WordPress
- Class variables not correct on page
- Why is one phpunit test throwing an error on one class when all other classes are similar without error?
- Activate my plugins via FTP
- Why is my custom post type not being activated on plug-in activation?
- How to add apply_filter for a class property in plugin
- Issue running db create table query from static method
- How to make multiple admin pages for one plugin?
- A server-side hook failed when committing plugin code to SVN
- Content-Security-Policy Headers are there and showing the correct settings, but still getting a refused connection
- How do I add $_SESSION[”] to my wordpress page?
- output html on post or page from custom plugin [closed]
- Built a second plugin but it overwrote the first one
- How to delete all categories programatically?
- How to use custom Javascript code inside a plugin?
- How to add/change (woocommerce) product tab icon
- Comment “like” problem – “users who like this” avatar linking to current user profile instead of “liker’s” profile
- I want to generate the pdf and save it in library?
- Way to send multiple values for column to MySQL?
- Using meta_query in a WP_Query not working for numbers properly
- How to write to a separate PHP file in plugin settings
- Create “new pages” within the admin section of plugin
- More gentle way to hook WordPress custom url
- How to display results from a data table with an encrypted user id?
- How to Call Specific .PHP file on add_submenu_page selection in Plugin Development?
- How to Add text value in Automatically in this conditions?
- Checkbox show / hide output result
- Conditional delete metadata does not works
- admin_notices show after load completed
- How can we get this dynamically as this folder may not be by the same name always → wp-admin
- Custom Plugin: How to Include Install Buttons of other 3rd Party Plugins?
- Hide content for specific users with id
- adding dynamic/multiple slug values in ‘option_none_value’
- Why is my shortcode not working?
- Rename a folder via HTML POST request
- Getting Fatal error: Uncaught Error: Call to undefined function plugin_dir_path() when linking to another file within my wordpress plugin
- How can I see $post object in frontend from functions.php?
- Move plugin pages from dashboard to front-end
- working code, not working – Plugin Dev
- WordPress plugin blog creation
- implement checkbox or toggle switch in plugin code
- 500 Internal server error wp_handle_upload_prefilter
- Create a form and have custom menu display based on user answers?
- Can’t access 3rd party API, code works on local server but not on wordpress
- Get post content inside plugin class method
- submit form data to wordpress existing database table using my plugin
- register_activation_hook doesn’t fire at all
- My ajax request don´t work and return 0
- Problem with conditional woocommerce custom checkout field
- How to limit each front-end user to view just his own uploaded files on Amazon S3?
- How can I insert a record into a custom table from my custom form in my custom admin page?
- Create csv file in plugin
- Saving plugin data returns “You do not have sufficient permissions to access this page.”
- Plugin Breaking WordPress Login
- How to add a handler for a button in plugin?
- Can anyone tell me why I can’t edit a plugin when it is installed without having to re-install?
- Load my plugin before cache
- How to fix ‘Call to undefined function do_settings()’ error?
- What is the source outline for a basic word press plugin?
- Submit posts in two different post types with the same form
- wordpress select multiple options and illegal string offset ‘timeslot’ [closed]
- Creating a press page for our project
- how to search through plugin in wordpress cimy-user-extra-fields?
- Function not being called on form submit, only blank admin-post.php page
- How can I save the selected page in the dropdown after anyone clicks on Save Changes?
- How can I save the selected page in the dropdown after anyone clicks on Save Changes?
- How to give specific class name to category in wordpress with code snippets
- WordPress Throwing Deprecated Errors on its own Files
- plugin doesn’t retrieve data from database
- REST API – filters not working inside plugin class
- WordPress site server getting blocked due to resource abuse
- Getting table to format properly in RSS feed emails
- Getting table to format properly in RSS feed emails
- Woocommerce attatch files and custom input meta to cart items and then to order
- Remove H1 title in admin post edit screen
- Call to undefined function create_function() – PHP 8.2