{"id":148217,"date":"2021-10-08T11:06:04","date_gmt":"2021-10-08T11:06:04","guid":{"rendered":"https:\/\/wordpress.org\/plugins\/headless-plugin\/"},"modified":"2022-01-28T19:15:27","modified_gmt":"2022-01-28T19:15:27","slug":"headless-converter","status":"publish","type":"plugin","link":"https:\/\/dv.wordpress.org\/plugins\/headless-converter\/","author":17460990,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_crdt_document":"","version":"1.0.6","stable_tag":"1.0.6","tested":"5.9.13","requires":"5.6","requires_php":"7.4","requires_plugins":null,"header_name":"Headless Converter","header_author":"Atte Liimatainen","header_description":"Converts frontend to JSON response when request is done with certain conditions.","assets_banners_color":"","last_updated":"2022-01-28 19:15:27","external_support_url":"","external_repository_url":"","donate_link":"","header_plugin_uri":"https:\/\/github.com\/AttLii\/headless-converter","header_author_uri":"https:\/\/github.com\/AttLii","rating":0,"author_block_rating":0,"active_installs":0,"downloads":2697,"num_ratings":0,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["faq","changelog","description"],"tags":{"1.0.0":{"tag":"1.0.0","author":"attlii","date":"2021-10-08 11:06:02"},"1.0.1":{"tag":"1.0.1","author":"attlii","date":"2021-10-08 11:23:22"},"1.0.2":{"tag":"1.0.2","author":"attlii","date":"2021-10-20 11:39:59"},"1.0.3":{"tag":"1.0.3","author":"attlii","date":"2021-11-15 12:52:49"},"1.0.4":{"tag":"1.0.4","author":"attlii","date":"2021-11-15 14:15:33"},"1.0.5":{"tag":"1.0.5","author":"attlii","date":"2021-11-16 14:23:57"},"1.0.6":{"tag":"1.0.6","author":"attlii","date":"2022-01-28 19:15:27"},"1.0.7":{"tag":"1.0.7","author":"attlii","date":"2022-03-01 19:12:16"},"1.0.8":{"tag":"1.0.8","author":"attlii","date":"2023-07-26 07:47:30"}},"upgrade_notice":[],"ratings":[],"assets_icons":[],"assets_banners":[],"assets_blueprints":{},"all_blocks":[],"tagged_versions":["1.0.0","1.0.1","1.0.2","1.0.3","1.0.4","1.0.5","1.0.6","1.0.7","1.0.8"],"block_files":[],"assets_screenshots":[],"screenshots":[],"jetpack_post_was_ever_published":false},"plugin_section":[],"plugin_tags":[5520,141196,1118],"plugin_category":[59],"plugin_contributors":[177030],"plugin_business_model":[],"class_list":["post-148217","plugin","type-plugin","status-publish","hentry","plugin_tags-converter","plugin_tags-headless","plugin_tags-json","plugin_category-utilities-and-tools","plugin_contributors-attlii","plugin_committers-attlii"],"banners":[],"icons":{"svg":false,"icon":"https:\/\/s.w.org\/plugins\/geopattern-icon\/headless-converter.svg","icon_2x":false,"generated":true},"screenshots":[],"raw_content":"<!--section=faq-->\n<dl>\n<dt id='application%20password%20doesn%27t%20show%20up%2C%20even%20though%20i%20use%20wordpress%20version%205.6%20or%20newer.%20what%20do%3F'><h3>Application Password doesn't show up, even though I use Wordpress version 5.6 or newer. What do?<\/h3><\/dt>\n<dd><p>This seems to be a feature that is opt-in on some environments. Following hook enables it.<\/p>\n\n<pre><code>add_filter( 'wp_is_application_passwords_available' , '__return_true' );\n<\/code><\/pre><\/dd>\n\n<\/dl>\n\n<!--section=changelog-->\n<h4>1.0.0 (2020-10-08):<\/h4>\n\n<ul>\n<li>Initial release<\/li>\n<\/ul>\n\n<h4>1.0.1 (2020-10-08):<\/h4>\n\n<ul>\n<li>Remove unnecessary stuff from the plugin folder<\/li>\n<\/ul>\n\n<h4>1.0.2 (2020-10-20):<\/h4>\n\n<ul>\n<li>Add FAQ about enabling application passwords<\/li>\n<\/ul>\n\n<h4>1.0.3 (2020-11-15):<\/h4>\n\n<ul>\n<li>Update and prune repository dependencies<\/li>\n<\/ul>\n\n<h4>1.0.4 (2020-11-15):<\/h4>\n\n<ul>\n<li>Run repository through a linter<\/li>\n<\/ul>\n\n<h4>1.0.5 (2020-11-16):<\/h4>\n\n<ul>\n<li>Fix documentation<\/li>\n<\/ul>\n\n<h4>1.0.6 (2021-01-28):<\/h4>\n\n<ul>\n<li>update tested up to version to 5.9<\/li>\n<\/ul>\n\n<!--section=description-->\n<p>Converts frontend to JSON response when request is done with certain conditions.<\/p>\n\n<h3>Inspiration<\/h3>\n\n<p>After creating a bunch of headless Wordpress + Web app stacks, I wanted to find a standard and a all-round solution for fetching per page information for web applications inside Wordpress. Built-in rest api works fine in basic cases, but it doesn't support querying by path. Usually this meant that for each project developers would create a custom rest endpoint which would return expected content using content type and slug parameters.<\/p>\n\n<p>Wordpress has few built-in functions to retrieve content by path, <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/url_to_postid\/\">url_to_postid<\/a> and <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_page_by_path\/\">get_page_by_path<\/a>, but they don't seem to work with multilanguage plugins, taxonomy or archive pages, which means that WP doesn't have a reliable way to fetch content this way through rest api.<\/p>\n\n<p>This plugin converts frontend to JSON which seems after above solution the best way to do things, with added layer of security through application passwords (Wordpress v5.6 feature) and a filter, which let's developers alter outgoing data.<\/p>\n\n<h3>How to start using the plugin<\/h3>\n\n<ul>\n<li>Install and activate this plugin in your environment<\/li>\n<li>Create application password for a user with administrator role<\/li>\n<\/ul>\n\n<p>After above steps have been made, make a request to a page with added <a href=\"https:\/\/en.wikipedia.org\/wiki\/Basic_access_authentication#Client_side\">Authorization header<\/a>. See data fetching examples at the end of this documentation for more help<\/p>\n\n<h3>Modifying the output<\/h3>\n\n<p>Plugin outputs current page's Post object or null. This can be modified using <code>headless-converter-modify-data<\/code>-filter. You can either modify passed in post object or do your own logic like in the example below.<\/p>\n\n<pre><code>\/**\n * Modifies Headless Converter plugin's output.\n * \n * @param WPPost|null $post - Current template's post object \n *\/\nfunction modify_headless_converter_output($post) {\n  if(is_404()) {\n    return \"this block renders 404 page content\";\n  } else if(is_page()) {\n    return \"this block renders page post types content\";\n  } else if (is_singular('post')) {\n    return \"this block renders single post content\";\n  } else if(is_home()) {\n    return \"this block renders post archive\";\n  } else {\n    return $post;\n  }\n}\n\nadd_filter('headless-converter-modify-data', 'modify_headless_converter_output');\n<\/code><\/pre>\n\n<h3>Data fetching examples<\/h3>\n\n<h4>Fetch<\/h4>\n\n<pre><code>const username = \"admin\"\nconst password = \"1111 1111 1111 1111 1111\"\nconst url = \"http:\/\/localhost:3000\/?page_id=2\"\nconst opts = {\n  headers: {\n     'Authorization': 'Basic ' + btoa(username + \":\" + password) \n  }, \n}\nfetch(url, opts).then(r =&gt; r.json()).then(console.log)\n<\/code><\/pre>\n\n<h4>Axios<\/h4>\n\n<pre><code>const axios = require(\"axios\")\n\nconst username = \"admin\"\nconst password = \"1111 1111 1111 1111 1111\"\nconst url = \"http:\/\/localhost:3000\/?page_id=2\"\nconst opts = {\n  auth: {\n    username,\n    password\n  }\n}\n\naxios(url, opts).then(r =&gt; r.data).then(console.log)\n<\/code><\/pre>","raw_excerpt":"Converts frontend to JSON response when request is done with certain conditions.","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/148217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=148217"}],"author":[{"embeddable":true,"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/attlii"}],"wp:attachment":[{"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=148217"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=148217"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=148217"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=148217"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=148217"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/dv.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=148217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}