.Net developer here trying to learn some of the basics of wordpress. I have a basic question but I guess there’s so much that I don’t know that I can’t even get a relevant result when searching here or via Google.
If I create a plugin and activate it, and run flush_rewrite_rules();
in my activation code, should I be able to put the path to a page in my plugin into the address bar of a browser and get the results of the script in my plugin page?
For example, say my plugin is named my-plugin. The path to my-plugin is of course wp-content/plugins/my-plugin
. I create the file wp-content/plugins/my-plugin/my-page.php
.
The contents of my-page.php are:
<?php
if (!isset($returnVal))
$returnVal = new stdClass();
$returnVal->status = "success";
$returnVal->info = "seems to work";
echo json_encode($returnVal);
Should I be able to access that by entering this in the browser:
https://my-site/wp-content/plugins/my-plugin/my-page.php
I’m asking this b/c I always get a 404 when I do that.
I’ve been beating my head against this for awhile now and I’ve come to the conclusion that there’s just something I’m missing about the way wordpress works.
More details:
I’m using the Avada theme (v7.6.1) Form builder. It has an option to POST to a url when the user clicks a submit button. I need it to POST the form data to the page in my plugin. Then my plugin page needs to return success or error to the frontend page.
I figured I would start at the bottom and work my way up to accomplish this, so I decided to see if I can even get my plugin to return the success/error object. This is what I’m stuck on.
If I get this to work then then next step would be putting the url to my plugin page in the Avada Form Builder options for what happens when the user clicks the submit button. I haven’t got to that point b/c I’m stuck on the above.
Here’s a post on s/o I made regarding the bigger picture.
UPDATE: SOME PROGRESS MADE
@kero’s comment pointed me in the direction of custom REST endpoints and I was able to make a GET request work in the browser to https://my-site.com/wp-json/my-plugin/v1/my-endpoint and it returns this:
{"status":"success","info":"seems to work"}
Now I’m stuck trying to get the Avada form to call that endpoint. I’v tried the following urls:
- wp-json/my-plugin/v1/my-endpoint
- /wp-json/my-plugin/v1/my-endpoint
- https://my-site.com/wp-json/my-plugin/v1/my-endpoint
Both 1 & 2 result in this:
{"status":"error","info":"url_failed"}
#3 returns simply a -1
Any ideas on how to get the Avada form to call that endpoint and get the result?
UDPATE: Requested details provided:
Here’s the REST endpoint code from my main plugin file:
add_action('rest_api_init', function () {
register_rest_route('my-plugin/v1', '/my-page', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
));
});
function my_awesome_func()
{
if (!isset($returnVal))
$returnVal = new stdClass();
$returnVal->status = "success";
$returnVal->info = "seems to work";
return $returnVal;
}
The methods in the snippet above is set to "GET" but I also tried it set to "POST" and the Avada form has a setting where you choose GET/POST and I tested it both ways. Neither the GET nor the POST would work for me.
The Avada form is POSTing to wp-admin/admin-ajax.php
with the following key:values. Interestingly it gets a 200 from the POST but still returns the same values mentioned above.
action:fusion_form_submit_form_to_url
fusionAction: wp-json/my-plugin/v1/my-page
formData: email=&main_license_number=&credit_card_last_four=&fusion_privacy_store_ip_ua=false&fusion_privacy_expiration_interval=48&privacy_expiration_action=anonymize&fusion-form-nonce-1808=3d676aef78&fusion-fields-hold-private-data=