简体   繁体   English

WordPress:如果要从javascript文件中调用它,我的PHP脚本应该放在哪里?

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

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. 我制作了一个Wordpress主题,并希望通过jQuery调用弹出式联系表单,如果该表单经过验证,我的js文件将调用一个php文件来发送电子邮件。

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 wordpress directory but all are not successful. 我只想知道应该在哪里放置mailer.php表单,因为我尝试将其与js文件放在同一目录中,或者放在主题根目录中或在wordpress目录中,但都没有成功。

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 您可以使用get_bloginfo()方法访问它: 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. 但是请记住,直接调用文件时不能调用wordpress函数。 If your mail function just sends out a mail without depending on WP, then you should be ok. 如果您的邮件功能只是发送邮件而不依赖WP,那么您应该可以。 Otherwise you will need to hook it into WP. 否则,您需要将其挂接到WP中。

A method I always use is to have this bit of code in functions.php 我一直使用的一种方法是在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. 然后,在包含的主题文件夹中有一个名为ajax_calls的文件。 This method will handle all ajax calls with WP in the stack too. 此方法也将处理堆栈中带有WP的所有ajax调用。 Make sure to have 'is_ajax_request' in all your ajax requests. 确保所有ajax请求中都包含“ is_ajax_request”。

Try putting it in the root folder of the WordPress directory and then calling it like this: /js/popup_form.js . 尝试将其放在WordPress目录的根文件夹中,然后像这样调用它: /js/popup_form.js Note the / in front, if you didn't know it means start from root. 注意前面的/ ,如果您不知道它的意思是从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). 它将具有自己的自包含MVC组件(大多数都为空)。

Only existing example I could found for this is JSON-API . 我只能为此找到的现有示例是JSON-API Its bit bigger but simpler. 它更大但更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM