1

I'd like if someone could give me some advice on creating this script, which I will add to the existing plug-in (see code below) script below.

So what I have now (with the script below) is a means to insert a predefined set of defaults into the wordpress site. What I'm wanting to add, is a helper utility, activated by a button or link that just reads "Copy Settings", that will take a site's existing settings (the sb2_options), write that to a file, then package the resulting file, along with the original file into a new zip file that essentially becomes a custom copy of the original plug-in for use in another site.

So the code needs to take an existing .php file containing the static code, open it up for writing, then insert all the name/value pairs from the wordpress options table matching a specific prefix (for example, all my custom options are prefixed with "sb2_"). Once it's done this, it would save the resulting file as "plugin.zip", for example and stream it to the user for download.

Here is the code that I have now, which sets up the site's defaults...

<?php /** * Plugin Name: my plugin * Description: Sets up your sites defaults. * Version: 1.0 */ function sb2_plugin_init() { if ( get_option( 'sb2_plugin' ) == "") { //Begin Insert List here. Open the file and write out all the name value pairs, just like in the example. //Option 1", $sb2_option1 = "test"; //Option 2", $sb2_option2 = "test"; //Option 1", $sb2_option3 = "test"; //End insert list here //update site defaults update_option('sb2_option1', sb2_option1); update_option('sb2_option2', sb2_option2); update_option('sb2_option3', sb2_option3); //etc // Create post objects $my_post = array(); $my_post['post_title'] = 'Main Blog Post Title'; $my_post['post_content'] = 'Main Blog Post Content'; $my_post['post_type'] = 'post'; //TODO >>> NEED TO MAKE THE POST STICKY // Insert the post into the database wp_insert_post($my_post); wp_cache_flush(); update_option('sb2_plugin', "1"); } } add_action( 'init','sb2_plugin_init'); 
1
  • None of the parts from the title are even attempted here. Looks like a coding request, not a programming question. Commented Sep 5, 2024 at 8:49

1 Answer 1

0

to download a file generated by your wordpress plugin, you can use the following code. and then the download url is wp-admin/admin-post.php?action=MY_PLUGIN__download_file.

add_action("admin_post_MY_PLUGIN__download_file", function () { // here you can test if the current user is allowed to access this file // file content generation $file_content = "abc\nhello"; $file_name = "hello.txt"; // download $file_size = strlen($file_content); $file_name = str_replace("\"", "\\\"", $file_name); header("Content-Type: application/octet-stream"); header("Content-Length: $file_size"); header("Content-Disposition: attachment; filename=\"$file_name\""); echo $file_content; exit(); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.