Вот код, который я использую для создания конечной точки:
Код: Выделить всё
//-----------------------------------------------------------------------
// Callback function to fetch Smart Slider 3 slider content
//-----------------------------------------------------------------------
function get_smart_slider_data($data) {
// Retrieve the slider ID from the URL parameter
$slider_id = isset($data['id']) ? intval($data['id']) : 0;
// Check if the 'smart_slider3' function is available after plugins are loaded
if (!function_exists('smart_slider3')) {
return new WP_Error('no_slider_function', 'Smart Slider function not found', ['status' => 500]);
}
// Use the shortcode to retrieve the slider HTML based on the given ID
$slider_html = do_shortcode('[smartslider3 slider=' . $slider_id . ']');
// Return the HTML response or a 404 error if the content is empty
if (!empty($slider_html)) {
return rest_ensure_response(['html' => $slider_html]);
} else {
return new WP_Error('no_slider', 'Slider not found', ['status' => 404]);
}
}
//-----------------------------------------------------------------------
// Function to register the REST API endpoint
//-----------------------------------------------------------------------
function register_smart_slider_endpoint() {
register_rest_route('custom/v1', '/slider/(?P\d+)', [
'methods' => 'GET',
'callback' => 'get_smart_slider_data',
'permission_callback' => '__return_true',
]);
}
//-----------------------------------------------------------------------
// Hook to initialize the endpoint after plugins are loaded
//-----------------------------------------------------------------------
add_action('rest_api_init', 'register_smart_slider_endpoint');
Код: Выделить всё
{
"code": "no_slider_function",
"message": "Smart Slider function not found",
"data": {
"status": 500
}
}
Что я пробовал:
- Проверка загрузки функции smart_slider3() с помощью function_exists('smart_slider3').
- Обеспечение конечной точки зарегистрирован в рамках plugins_loaded для задержки его выполнения.
- Попытка вручную включить основной файл плагина, но это привело к ошибке:
Код: Выделить всё
Warning: Failed opening '.../wp-content/plugins/smart-slider-3/Nextend/SmartSlider3.php' for inclusion
- Как лучше всего предоставить слайдер из плагина Smart Slider 3 через REST API?
- Существует ли какой-то конкретный способ или подход, обеспечивающий доступность функций плагина при регистрации моя конечная точка?
- Я использую WordPress 6.6.2 с установленным и активным плагином Smart Slider 3.< /li>
Я разрабатываю собственную тему для headless-проекта, в котором мы используем данные WordPress через REST API.
Подробнее здесь: https://stackoverflow.com/questions/791 ... s-rest-api
Мобильная версия