How to handle URL's in WordPress plugins?

181
January 24, 2019, at 05:20 AM

I am having a hard time articulating exactly what it is that I am needing/doing wrong.

I am writing a plugin for WordPress that has multiple PHP files that are called via AJAX.

The issue I am having is how do you tell wordpress that a URL is relative to a plugin?

I have following code in the pluginname.php file:

function build_form() {
    // hacky way of providing the url to the front end.
    echo ("<script> var submit_url = \"" . plugin_dir_url( __FILE__ ) .  "submit.php \" </script>");
    $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' );

Then on the front-end we post to the echo'd URL.

$.ajax({
       url: submit_url,
       data: {},
       type: 'POST'
    });

It works fine as far as telling the plugin what the submitted URL is, but seems a little hacky.

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.

wp_mail(),plugin_dir_url()

I feel like I am missing something very fundemental to do with registering URL's or something similar in the plugin file.

If I can be more clear, please do let me know how.

Rent Charter Buses Company
READ ALSO
How to cache multiple imdb rating results

How to cache multiple imdb rating results

I'm trying to make a movie list where is movie name and I'm trying to get imdb ratings, from url (database) and code worksBut my problem is that if I have 50 movies in the list, this code makes 50 different omdb api calls and it slows down my site very well

175
PHP: Can I safely use explode on a multi-byte string

PHP: Can I safely use explode on a multi-byte string

Can I safely use explode() on a multi-byte string, specifically UTF8? Or do I need to use mb_split()?

254
Dynamic redirect inside page PHP

Dynamic redirect inside page PHP

Today I have so troublemaking question for you

162
configure php backend and mysql database for angular app

configure php backend and mysql database for angular app

Currently, I am developing an Angular app and I request your help to setup the backendOn my laptop I have installed xampp on "localhost:8080", I have created a mysql database using phpMyAdmin

159