title: How to handle URL's in WordPress plugins?
body: <p>I am having a hard time articulating exactly what it is that I am needing/doing wrong.</p>
  
  <p>I am writing a plugin for WordPress that has multiple PHP files that are called via AJAX.</p>
  
  <p>The issue I am having is how do you tell wordpress that a URL is relative to a plugin?</p>
  
  <p>I have following code in the <code>pluginname.php</code> file:</p>
  
  <pre><code>function build_form() {
      // hacky way of providing the url to the front end.
      echo ("&lt;script&gt; var submit_url = \"" . plugin_dir_url( __FILE__ ) .  "submit.php \" &lt;/script&gt;");
      $asubHTML = file_get_contents(plugins_url('/estimate-form.php',__FILE__ ));
      echo $asubHTML;
   }
  
  //used on a page like a widget
  add_shortcode( 'estimate-form', 'build_form' );
  </code></pre>
  
  <p>Then on the front-end we post to the echo'd URL.</p>
  
  <pre><code>$.ajax({
         url: submit_url,
         data: {},
         type: 'POST'
      });
  </code></pre>
  
  <p>It works fine as far as telling the plugin what the submitted URL is, but seems a little hacky.</p>
  
  <p>But when you get to the actual PHP script that is called, you are no longer within the scope of the WordPress framework and as such, the WordPress functions will not work. e.g.</p>
  
  <p><code>wp_mail(),plugin_dir_url()</code></p>
  
  <p>I feel like I am missing something very fundemental to do with registering URL's or something similar in the plugin file.</p>
  
  <p>If I can be more clear, please do let me know how.</p>