简体   繁体   English

如何为 ZC31B32364CE19CA8ZFCD150A417ECCE5 应用程序创建 WordPress REST API

[英]How to create a WordPress REST API for an android application

I am basically android developer, I also work on PHP to create REST APIs for mobile apps.我基本上是 android 开发人员,我也在 PHP 上工作,为移动应用程序创建 REST API。 I am working on an android app, I want to create a WordPress-REST-API for my android app to add users into WordPress wp_user's table through the android app. I am working on an android app, I want to create a WordPress-REST-API for my android app to add users into WordPress wp_user's table through the android app.

For this, i created a REST API in PHP and used wp_create_user() method to add a user into WordPress database. For this, i created a REST API in PHP and used wp_create_user() method to add a user into WordPress database. I placed API into 'wp-content/themes/mytheme' directory but it's always showing me this error 'Call to undefined function wp_create_user()', i think wp_create_user() function is unavailable in my API. I placed API into 'wp-content/themes/mytheme' directory but it's always showing me this error 'Call to undefined function wp_create_user()', i think wp_create_user() function is unavailable in my API.

I asked from my WordPress friend and he said, you have to create a custom page template and have to call that page from android app but this is illogical fro me.我问我的 WordPress 朋友,他说,你必须创建一个自定义页面模板,并且必须从 android 应用程序调用该页面,但这对我来说是不合逻辑的。 How can I resolve this issue?我该如何解决这个问题?

<?php
if (!isset($_POST['ins_name'])) {
    die("ins_name  is not set");
}

$ins_name = $_POST['ins_name'];

if (!isset($_POST['ins_email'])) {
    die("ins_email is not set");
}

$ins_email = $_POST['ins_email'];

if (!isset($_POST['ins_password'])) {
    die("ins_password  is not set");
}

$ins_password = $_POST['ins_password'];


function addUserIntoWordpressTable($email, $password) {
      $user_id = wp_create_user( $username, $password, $email );
      echo $user_id;
}
addUserIntoWordpressTable($ins_name, $ins_password, $ins_email);

?>

I want to add users's data into WordPress database through REST API.我想通过REST API将用户的数据添加到WordPress数据库中。

Wordpress has it's own built in REST API framework where you can use all the existing functions, and extend it in a clean way without having to worry about REST boilerplate or doing ugly things like making a REST page template. Wordpress has it's own built in REST API framework where you can use all the existing functions, and extend it in a clean way without having to worry about REST boilerplate or doing ugly things like making a REST page template.

You can read more about it here: https://developer.wordpress.org/rest-api/你可以在这里阅读更多信息: https://developer.wordpress.org/rest-api/

For any WordPress functions to work in your custom folder or api folder you have to include the file wp-load.php in your file.对于要在您的自定义文件夹或 api 文件夹中工作的任何 WordPress 函数,您必须在文件中包含文件wp-load.php

Like:喜欢:

require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

You need to include this file at the top of your php file.您需要将此文件包含在 php 文件的顶部。

Hope it works for you.希望对你有效。

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

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