开发者

Wordpress: Where to place my php script if I want to call it from a javascript file?

开发者 https://www.devze.com 2023-02-10 19:35 出处:网络
This is a file location not a script or functionality problem. I made a Wordpress theme and wanted to call a popup contact form via jQuery, if the form is validated, my js file will call a php file to

This is a file location not a script or functionality problem. I made a Wordpress theme and wanted to call a popup contact form via jQuery, if the form is validated, my js file will call a php file to send the email.

Here is the file/folder structure:

wp_theme_root
|
js/popup_form.js
|
mailer.php

I just want to know where should I place the mailer.php form, as I tried put it the same directory with the js file or in theme root or in the wordpres开发者_StackOverflow中文版s directory but all are not successful.

Thanks for helping.


If it's part of the theme you are building you need to place it in the theme folder or an includes folder inside the theme. You can access it by using the get_bloginfo() method : http://codex.wordpress.org/Function_Reference/get_bloginfo#Template_Directory

But keep in mind that you can't call wordpress functions when you directly call the file. If your mail function just sends out a mail without depending on WP, then you should be ok. Otherwise you will need to hook it into WP.

A method I always use is to have this bit of code in functions.php

add_action('wp', 'check_ajax'); //run this method for every call
function check_ajax() {
    if (isset($_GET["is_ajax_request"])) { 
        get_template_part('includes/axax_calls'); die();
    }
} 

And then I have a file called ajax_calls inside the theme folder in includes. This method will handle all ajax calls with WP in the stack too. Make sure to have 'is_ajax_request' in all your ajax requests.


Try putting it in the root folder of the WordPress directory and then calling it like this: /js/popup_form.js. Note the / in front, if you didn't know it means start from root.

Hope this works for you.


Cleanest way for this will be to write code which handles mail sending as plugin. Which will have its own self contained MVC components (most of then just empty).

Only existing example I could found for this is JSON-API. Its bit bigger but simpler.

0

精彩评论

暂无评论...
验证码 换一张
取 消