Home >CMS Tutorial >WordPress >Building a PhoneGap App with a WordPress Backend
This tutorial demonstrates using WordPress as a backend for a PhoneGap mobile application, focusing on creating REST APIs for seamless communication. We'll build a simple login and blog post display app. While PhoneGap itself is discontinued, the principles apply to its open-source successor, Apache Cordova.
Unlike limited blog-app solutions like AppPresser, this approach allows building diverse apps using WordPress's backend.
Key Concepts:
wp_ajax_
and wp_ajax_nopriv_
actions are crucial for creating GET/POST APIs.Building WordPress REST APIs:
WordPress provides actions for creating REST APIs accessible by any HTTP client. Let's build APIs for login and post retrieval.
Login API:
<code class="language-php">function already_logged_in() { echo "User is already Logged In"; die(); } function login() { $creds = array( 'user_login' => $_GET["username"], 'user_password' => $_GET["password"] ); $user = wp_signon($creds, false); if (is_wp_error($user)) { echo "FALSE"; die(); } echo "TRUE"; die(); } add_action("wp_ajax_login", "already_logged_in"); add_action("wp_ajax_nopriv_login", "login");</code>
This API handles login attempts. If a user is already logged in (wp_ajax_
), already_logged_in
is executed. Otherwise (wp_ajax_nopriv_
), login
verifies credentials using wp_signon
.
Blog Post API:
<code class="language-php">function posts() { header("Content-Type: application/json"); $posts_array = array(); $args = array( "post_type" => "post", "orderby" => "date", "order" => "DESC", "post_status" => "publish", "posts_per_page" => "10" ); $posts = new WP_Query($args); if ($posts->have_posts()) : while ($posts->have_posts()) : $posts->the_post(); $post_array = array(get_the_title(), get_the_permalink(), get_the_date(), wp_get_attachment_url(get_post_thumbnail_id())); array_push($posts_array, $post_array); endwhile; else : echo json_encode(array('posts' => array())); die(); endif; echo json_encode($posts_array); die(); } function no_posts() { echo "Please login"; die(); } add_action("wp_ajax_posts", "posts"); add_action("wp_ajax_nopriv_posts", "no_posts");</code>
This API returns the ten latest posts in JSON format. Unlogged users receive a login prompt.
Creating the PhoneGap App:
We'll use the PhoneGap Desktop Builder (or Apache Cordova equivalent). The app structure will be:
<code>--www --cordova.js --js --index.js --index.html --css --style.css (optional)</code>
index.html: (Simplified for brevity, uses jQuery Mobile)
<code class="language-html"><!DOCTYPE html> <meta charset="utf-8"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height"> <title>PhoneGap WordPress App</title> <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <!-- Login Page --> <div data-role="page" id="login">...</div> <!-- Posts Page --> <div data-role="page" id="posts">...</div> </code>
index.js: (Simplified for brevity)
<code class="language-javascript">function login() { // ... (Login logic using XMLHttpRequest, similar to the original example) ... } function fetchAndDisplayPosts() { // ... (Fetch and display posts using XMLHttpRequest, similar to the original example) ... }</code>
Remember to replace "http://localhost/wp-admin/admin-ajax.php"
with your WordPress site's URL.
Further Resources and FAQs: (The original FAQs are still relevant and can be included here, potentially rephrased for clarity and updated to reflect the shift from PhoneGap to Apache Cordova.)
This revised response provides a more concise and structured tutorial, maintaining the core functionality while addressing the obsolescence of PhoneGap and emphasizing the migration path to Apache Cordova. Remember to replace placeholder image URLs with actual image URLs.
The above is the detailed content of Building a PhoneGap App with a WordPress Backend. For more information, please follow other related articles on the PHP Chinese website!