1

So I have a single.php wich contains this.

    $('#ajax').load('<?php echo get_template_directory_uri(); ?>/test.php?choices=<?php echo $post_id; ?>');

This loads test.php into this

     <div id="ajax"></div>

The test.php loads in perfectly. But I want to use a function from functions.php In single.php i can perform any function from functions.php. Since this is the wordpress standard.

But when I try to use a function from it in test.php I get an error. This is the error

  Fatal error: Call to undefined function add_theme_support() in /customers/9/9/c/papercraftplaza.com/httpd.www/wp-content/themes/papercraftplazaV2/functions.php on line 2

This is everything that is in test.php. The functions I want to use are currently a comment. How would i include it so I can use my function?

  <?php
  include "functions.php";

  $_GET['choices'];
  $passedvar = reset($_GET);

  //setPostDownloads($passedvar);
  //echo getPostDownloads($passedvar);

  ?>
  <p>You downloaded papercraft: <?php echo reset($_GET); ?></p> 
Ben Aerens
  • 157
  • 3
  • 13
  • Does `functions.php` load the wordpress files? – Barmar May 26 '17 at 22:02
  • No, functions.php just contains all my functions nice and tidy in one php file. In wordpress i can just use the function setPostDownloads($passedvar); on my index.php or any page. But for some reason it won't work in my .load test.php – Ben Aerens May 26 '17 at 22:08
  • Maybe this is helpful: https://www.sitepoint.com/how-to-use-ajax-in-wordpress-a-real-world-example/ – Barmar May 26 '17 at 22:10

1 Answers1

0

I had to include wp-load.php apparently This should replace the include functions. It works now!

  $path = preg_replace('/wp-content.*$/','',__DIR__);
  $path = preg_replace('/wp-content(?!.*wp-content).*/','',__DIR__);
  include($path.'wp-load.php');
Ben Aerens
  • 157
  • 3
  • 13
  • I works but is very very crazy :) Great problem solving skills though! When you include `wp-load.php`, you're calling *the whole WP all over again* in a single/simple Ajax call.... [This is how to use Ajax in WP](https://stackoverflow.com/a/13614297/1287812) – brasofilo May 27 '17 at 23:29