WordPress plugin admin page - using WordPress function in linked php file
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.
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.
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' );
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
});
});
});
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.
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!)!
Cheers, Matt
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.
You are missing a closing brace in your validateUserName
function:
function validateUserName($username) {
if ( username_exists($username) ) {
echo $username.' does exist';
} else {
echo $username.' doesn\'t exist';
}
}
should work.
As a function, however, it's probably better to return a value, and echo the result yourself:
function validateUserName($username) {
if ( username_exists($username) ) {
return $username.' does exist';
} else {
return $username.' doesn\'t exist';
}
}
echo validateUserName($someUsername);
精彩评论