WordPress 插件管理页面 - 在链接的 php 文件中使用 WordPress 功能 [英] WordPress plugin admin page - using WordPress function in linked php file

查看:23
本文介绍了WordPress 插件管理页面 - 在链接的 php 文件中使用 WordPress 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个非常简单的 WordPress 插件,并想对页面上的某些字段进行一些实时验证,这就是我被卡住的地方.我一直在到处寻找有用的东西,但我根本无法让它发挥作用.

I've been developing a pretty simple WordPress plugin, and wanted to do some live validation of certain fields on the page, which is where I am stuck. I've been looking everywhere for something that worked, and I simply can't make it function.

我一直试图让 ajax 正常运行,但我知道我遗漏了一些明显的东西,我就是想不通.

I've been trying to get the ajax to function properly and I know I'm missing something obvious, I just can't figure it out.

主函数文件包含这个来注册我的js文件.

The main function file includes this to register my js file.

function on_screen_validation() {
    wp_enqueue_script( "field_validation", path_join(WP_PLUGIN_URL, basename( dirname( __FILE__ ) )."/field-validation.js"), array( 'jquery' ) );
}
add_action( 'admin_print_scripts', 'on_screen_validation' );

js 运行这段代码来捕获 onblur 命令并将值传递给 php 验证文件.

The js runs this code to capture the onblur command and pass the value to the php validation file.

jQuery(document).ready(function() {
    //run field validation on username on blur
    jQuery('.valusername').blur(function() {

        var usernameID = jQuery(this).attr('id');
        var usernameVal = jQuery('#'+usernameID).val();
        var thisFunction = 'validateUserName';

        jQuery.post("mywebaddress...validation.php",{Function: thisFunction, thevars: usernameVal}, function(data) {
             alert(data); //would update validation message here
        });
    });
});

validation.php 脚本如下所示:

And the validation.php script looks like this:

if(isset($_POST['Function'])){
    call_user_func($_POST['Function'], $_POST['thevars']);
}

function validateUserName($username){   
   if ( username_exists($username) ) {
       echo $username.' does exist'; 
   } else {
       echo $username.' doesnt exist';
   }
}

显然,我现在只是使用警报来确保正确检查数据.

Obviously I'm just using alerts for now to make sure the data is being checked properly.

如果我去掉 WordPress username_exists 函数,然后只回显一个字符串,它就可以正常工作.但是使用 username_exists,它会产生 500 内部服务器错误.我需要知道如何获取这个外部validation.php 文件来识别WordPress 功能(我认为),到目前为止我发现的任何东西都不起作用.

If I take out the WordPress username_exists function, and just echo back a string, it works fine. But with username_exists, it creates a 500 internal server error. I need to know how to get this external validation.php file to recongnise WordPress functions (I think), and nothing I've found so far will work.

感谢您的阅读...抱歉我的解释太长了,我只是想确保上下文都在那里,所以它是有道理的(我希望!)!

Thanks for reading... sorry for the long explanation I just wanted to make sure the context was all there so it made sense (I hope!)!

干杯,马特

推荐答案

我在 Wordpress Answers 上问了同样的问题 - https://wordpress.stackexchange.com/questions/20915/wordpress-plugin-admin-page-using-wordpress-function-in-linked-php文件

I asked the same question over at Wordpress Answers - https://wordpress.stackexchange.com/questions/20915/wordpress-plugin-admin-page-using-wordpress-function-in-linked-php-file

我认为最好简单地链接到对我有帮助的答案 =) 谢谢大家.

I thought it best to simply link to the answer that helped me =) Thanks all.

这篇关于WordPress 插件管理页面 - 在链接的 php 文件中使用 WordPress 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆