WordPress https://atendesigngroup.com/ en Three things the WordPress authoring experience gets right https://atendesigngroup.com/articles/three-things-wordpress-authoring-experience-gets-right <span>Three things the WordPress authoring experience gets right</span> <figure> <picture> <source srcset="/sites/default/files/2021-02/ATEN_AUTHORING_EXPERIENCE.jpg 1x" media="(min-width: 1860px)" type="image/jpeg"/> <source srcset="/sites/default/files/2021-02/ATEN_AUTHORING_EXPERIENCE.jpg 1x" media="(min-width: 1540px)" type="image/jpeg"/> <source srcset="/sites/default/files/styles/responsive_large/public/2021-02/ATEN_AUTHORING_EXPERIENCE.jpg?itok=abytzgCS 1x" media="(min-width: 1265px)" type="image/jpeg"/> <source srcset="/sites/default/files/styles/responsive_medium/public/2021-02/ATEN_AUTHORING_EXPERIENCE.jpg?itok=BuZenNBJ 1x" media="(min-width: 1024px)" type="image/jpeg"/> <source srcset="/sites/default/files/styles/responsive_medium/public/2021-02/ATEN_AUTHORING_EXPERIENCE.jpg?itok=BuZenNBJ 1x" media="(min-width: 768px)" type="image/jpeg"/> <source srcset="/sites/default/files/styles/responsive_medium/public/2021-02/ATEN_AUTHORING_EXPERIENCE.jpg?itok=BuZenNBJ 1x" media="(min-width: 600px)" type="image/jpeg"/> <source srcset="/sites/default/files/styles/responsive_small/public/2021-02/ATEN_AUTHORING_EXPERIENCE.jpg?itok=qj8-sf-t 1x" media="(min-width: 500px)" type="image/jpeg"/> <source srcset="/sites/default/files/styles/responsive_small/public/2021-02/ATEN_AUTHORING_EXPERIENCE.jpg?itok=qj8-sf-t 1x" media="(min-width: 0)" type="image/jpeg"/> <img src="/sites/default/files/2021-02/ATEN_AUTHORING_EXPERIENCE.jpg" alt="" typeof="foaf:Image" /> </picture> </figure> <span><a title="View user profile." href="/user/eric-toupin" lang="" about="/user/eric-toupin" typeof="schema:Person" property="schema:name" datatype="">Eric Toupin</a></span> <span>Thu, 02/04/2021 - 11:30</span> <a href="/blog/category/wordpress" class="tag" >WordPress</a> <a href="/articles/authoring-experience" class="tag" >Authoring Experience</a> <div class="field field--name-body field--type-text-with-summary field--label-hidden t--body field__item"><p>WordPress makes assumptions about its users just like any other digital platform, but in a lot of ways they’re the right assumptions. When I build WordPress sites for clients, I’m building websites for people who are content creators at heart. If you have a message, a story, or a brand you’re trying to engage users with, the WordPress authoring experience is quick to put you in the driver’s seat and let you construct multimedia web content — text, images, videos, slideshows, fancy pull-quotes or the like — right from the get-go.</p> <p>A lot of the time, users on the WordPress sites I build have no idea what’s going on “behind the scenes” in terms of HTML markup or code, and of course they don’t need to — and shouldn’t have to. Content creators aren’t programmers, and they shouldn’t have to blur the line by brushing up on HTML, diving into CSS, or even learning the nuances of techy-focused editors with buttons like <i>Source code</i> or style dropdown options like <i>Div</i>.</p> <p>Compared to other content management platforms or development frameworks, WordPress can be pretty constraining when it comes to application level functionality. Things like multi-user editorial workflows, creating complex content relationships, building multi-faceted searches, or producing data-driven visualizations aren’t (currently) the best fit for WordPress.</p> <p>That said, even the most robust, feature rich, data driven or task oriented web applications <i>still need to make a home for content creators</i>. Authoring content is a permanent feature of multimedia communications, and web applications need to treat it that way. Here are three things the WordPress authoring experience gets right — other platforms could take a cue:</p> <h3>Ready to go: Get started in a flash</h3> <p>WordPress lets you compose rich multimedia web content that’s ready for an audience right out of the box. Hosted solutions let non-technical do-it-yourselfers get a site up and running with a few clicks, and are accessible to virtually anyone with options ranging from budget friendly providers like <a href="https://www.bluehost.com/special/wordpress">BlueHost</a> through performance tuned premium vendors like <a href="https://pantheon.io/product/wordpress-hosting">Pantheon</a>. Developers can download the free <a href="https://wordpress.org/download/">WordPress source code</a>, build locally, and deploy to their existing domain. From blog enthusiasts to web editors to seasoned developers, WordPress gets you producing good looking content in minutes.</p> <h3>Easy to use: Zero to beautiful content in minutes</h3> <p>Once your WordPress site is up and running, creating quality multimedia content is very, very easy. A brand new WordPress site features a straightforward dashboard and clear menu items. One click from the very first screen of your fresh install (<i>Pages</i> &gt; <i>Add New</i>) and you’re launched into the slick WordPress Editor, complete with a little click-through tutorial and a <a href="https://wordpress.org/support/article/wordpress-editor/">link to the explicit getting started guide</a>.</p> <figure role="group" class="align-center"> <div alt="WordPress Editor" data-embed-button="media_browser" data-entity-embed-display="media_image" data-entity-embed-display-settings="responsive_large" data-entity-type="media" data-entity-uuid="8ea0d1ec-eb87-434c-96a7-7c8f1859872f" data-langcode="en" title="WordPress Editor" class="embedded-entity"> <img src="/sites/default/files/styles/responsive_large/public/2021-02/aten_wordpress_authoring_editor.png?itok=QDxZg5nT" alt="WordPress Editor" title="WordPress Editor" typeof="foaf:Image" /> </div> <figcaption>With my local web server ready-to-go, the WordPress install — from download to adding my first page — <em>takes less than three minutes </em>at an easygoing pace.</figcaption> </figure> <p>The WordPress Editor hardly needs explaining. Click to type, click to add new elements, and enjoy 50+ totally ready-to-go text types, media blocks, design elements, widgets, embeds, etc. From deciding to install WordPress to publishing my first page took less than eight minutes. And my WordPress Sandbox page features a quote, pull-quote, link, headers, a stylized image and an embedded YouTube video.</p> <div class="image-right"> <div alt="WordPress content example" data-embed-button="media_browser" data-entity-embed-display="media_image" data-entity-embed-display-settings="responsive_small" data-entity-type="media" data-entity-uuid="910aeacf-de1a-4a80-b927-7646eb72eed7" data-langcode="en" title="WordPress content example" class="align-right embedded-entity"> <img src="/sites/default/files/styles/responsive_small/public/2021-02/aten_wordpress_authoring_example.png?itok=-at7s7Hq" alt="WordPress content example" title="WordPress content example" typeof="foaf:Image" /> </div> </div> <p>For my <em>WordPress Sandbox</em> example I made zero configuration changes, zero template customizations, and no style changes whatsoever — although tons of those options are just as intuitive as the WordPress Editor itself. I may as well never have heard of HTML or CSS.</p> <h3>Community support: Tons of resources for content authors</h3> <p>Content creators using WordPress are well supported. Whether you’re looking to choose a theme for your website, understand how to arrange content on a page, make the most of the WordPress Editor experience, or take advantage of some of the more than 57K WordPress plugins, how-to guides are available at every turn. <a href="https://learn.wordpress.org/">Learn WordPress</a> on WordPress.org is a great resource for workshops, courses and lesson plans, and tons of 3rd party content creators like <a href="https://www.wpbeginner.com/">WPBeginner.com</a> provide a host of resources as well. Tons of resources are focused on what authors care about most: creating beautiful content.</p> <p>Premium theme providers often deliver top-notch support, too. While I was working with The Colorado Human Trafficking Council against a tight deadline to publish <a href="https://thisishumantrafficking.com/">This is human trafficking</a>, I ran into a problem while trying to override the premium theme <a href="https://themeforest.net/item/avada-responsive-multipurpose-theme/2833226">Avada</a> we were using as a base theme. The theme’s support team answered my question and had me on my way in less than fifteen minutes.</p> <hr /> <p>Many organizations are driven into the arms of more robust web application frameworks and content management systems by their needs, and for good reason. Lots of technical requirements point a finger <i>away from WordPress</i> and towards other solutions: faceted content searches, multi-role editorial workflows, API integrations, granular user permissions, complex notification systems, data visualizations — the list goes on.</p> <p><i>But virtually every web application needs to publish beautiful multimedia content.</i> Content creators need powerful, intuitive tools, and many world class frameworks like <a href="https://www.drupal.org/">Drupal</a> — at least out of the box — fall flat. It’s time for full featured content management systems and frameworks to put authors at the center of the experience, and they could all take a cue from WordPress.</p> <!-- google doc id: 1XtOe4-NqZnGmlkuVtWrSI3TFpZhvU3lcGv6NI4L4xgU --></div> <a href="/about/jon-bowman" hreflang="en">Jon Bowman</a> Thu, 04 Feb 2021 18:30:24 +0000 Eric Toupin 10220 at https://atendesigngroup.com https://atendesigngroup.com/articles/three-things-wordpress-authoring-experience-gets-right#comments Customizing the REST API Response in WordPress https://atendesigngroup.com/articles/customizing-rest-api-response-wordpress <span>Customizing the REST API Response in WordPress</span> <figure> <picture> <source srcset="/sites/default/files/18-07-31---Customizing-the-REST-API-Response-in-WordPress.png 1x" media="(min-width: 1860px)" type="image/png"/> <source srcset="/sites/default/files/18-07-31---Customizing-the-REST-API-Response-in-WordPress.png 1x" media="(min-width: 1540px)" type="image/png"/> <source srcset="/sites/default/files/styles/responsive_large/public/18-07-31---Customizing-the-REST-API-Response-in-WordPress.png?itok=HJiDRMHO 1x" media="(min-width: 1265px)" type="image/png"/> <source srcset="/sites/default/files/styles/responsive_medium/public/18-07-31---Customizing-the-REST-API-Response-in-WordPress.png?itok=qNUkV7I4 1x" media="(min-width: 1024px)" type="image/png"/> <source srcset="/sites/default/files/styles/responsive_medium/public/18-07-31---Customizing-the-REST-API-Response-in-WordPress.png?itok=qNUkV7I4 1x" media="(min-width: 768px)" type="image/png"/> <source srcset="/sites/default/files/styles/responsive_medium/public/18-07-31---Customizing-the-REST-API-Response-in-WordPress.png?itok=qNUkV7I4 1x" media="(min-width: 600px)" type="image/png"/> <source srcset="/sites/default/files/styles/responsive_small/public/18-07-31---Customizing-the-REST-API-Response-in-WordPress.png?itok=S61TyLmm 1x" media="(min-width: 500px)" type="image/png"/> <source srcset="/sites/default/files/styles/responsive_small/public/18-07-31---Customizing-the-REST-API-Response-in-WordPress.png?itok=S61TyLmm 1x" media="(min-width: 0)" type="image/png"/> <img src="/sites/default/files/18-07-31---Customizing-the-REST-API-Response-in-WordPress.png" alt="" typeof="foaf:Image" /> </picture> </figure> <span><a title="View user profile." href="/user/kay-thayer" lang="" about="/user/kay-thayer" typeof="schema:Person" property="schema:name" datatype="">Kay Thayer</a></span> <span>Mon, 07/30/2018 - 11:20</span> <a href="/blog/category/development" class="tag" >Code</a> <a href="/blog/category/wordpress" class="tag" >WordPress</a> <div class="field field--name-body field--type-text-with-summary field--label-hidden t--body field__item"><p>The <a href="https://developer.wordpress.org/rest-api/">WordPress REST API</a> is a core feature introduced in WordPress 4.7 allowing developers to make HTTP requests to access WordPress data as a JSON object. The REST API allows for many different solutions, whether you are looking to create a decoupled website or app with data from a WordPress backend, or to simply allow external websites to read a post feed. In this article is a look at common scenarios for customizing the REST API response for GET requests to posts.</p> <h2>A brief look at the core API</h2> By default, a WordPress site’s post information can be found at <span class="geshifilter"><code class="php geshifilter-php">yourwebsite<span style="color: #339933;">.</span>address<span style="color: #339933;">/</span>wp<span style="color: #339933;">-</span>json<span style="color: #339933;">/</span>v2<span style="color: #339933;">/</span>posts</code></span>. This endpoint displays the 10 most recent posts of any type. What is actually returned in the default response are the rows and fields in the database table <span class="geshifilter"><code class="php geshifilter-php">wp_posts</code></span> of type “post”, plus the ID(s) of the featured image, categories, and tags, if any. Similarly, pages (posts of type "page" in the WordPress database) can be found at <span class="geshifilter"><code class="php geshifilter-php">yourwebsite<span style="color: #339933;">.</span>address<span style="color: #339933;">/</span>wp<span style="color: #339933;">-</span>json<span style="color: #339933;">/</span>v2<span style="color: #339933;">/</span>pages</code></span>. <h3>Adding custom post types</h3> The REST API supports custom post types, but if the custom post type has been registered without setting the <span class="geshifilter"><code class="php geshifilter-php">show_in_rest</code></span> argument to <span class="geshifilter"><code class="php geshifilter-php"><span style="color: #009900; font-weight: bold;">true</span></code></span>, the response will return a 404 with the code <span class="geshifilter"><code class="php geshifilter-php">rest_no_route</code></span>. Ideally, if the WordPress website is registering the custom post type through a <strong>custom theme or plugin</strong>, the modification can be made in the original argument array passed to <a href="https://codex.wordpress.org/Function_Reference/register_post_type">register_post_type()</a>: <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> pluginname_custom_post_type<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'label'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Custom Post Type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'public'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'show_in_rest'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rest_base'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'custom'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> register_post_type<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'custom_post_type'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'custom_post_type'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div> <p>Note: Most custom post type arguments will and should utilize more of the many post type options available than shown in this short example.</p> <p>However, if the custom post type has been registered in a 3rd-party or contributed plugin or theme, it is a better practice to modify the arguments list in a function located in a custom theme or plugin and invoking the <span class="geshifilter"><code class="php geshifilter-php">register_post_type_args</code></span> hook:</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> pluginname_modify_custom_post_type<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post_type</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$post_type</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'custom_post_type'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'show_in_rest'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'rest_base'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'custom'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$args</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'register_post_type_args'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'modify_custom_post_type'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div> <p>In either case, setting <span class="geshifilter"><code class="php geshifilter-php">show_in_true</code></span> to <span class="geshifilter"><code class="php geshifilter-php"><span style="color: #009900; font-weight: bold;">true</span></code></span> allows a new endpoint to be accessed using the <span class="geshifilter"><code class="php geshifilter-php">rest_base</code></span> value. In this example: <span class="geshifilter"><code class="php geshifilter-php"><span style="color: #339933;">/</span>wp<span style="color: #339933;">-</span>json<span style="color: #339933;">/</span>wp<span style="color: #339933;">/</span>v2<span style="color: #339933;">/</span>custom</code></span>.</p> <h3>Adding fields to the WordPress core response</h3> There are cases where it is necessary or just convenient to modify the fields returned in the response. For example, while any data included in the <span class="geshifilter"><code class="php geshifilter-php">_links</code></span> section of a post with <span class="geshifilter"><code class="php geshifilter-php"><span style="color: #0000ff;">&quot;embeddable&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">true</span></code></span> can be included in its entirety without making another HTTP request by adding <span class="geshifilter"><code class="php geshifilter-php">_embed<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span></code></span> to the query, the embedded data might be too many layers deep to easily deal with from the API client's perspective: <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;"><span style="color: #0000ff;">&quot;_embedded&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;author&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Aten Design Group&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;url&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;https://www.aten.io&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;link&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;https://www.aten.io&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;slug&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;atendesigngroup&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span></pre></div> <p>In an example case where the API client needs the post's author name presented at the same level as the post title and publish date, it is best to <strong>add a new field</strong> to the response. It is important to <strong>not modify</strong> the existing <span class="geshifilter"><code class="php geshifilter-php">author</code></span> field to ensure that API clients are able to continue working with the standard core response, regardless of any one client's needs.</p> <p><a href="https://developer.wordpress.org/reference/functions/register_rest_field/">register_rest_field()</a> should be used in a custom function to add fields to the REST API response, and invoking that function with the <span class="geshifilter"><code class="php geshifilter-php">rest_api_init</code></span> hook:</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> pluginname_get_author_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$post</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> get_the_author_meta<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'display_name'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> &nbsp; <span style="color: #000000; font-weight: bold;">function</span> pluginname_add_author_name_to_api<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> register_rest_field<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'author_name'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'get_callback'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pluginname_get_author_name'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'rest_api_init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pluginname_add_author_name_to_api'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div> <h2>Creating a custom endpoint</h2> There are cases when the WordPress core REST API endpoints and fields are not suitable for a project: <ul> <li>A need to turn off the core endpoints,</li> <li>require authentication against a custom endpoint and not core endpoints or vice versa,</li> <li>limit or change fields returned,</li> <li>return a very specific set of fields, or</li> <li>some combination of these reasons.</li> </ul> In any of those cases it may be necessary to create a custom endpoint using <a href="https://developer.wordpress.org/reference/functions/register_rest_route/">register_rest_route</a>: <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> pluginname_get_post_items<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'post_status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'publish'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$posts</span> <span style="color: #339933;">=</span> get_posts<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$posts</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$post</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_title</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'author'</span> <span style="color: #339933;">=&gt;</span> get_the_author_meta<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'display_name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_author</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'content'</span> <span style="color: #339933;">=&gt;</span> apply_filters<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_content</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'teaser'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_excerpt</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$items</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> &nbsp; <span style="color: #000000; font-weight: bold;">function</span> pluginname_get_page_items<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pages</span> <span style="color: #339933;">=</span> get_pages<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$page</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_title</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'author'</span> <span style="color: #339933;">=&gt;</span> get_the_author_meta<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'display_name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_author</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'content'</span> <span style="color: #339933;">=&gt;</span> apply_filters<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_content</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'teaser'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_excerpt</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$items</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> &nbsp; <span style="color: #000000; font-weight: bold;">function</span> pluginname_register_api_endpoints<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> register_rest_route<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'pluginname/v2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/posts'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'methods'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'callback'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pluginname_get_post_items'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> register_rest_route<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'pluginname/v2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/pages'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'methods'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'callback'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pluginname_get_page_items'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> &nbsp; add_action<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'rest_api_init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pluginname_register_api_endpoints'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div> <p>Getting to know the capabilities of the WordPress REST API is a first step toward thinking outside the box with the WordPress CMS and creating new applications leveraging WordPress' standard structured data and backend.</p></div> <a href="/about/kay-thayer" hreflang="en">Kay Thayer</a> Mon, 30 Jul 2018 17:20:38 +0000 Kay Thayer 2507 at https://atendesigngroup.com