add_action('rest_api_init', function () { register_rest_route('seo-monitoring/v1', '/keywords', array( 'methods' => 'GET', 'callback' => 'sm_get_keywords', 'permission_callback' => function() { return current_user_can('manage_options'); // Только админы } )); register_rest_route('seo-monitoring/v1', '/keywords', array( 'methods' => 'POST', 'callback' => 'sm_add_keyword', 'permission_callback' => function() { return current_user_can('manage_options'); } )); register_rest_route('seo-monitoring/v1', '/keywords/(?P\d+)', array( 'methods' => 'DELETE', 'callback' => 'sm_delete_keyword', 'permission_callback' => function() { return current_user_can('manage_options'); } )); }); // Получаем список ключевых слов function sm_get_keywords() { global $wpdb; $table = $wpdb->prefix . 'sm_keywords'; $results = $wpdb->get_results("SELECT * FROM $table ORDER BY created_at DESC"); return rest_ensure_response($results); } // Добавляем ключевое слово function sm_add_keyword(WP_REST_Request $request) { global $wpdb; $table = $wpdb->prefix . 'sm_keywords'; $keyword = sanitize_text_field($request->get_param('keyword')); $region = sanitize_text_field($request->get_param('region')); $user_id = get_current_user_id(); if (empty($keyword)) { return new WP_Error('empty_keyword', 'Ключевое слово не может быть пустым', array('status' => 400)); } $wpdb->insert($table, array( 'user_id' => $user_id, 'keyword' => $keyword, 'region' => $region, 'created_at' => current_time('mysql', 1) )); return rest_ensure_response(array('message' => 'Ключевое слово добавлено')); } // Удаляем ключевое слово function sm_delete_keyword(WP_REST_Request $request) { global $wpdb; $table = $wpdb->prefix . 'sm_keywords'; $id = (int) $request->get_param('id'); $wpdb->delete($table, array('id' => $id)); return rest_ensure_response(array('message' => 'Ключевое слово удалено')); } admin — My CMS

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

Опубликовано
В рубрике Uncategorized