2016-10-21 76 views
1

這是我第一次使用wordpress創建應用程序。在那裏我想做一個api,但不知道如何在wordpress中製作。如何在Wordpress中製作API

這是我的代碼的網站:

function drivers_post_type() { 
    $labels = array(
     'name'    => _x('driver', 'Post Type General Name', 'text_domain'), 
     'singular_name'  => _x('driver', 'Post Type Singular Name', 'text_domain'), 
     'menu_name'   => __('driver', 'text_domain'), 
     'name_admin_bar'  => __('Post Type', 'text_domain'), 
     'parent_item_colon' => __('Parent Item:', 'text_domain'), 
     'all_items'   => __('All Items', 'text_domain'), 
     'add_new_item'  => __('Add New Item', 'text_domain'), 
     'add_new'    => __('Add New', 'text_domain'), 
     'new_item'   => __('New Item', 'text_domain'), 
     'edit_item'   => __('Edit Item', 'text_domain'), 
     'update_item'   => __('Update Item', 'text_domain'), 
     'view_item'   => __('View Item', 'text_domain'), 
     'search_items'  => __('Search Item', 'text_domain'), 
     'not_found'   => __('Not found', 'text_domain'), 
     'not_found_in_trash' => __('Not found in Trash', 'text_domain'), 
    ); 
    $args = array(
     'label'    => __('driver', 'text_domain'), 
     'description'   => __('driver', 'text_domain'), 
     'labels'    => $labels, 
     'supports'   => array('title','thumbnail'), 
     'hierarchical'  => false, 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_menu'  => true, 
     'menu_position'  => 5, 
     'show_in_admin_bar' => true, 
     'show_in_nav_menus' => true, 
     'can_export'   => true, 
     'has_archive'   => true, 
     'exclude_from_search' => false, 
     'publicly_queryable' => true, 
     'rewrite'    => array('slug' => 'driver'), 
     'capability_type'  => 'page', 
    ); 
    register_post_type('drivers', $args); 
} 
// Hook into the 'init' action 
add_action('init', 'drivers_post_type', 0); 

// Little function to return a custom field value 
function driverMB_get_custom_field($value) { 
    global $post; 

    $custom_field = get_post_meta($post->ID, $value, true); 
    if (!empty($custom_field)) 
     return is_array($custom_field) ? stripslashes_deep($custom_field) : stripslashes(wp_kses_decode_entities($custom_field)); 

    return false; 
} 

// Register the Metabox 
function driverMB_add_custom_meta_box() { 
    add_meta_box( 
     'driverMB-meta-box', 
     __('driver Info', 'textdomain'), 
     'driverMB_meta_box_output', 
     'drivers', 
     'normal', 
     'default' 
    ); 
} 
add_action('add_meta_boxes', 'driverMB_add_custom_meta_box'); 

// Output the Metabox 
function driverMB_meta_box_output($post) { 
    // create a nonce field 
    wp_nonce_field('my_driverMB_meta_box_nonce', 'driverMB_meta_box_nonce'); ?> 

    <p> 
     <label><b>ID</b></label> 
     <label><?php echo get_the_ID() ?></label> 
    </p> 
    <p> 
     <label><b>Username</b></label> 
     <input type="text" placeholder="Username" name="username" id="username" value="<?php echo driverMB_get_custom_field('username'); ?>" style="width:100%;" /> 
    </p> 
    <p> 
     <label><b>Password</b></label> 
     <input type="password" placeholder="Password" name="password" id="password" value="<?php echo driverMB_get_custom_field('password'); ?>" style="width:100%;" /> 
    </p> 
    <p> 
     <label><b>Email</b></label> 
     <input type="text" placeholder="Email" name="email" id="email" value="<?php echo driverMB_get_custom_field('email'); ?>" style="width:100%;" /> 
    </p> 
    <p> 
     <label><b>Phone Number</b></label> 
     <input type="text" placeholder="Ext : 088216192560" name="phone" id="phone" value="<?php echo driverMB_get_custom_field('phone'); ?>" style="width:100%;" /> 
    </p> 

    <?php 
} 

// Save the Metabox values 
function driverMB_meta_box_save($post_id) { 
    // Stop the script when doing autosave 
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; 

    // Verify the nonce. If insn't there, stop the script 
    if(!isset($_POST['driverMB_meta_box_nonce']) || !wp_verify_nonce($_POST['driverMB_meta_box_nonce'], 'my_driverMB_meta_box_nonce')) return; 

    // Stop the script if the user does not have edit permissions 
    if(!current_user_can('edit_post')) return; 

    // Save the textfield 
    if(isset($_POST['username'])) 
    update_post_meta($post_id, 'username', esc_attr($_POST['username'])); 
    if(isset($_POST['password'])) 
    update_post_meta($post_id, 'password', esc_attr($_POST['password'])); 
    if(isset($_POST['email'])) 
    update_post_meta($post_id, 'email', esc_attr($_POST['email'])); 
    if(isset($_POST['phone'])) 
    update_post_meta($post_id, 'phone', esc_attr($_POST['phone'])); 
} 
add_action('save_post', 'driverMB_meta_box_save'); 

我不知道如何將其創建爲API。我的朋友告訴我創建如果URL等於然後執行動作功能,但我不知道如何?

有人告訴我或幫我創建一個WordPress的api?

回答

2

在這裏,你可以閱讀如何嵌入自己的函數使用WordPress
http://v2.wp-api.org/extending/adding/

這裏闡述瞭如何構建自己的REST API
https://wordpress.stackexchange.com/questions/162864/building-a-custom-rest-api

這裏如何創建自己的API端點
http://coderrr.com/create-an-api-endpoint-in-wordpress/

+0

您給出真正幫助的解釋,但因爲我只是讓我更加困惑,或者有沒有一種方法不使用任何插件,因爲我仍然困惑我應該在哪裏放這些腳本? 或U可以給我只是簡單的API,其中fnction其輸入(createting數據) - 在它可以將數據我在TABEL驅動器(上圖) 感謝 –

+0

你必須使用插件inputed>,造成WordPress是基於插件的對於api的 – Blueblazer172

+0

哦,哦。所以我需要安裝WP休息或任何其他插件,我需要安裝?然後我需要創建/放置代碼(我的意思是文件夾)我應該使用? –

4

毫無疑問,市場上還有很多其他插件,但這些是我個人用來在WordPress中創建API的兩個插件:

  1. 使用WP Rest API(現在有一天它被WordPress官方支持): 提供了相同的詳細文檔here

  2. 使用Json API插件(爲簡單起見)。如果你使用這個插件,那麼它將允許你創建你的「控制器」,「模型」等文件,因此你可以編寫你的自定義端點。您可以查看更多詳情here

注:我去年使用過這個插件,它的效果很好。