1

I have a wordpress website en would like to create users with a button (to start with)

It works in pieces, but i can't get this two pieces to work together

i have this piece of code (works on functions.php , but not in my createaccount.php file)

 $userid = new WP_User(wp_create_user( 'My_new_name' , '123458' , '[email protected]'));
   $userid->set_role('client');  //custom role 'client' already set

this on jquery //php file works when echo 'test';

$(document).ready( function() {

   $('#newbtnaccount').click( function() {

      $.post('php/createaccount.php', { } , 
                      function(data) {

                         alert(data);

    });

  });
});  

i already tried a lot of options but nothings seems to work yet.

Anyone who can Help? Thanks!

6
  • Can you include the error message in your question when you run the createaccount.php? Commented Oct 28, 2015 at 15:06
  • when i add the php i get this message: Failed to load resource: the server responded with a status of 500 (Internal Server Error) when i only echo something it works when i add this php to functions.php this code works, but i would like to trigger this function and add attribute later Commented Oct 28, 2015 at 15:12
  • So did you look at the error log on your server to see what is happening? Commented Oct 28, 2015 at 15:15
  • Have you loaded WordPress in createaccount.php? WordPress functions won't be available to you unless you explicitly load WordPress in a custom file like this. Commented Oct 28, 2015 at 15:19
  • i tried using global $wpdb; AND include("../../../wp-load.php"); Commented Oct 28, 2015 at 15:41

1 Answer 1

2

In wordpress you can make an AJAX request to admin-ajax.php and attach functions in your functions.php file with wp_ajax_my_action (for logged users) and wp_ajax_nopriv_my_action (for non logged users).

1. Set the admin-ajax.php url available as JS variable

In header.php add this in the head part:

<script type="text/javascript">
var ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>

2. Request the function through ajax

You need to add an action parameter to your request reflecting the function that you need to call in functions.php - let's call it create_user.

$.post(ajax_url, {action: 'create_user'} , function(data) {
  alert(data);
});

3. Create the function in functions.php

Inside functions.php, add the following:

function ajax_create_user() {
  $userid = new WP_User(wp_create_user( 'My_new_name' , '123458' , '[email protected]'));
  $userid->set_role('client');
  // echo whatever you need to return
}
add_action( 'wp_ajax_create_user', 'ajax_create_user' );
add_action( 'wp_ajax_nopriv_create_user', 'ajax_create_user' );

Read more about AJAX in Wordpress

Sign up to request clarification or add additional context in comments.

3 Comments

Great answer. But isn't step #1 unnecessary? Why not just set the AJAX URI in the $.post() call?
@rnevius In most cases it is unnecessary and can be safely replaced by /wp-admin/admin-ajax.php - it's just a bulletproof solution in case of specific installations (admin folder being renamed, prefix for the WP installation).
I agree with you. I was just referring to keeping things all in one place, rather than defining a variable in the head and then making the AJAX call elsewhere. I would rather use $.post('<?php echo admin_url('admin-ajax.php'); ?>', {action: 'create_user'} , function(data) { ..., but either works, of course :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.