ajax - Where to include php files in wordpress and how to refer to them later

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Upd

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 6 years ago.

Improve this question

I am currently creating a customize web page and I am willing to refer to a php file inside an ajax command open(GET,"url php",true) so my question is where to put my php file so that it would be seen by this snippet and how to call it.

Thanks

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 6 years ago.

Improve this question

I am currently creating a customize web page and I am willing to refer to a php file inside an ajax command open(GET,"url php",true) so my question is where to put my php file so that it would be seen by this snippet and how to call it.

Thanks

Share Improve this question edited Apr 3, 2019 at 14:46 rudtek 6,3835 gold badges30 silver badges52 bronze badges asked Apr 3, 2019 at 10:05 ahmedahmed 311 silver badge3 bronze badges 1
  • 4 have you looked at creating a REST API endpoints? You should never ping a PHP file directly in WP as it has security and maintainability consequences – Tom J Nowell Commented Apr 3, 2019 at 10:51
Add a comment  | 

3 Answers 3

Reset to default 5

You can put your code in a plugin, then create a REST API endpoint.

For example lets create a plugin, just put a PHP file wp-content/plugins/ahmedsplugin.php or wp-content/plugins/ahmedsplugin/plugin.php with a comment in it at the top like this:

<?php
/**
 * Plugin Name: Ahmeds Plugin
 **/

... your code goes here ...

Now you'll see "Ahmeds Plugin" in the plugins folder. A plugin is just a PHP file in the plugins folder, or a subfolder of the plugins folder, that has that comment at the top.

You can put WP code in here that will run such as filters, actions, classes, etc. Some people put these things in their themes functions.php, but themes are for visuals/presentation, and you lose it all when you switch a theme.

Note this code will run before a theme template is loaded, and before the init hook. You should try to put all your code in hooks for better control of when it runs.

Now lets make an endpoint that your javascript can make a request to. Start by telling WP you want to create an endpoint:

add_action( 'rest_api_init', function () { // when WP sets up the REST API
        register_rest_route( // tell it we want an endpoint
                'ahmed/v1', '/test/', // at example/wp-json/ahmed/v1/test
                [ 
                        'methods' => 'GET', // that it handles GET requests
                        'callback' => 'ahmed_test_endpoint' // and calls this function when hit
                ]
        );
} );

When you visit /wp-json/ahmed/v1/test it will run the function ahmed_test_endpoint, so lets create that function:

function ahmed_test_endpoint( $request ) {
    return 'Hello World';
}

The REST API will take whatever you return, JSON encode it, and send it out.

You can return a WP_Error object if something goes wrong and it will change the HTTP codes etc and output the message. If you need any of the parameters, use the $request object, e.g. if you added ?bananas=yummy to the URL, then $request['bananas'] will contain "yummy", just like $_GET.

Remember to flush/resave your permalinks when you add a new endpoint!

Now when we go to yoursite/wp-json/ahmed/v1/test you'll see "Hello World"

If you like, you can expand register_rest_route to add more information, such as which parameters your code expects, how to validate them, checking if the user is logged in and has permission to do what they want to do, etc.

If you do this, the REST API will even help you out, so if you tell it there's going to be an ID parameter, but none is given, it'll tell you the ID parameter is missing. Admin AJAX, or stand alone PHP files won't do this, and it makes debugging very difficult. It also greatly improves security

Why Not A Standalone PHP File?

  1. WP APIs won't be available so you'll need to bootstrap WP manually, which is painful
  2. The endpoint is available even if your plugin/theme is deactivated which can pose a security issue
  3. Other plugins can't hook into it, so no benefits from caching or optimisation plugins, so there's a possible performance penalty
  4. You'll need to roll out all the security checks yourself, building them manually, and that's not easy. WP will do them for you if you use the REST API

In the olden days, the solution was to use the Admin AJAX API, but it doesn't do a lot for you. Also, it's very unforgiving, if you don't match your AJAX actions correctly, you get a cryptic response. It also does no checking beyond logged in/out, no validation or sanitisation, and no discovery mechanisms

If you want to do some AJAX in your theme and you're asking where to put the PHP file that will process such request, then... Nowhere is the real answer...

In WordPress you should deal with AJAX a little bit different than in normal PHP apps. There is already a mechanism for such requests.

So first, you should localize your JS script:

wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) );

Then you should use that variable as request address:

// it's important to send "action" in that request - this way WP will know which action should be processing this request
jQuery.get(ajax_object.ajax_url, {'action': 'my_action', ...}, ... );

And then you should register your callbacks that will process the request:

add_action( 'wp_ajax_my_action', 'my_action' );  // for logged in users
add_action( 'wp_ajax_nopriv_my_action', 'my_action' );  // for anonymous users
function my_action() {
    ...
}

You can read more on that topic here:

  • https://codex.wordpress/AJAX_in_Plugins

You should be able to use the functions defined in your PHP files by including it into your functions.php file. It's kinda hard to advice you better without knowing more about your structure. But functions.php seems to be a good location to require your file IMHO.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745492040a4630028.html

相关推荐

  • ajax - Where to include php files in wordpress and how to refer to them later

    Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Upd

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信