dsfliu1129 2015-10-28 15:03
浏览 55

Wordpress通过javascript添加用户

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' , 'me@mail.com'));
   $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!

  • 写回答

1条回答 默认 最新

  • dongrang2140 2015-10-28 15:38
    关注

    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' , 'me@mail.com'));
      $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

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料