{"id":3003,"date":"2021-11-18T21:53:04","date_gmt":"2021-11-18T21:53:04","guid":{"rendered":"https:\/\/tschaki.com\/?p=3003"},"modified":"2021-11-18T21:55:09","modified_gmt":"2021-11-18T21:55:09","slug":"extend-wordpress-rest-api","status":"publish","type":"post","link":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/","title":{"rendered":"Extend WordPress REST API"},"content":{"rendered":"<p>Every WordPress website has an API interface by default with various endpoint. This is dynamically extensible via, for example, a plugin or theme. With each newly added endpoint, a function can be executed and a value can be output.<\/p>\n\n\n\n<h2 id=\"h-what-wordpress-rest-api-is-for\">What WordPress REST API is for<\/h2>\n\n\n\n<p>The already existing REST API is ideally suited to read out custom post types, for example, with all the posts contained therein. Filtering options are also available via URL.<\/p>\n\n\n\n<p>To get to the API interface overview, the URL call looks like this: <a href=\"https:\/\/tschaki.com\/wp-json\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/tschaki.com\/wp-json\/<\/a><\/p>\n\n\n\n<p>In this post we will cover the dynamic extension of the interface with <code>POST<\/code> and <code>GET<\/code> requests.<\/p>\n\n\n\n<h2>Add new API endpoint for POST<\/h2>\n\n\n\n<p>If a new endpoint is defined via <code>POST<\/code>, it cannot be retrieved as, for example, <code>GET<\/code>. This would result in a 404 error. <code>POST<\/code> is a great way to pass user data to the API in a hidden way. Callback is best suited to place additional functions. For example, when used for database entries.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">\/\/ Register new WP REST API Endpoint.\nadd_action( 'rest_api_init', function ( ) {\n    register_rest_route( 'namespace', 'apicall', array(\n        'methods' =&gt; 'POST',\n        'callback' =&gt; 'api_call'\n    ) );\n} );\n\n\/\/ Callback for new API Endpoint Call.\nfunction api_call( WP_REST_Request $request ) {\n    $request_params = $request-&gt;get_params( );\n  \treturn $request_params['param']; \/\/ returns &quot;sended_param&quot; on success.\n}<\/pre><\/div>\n\n\n\n<p>Passed <code>POST<\/code> variables by jQuery Ajax are to be read as parameters in the callback. These are created in an object.<\/p>\n\n\n\n<p>In this example Javascript jQuery snippet, the new WordPress API endpoint can be called via an Ajax request.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">jQuery.ajax({\n\tmethod: &quot;POST&quot;,\n\ttimeout: 5000,\n\tasync: true,\n\turl: &quot;\/wp-json\/namespace\/apicall&quot;,\n\tdata: {param: 'sended_param'},\n\terror: function() {\n\t\tconsole.log( &quot;failed&quot; );\n\t}\n}).done(function ( msg ) {\n  \t\/\/ Maybe markup manipulation.\n});<\/pre><\/div>\n\n\n\n<h2>Add new endpoint for GET<\/h2>\n\n\n\n<p>As a <code>GET<\/code> endpoint, the only thing that falls away is the ability to attach a jQuery <code>data: {}<\/code> object to be appended.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">\/\/ Register new WP REST API Endpoint.\nadd_action( 'rest_api_init', function ( ) {\n    register_rest_route( 'namespace', 'apicall', array(\n        'methods' =&gt; 'GET',\n        'callback' =&gt; 'api_call'\n    ) );\n} );\n\n\/\/ Callback for new API Endpoint Call.\nfunction api_call( ) {\n  \treturn true; \/\/ returns &quot;true&quot; on success.\n}<\/pre><\/div>\n\n\n\n<p>Also in this case the WordPress REST API endpoint must be called as <code>GET<\/code>. As <code>POST<\/code> this results in code 404.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">jQuery.ajax({\n\tmethod: &quot;GET&quot;,\n\ttimeout: 5000,\n\tasync: true,\n\turl: &quot;\/wp-json\/namespace\/apicall&quot;,\n\terror: function() {\n\t\tconsole.log( &quot;failed&quot; );\n\t}\n}).done(function ( msg ) {\n  \t\/\/ Maybe markup manipulation.\n});<\/pre><\/div>\n\n\n\n<p>Source: <a href=\"https:\/\/developer.wordpress.org\/rest-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress Codex<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Every WordPress website has an API interface by default with various endpoint. This is dynamically extensible via, for example, a plugin or theme. With each newly added endpoint, a function can be executed and a value can be output. What WordPress REST API is for The already existing REST API is ideally suited to read [&hellip;]<\/p>","protected":false},"author":1,"featured_media":2398,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[124,125],"tags":[131],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v16.6 (Yoast SEO v17.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Extend WordPress REST API &bull; tschaki<\/title>\n<meta name=\"description\" content=\"Every WordPress website has an REST API interface by default. This is dynamically extensible via plugin or theme.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Extend WordPress REST API\" \/>\n<meta property=\"og:description\" content=\"Every WordPress website has an REST API interface by default. This is dynamically extensible via plugin or theme.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/\" \/>\n<meta property=\"og:site_name\" content=\"tschaki\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-18T21:53:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-18T21:55:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1580\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Trent Bojett\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/tschaki.com\/#organization\",\"name\":\"Tschaki.com\",\"url\":\"https:\/\/tschaki.com\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/tschaki.com\/#logo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png\",\"contentUrl\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png\",\"width\":1022,\"height\":273,\"caption\":\"Tschaki.com\"},\"image\":{\"@id\":\"https:\/\/tschaki.com\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tschaki.com\/#website\",\"url\":\"https:\/\/tschaki.com\/\",\"name\":\"tschaki\",\"description\":\"Codesupport and alternatives\",\"publisher\":{\"@id\":\"https:\/\/tschaki.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tschaki.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\",\"contentUrl\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\",\"width\":1580,\"height\":500,\"caption\":\"create wordpress plugin\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#webpage\",\"url\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/\",\"name\":\"Extend WordPress REST API &bull; tschaki\",\"isPartOf\":{\"@id\":\"https:\/\/tschaki.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#primaryimage\"},\"datePublished\":\"2021-11-18T21:53:04+00:00\",\"dateModified\":\"2021-11-18T21:55:09+00:00\",\"description\":\"Every WordPress website has an REST API interface by default. This is dynamically extensible via plugin or theme.\",\"breadcrumb\":{\"@id\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tschaki.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Extend WordPress REST API\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#webpage\"},\"author\":{\"@id\":\"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219\"},\"headline\":\"Extend WordPress REST API\",\"datePublished\":\"2021-11-18T21:53:04+00:00\",\"dateModified\":\"2021-11-18T21:55:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#webpage\"},\"wordCount\":250,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/tschaki.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg\",\"keywords\":[\"wordpress\"],\"articleSection\":[\"Technology\",\"Wordpress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219\",\"name\":\"Trent Bojett\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/tschaki.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g\",\"caption\":\"Trent Bojett\"},\"description\":\"My name is Trent Bojett, I am 28 years old and I live in Switzerland. Because of my enthusiasm for writing and the logical world (= software development) I started this blog in early 2020.\",\"sameAs\":[\"https:\/\/tschaki.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Extend WordPress REST API &bull; tschaki","description":"Every WordPress website has an REST API interface by default. This is dynamically extensible via plugin or theme.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/","og_locale":"en_US","og_type":"article","og_title":"Extend WordPress REST API","og_description":"Every WordPress website has an REST API interface by default. This is dynamically extensible via plugin or theme.","og_url":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/","og_site_name":"tschaki","article_published_time":"2021-11-18T21:53:04+00:00","article_modified_time":"2021-11-18T21:55:09+00:00","og_image":[{"width":1580,"height":500,"url":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Trent Bojett","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/tschaki.com\/#organization","name":"Tschaki.com","url":"https:\/\/tschaki.com\/","sameAs":[],"logo":{"@type":"ImageObject","@id":"https:\/\/tschaki.com\/#logo","inLanguage":"en-US","url":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png","contentUrl":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/12\/tschaki-logo-1.png","width":1022,"height":273,"caption":"Tschaki.com"},"image":{"@id":"https:\/\/tschaki.com\/#logo"}},{"@type":"WebSite","@id":"https:\/\/tschaki.com\/#website","url":"https:\/\/tschaki.com\/","name":"tschaki","description":"Codesupport and alternatives","publisher":{"@id":"https:\/\/tschaki.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tschaki.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#primaryimage","inLanguage":"en-US","url":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","contentUrl":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","width":1580,"height":500,"caption":"create wordpress plugin"},{"@type":"WebPage","@id":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#webpage","url":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/","name":"Extend WordPress REST API &bull; tschaki","isPartOf":{"@id":"https:\/\/tschaki.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#primaryimage"},"datePublished":"2021-11-18T21:53:04+00:00","dateModified":"2021-11-18T21:55:09+00:00","description":"Every WordPress website has an REST API interface by default. This is dynamically extensible via plugin or theme.","breadcrumb":{"@id":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tschaki.com\/en\/"},{"@type":"ListItem","position":2,"name":"Extend WordPress REST API"}]},{"@type":"Article","@id":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#article","isPartOf":{"@id":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#webpage"},"author":{"@id":"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219"},"headline":"Extend WordPress REST API","datePublished":"2021-11-18T21:53:04+00:00","dateModified":"2021-11-18T21:55:09+00:00","mainEntityOfPage":{"@id":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#webpage"},"wordCount":250,"commentCount":0,"publisher":{"@id":"https:\/\/tschaki.com\/#organization"},"image":{"@id":"https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#primaryimage"},"thumbnailUrl":"https:\/\/tschaki.com\/wp-content\/uploads\/2020\/11\/WP-WooCommerce.jpg","keywords":["wordpress"],"articleSection":["Technology","Wordpress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/tschaki.com\/en\/extend-wordpress-rest-api\/#respond"]}]},{"@type":"Person","@id":"https:\/\/tschaki.com\/#\/schema\/person\/b4bf4286ba196fcd756146568acb3219","name":"Trent Bojett","image":{"@type":"ImageObject","@id":"https:\/\/tschaki.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7344f00aff4a7391a8f499ff58fc7275?s=96&d=mm&r=g","caption":"Trent Bojett"},"description":"My name is Trent Bojett, I am 28 years old and I live in Switzerland. Because of my enthusiasm for writing and the logical world (= software development) I started this blog in early 2020.","sameAs":["https:\/\/tschaki.com"]}]}},"_links":{"self":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts\/3003"}],"collection":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/comments?post=3003"}],"version-history":[{"count":2,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts\/3003\/revisions"}],"predecessor-version":[{"id":3007,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/posts\/3003\/revisions\/3007"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/media\/2398"}],"wp:attachment":[{"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/media?parent=3003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/categories?post=3003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tschaki.com\/en\/wp-json\/wp\/v2\/tags?post=3003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}