diff --git a/seo-to-api.php b/seo-to-api.php index f834ea0..d9b0a41 100644 --- a/seo-to-api.php +++ b/seo-to-api.php @@ -19,43 +19,45 @@ 'show_in_rest' => true, ]; - $args = [ - 'name' => $_REQUEST['slug'], - 'post_type' => ['post', 'page', 'work'], - 'post_status' => 'publish', - 'numberposts' => 1 - ]; + if (isset($_REQUEST['slug'])) { + $args = [ + 'name' => $_REQUEST['slug'], + 'post_type' => ['post', 'page', 'work'], + 'post_status' => 'publish', + 'numberposts' => 1 + ]; - $post = get_posts($args)[0]; + $post = get_posts($args)[0]; - register_rest_field(['post', 'page', 'work'], 'meta_title', [ - 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_title($post->ID)); } - ]); + register_rest_field(['post', 'page', 'work'], 'meta_title', [ + 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_title($post->ID)); } + ]); - register_rest_field(['post', 'page', 'work'], 'meta_description', [ - 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_description($post->ID)); } - ]); + register_rest_field(['post', 'page', 'work'], 'meta_description', [ + 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_description($post->ID)); } + ]); - register_rest_field(['post', 'page', 'work'], 'meta_social_image_url', [ - 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_image_from_cache($post->ID)); } - ]); + register_rest_field(['post', 'page', 'work'], 'meta_social_image_url', [ + 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_social_image( ['post_id' => $post->ID,])); } + ]); - register_rest_field(['post', 'page', 'work'], 'meta_open_graph_title', [ - 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_open_graph_title($post->ID)); } - ]); + register_rest_field(['post', 'page', 'work'], 'meta_open_graph_title', [ + 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_open_graph_title($post->ID)); } + ]); - register_rest_field(['post', 'page', 'work'], 'meta_open_graph_description', [ - 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_open_graph_description($post->ID)); } - ]); + register_rest_field(['post', 'page', 'work'], 'meta_open_graph_description', [ + 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_open_graph_description($post->ID)); } + ]); - register_rest_field(['post', 'page', 'work'], 'meta_twitter_title', [ - 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_twitter_title($post->ID)); } - ]); + register_rest_field(['post', 'page', 'work'], 'meta_twitter_title', [ + 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_twitter_title($post->ID)); } + ]); - register_rest_field(['post', 'page', 'work'], 'meta_twitter_description', [ - 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_twitter_description($post->ID)); } - ]); + register_rest_field(['post', 'page', 'work'], 'meta_twitter_description', [ + 'get_callback' => function () use ($post) { return html_entity_decode(the_seo_framework()->get_twitter_description($post->ID)); } + ]); + } }); \ No newline at end of file