• Hello! I’m fairly new to the plugin and WordPress in general, so I apologize in advance if my questions are a bit silly.

    I’m trying to create a button on my site; on a click, it should run a php snippet (without reloading the page, if possible, though I suspect I’ll need and AJAX for that).

    So I’ve written the function in a snippet, but I don’t know how should I add it to the WordPress functions (You know, “add_action” and all that jazz. It’s still quite confusing to me)and then “link it” to the button.

    Could I please get some enlightenment?

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Imagefblanco

    (@fblanco)

    Small update. I’va managed to add this to a snippet:

    function testphprun(){
      if( isset($_GET['testphprun']) ) {
        set_time_limit(0);
        $user_id = get_current_user_id(); 
      	$user_info = get_userdata($user_id);
      	$mailadress = $user_info->user_email;
    	$path = file_get_contents('./wp-content/emails/'.$mailadress.'.txt'); 
    	$result = "";
    	exec('py ./wp-content/script.py '.$mailadress.' '.basename($path).'', $result, $return_var);
    	foreach ($result as $line) {
           echo $line;
      	}
      } 
    }	
    
    add_action('init', 'testphprun');

    So now, whenever I click this button:

    <form method="post" action="http://localhost/wordpress/pdfPython/?testphprun">
        <input type="submit" value="RunScript">
    </form>

    I get redirected to /localhost/wordpress/pdfPython/?testphprun and the php script runs. I’m sure it’s not the best solution, but it works.

    Now I’d like to modify the snippet so that when the page loads, a certain file is downloaded. I tried adding the appropiate headers and a “readfile()” call to this same snippet (after the current code, of course) but that apparently prevents the script from running. It just downloads the file.

    • This reply was modified 5 years, 3 months ago by Imagefblanco.
    Plugin Author ImageShea Bunge

    (@bungeshea)

    Hi @fblanco,

    Is the file you’d like to download already present on the server? You should be able to manage it using the wp_redirect() function to redirect the current request to the file you want to download.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Running snippet on button click’ is closed to new replies.