{"id":5002,"date":"2022-02-11T16:31:00","date_gmt":"2022-02-11T16:31:00","guid":{"rendered":"https:\/\/corbinrose.com\/blog\/?p=3390"},"modified":"2022-02-11T16:31:00","modified_gmt":"2022-02-11T16:31:00","slug":"creating-madlibs-with-advanced-custom-fields-in-wordpress","status":"publish","type":"post","link":"https:\/\/corbinrose.com\/blog\/technology\/creating-madlibs-with-advanced-custom-fields-in-wordpress\/","title":{"rendered":"Creating Madlibs with Advanced Custom Fields in WordPress"},"content":{"rendered":"\n<p>I, like many of you, grew up playing MadLibs\u00ae \u2013 and I sure do miss them! So, I decided to try to make some of my own using WordPress and the<a rel=\"noreferrer noopener\" href=\"https:\/\/www.advancedcustomfields.com\/\" target=\"_blank\">&nbsp;Advanced Custom Fields<\/a>&nbsp;plugin. The way I set this up was to make two fields in a group:&nbsp;<br><strong>Madlib Body:<\/strong>&nbsp; text field with the script of the MadLib with variables&nbsp;<br><strong>Madlib Parts of Speech:<\/strong>&nbsp; list of the parts of speech associated with the variables in the MadLib Body field Here is an example script I\u2019ve added to a post:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>One day Dino decided to go for a walk. It was a sunny day and the earth was especially &nbsp;<strong>%1%<\/strong> &nbsp;Dino was really &nbsp;<strong>%2%<\/strong>&nbsp; and was thinking about &nbsp;<strong>%3%<\/strong>&nbsp; (even though this was culturally inappropriate). He noticed a T-Rex in the &nbsp;<strong>%4%<\/strong> . Then he saw that the T-Rex was headed for a &nbsp;<strong>%5%<\/strong> ! Dino got very &nbsp;<strong>%6%<\/strong>&nbsp; when he saw the T Rex coming with a &nbsp;<strong>%7%<\/strong>&nbsp; . So he decided to &nbsp;<strong>%8%<\/strong>&nbsp; and &nbsp;<strong>%9%<\/strong> . As Dino was &nbsp;<strong>%10%<\/strong>&nbsp; his luck had turned. He was trembling with &nbsp;<strong>%11%<\/strong> when he decided to go into the &nbsp;<strong>%12%<\/strong> . It was then that Dino saw &nbsp;<strong>%13%<\/strong>&nbsp; and &nbsp;<strong>%14%<\/strong> . And he thought that is was time for lunch. He found some &nbsp;<strong>%15%<\/strong>&nbsp; and munched. For desert he had a &nbsp;<strong>%16%<\/strong> . He then took a nap and began to deam about &nbsp;<strong>%17%<\/strong> . Oh, Dino thought, another &nbsp;<strong>%18%<\/strong>&nbsp;!<\/code><\/pre>\n\n\n\n<p>&nbsp; And the parts of speech:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1:adjectives;2:adjectives;3:pluralnouns;4:pluralnouns;5:nouns;6:adjectives;7:nouns;8:verbs;9:verbs;10:verbing;11:pluralnouns;12:pluralnouns;13:pluralnouns;14:verbed;15:pluralnouns;16:nouns;17:pluralnouns;18:nouns<\/code><\/pre>\n\n\n\n<p>So, now what? In my template to show a single post (<code>templates\/loop-single.php<\/code>&nbsp;in my case), I added some code to check if those fields are filled in or not. (By default they are blank for non-Madlib posts).<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$madlibBody = get_post_meta( get_the_ID(), &#39;madlib_body&#39;, true);\nif(!empty($madlibBody)){\n   \/\/ it&#39;s a madlib!\n   $madlibparts = get_post_meta( get_the_ID(), &#39;madlib_parts_of_speech&#39;, true);\n   \/\/ get the parts as well\n<\/code><\/pre><\/div>\n\n\n\n<p>Next. we check to see if this is a form submit, or the first time viewing the page.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>if(isset($_REQUEST[&#39;madlibsubmit&#39;])){\n  \/\/ because we named the parts of speech &quot;part_[num]&quot; we can search for them in the request\nforeach($_REQUEST as $name=&gt;$param){\n\t\t\t\t$pos = strpos($name, &quot;part_&quot;);\n\t\t\t\tif($pos===false){\n\t\t\t\t   \/\/ it is a different request param\n\t\t\t\t} else {\n\t\t\t\t\t$num = substr($name, 5); \n\t\t\t\t\t$madlibBody = str_replace(&quot;%&quot;. $num . &quot;%&quot;, &quot;&quot; . $_REQUEST[&#39;part_&#39; . $num] . &quot;&quot;, $madlibBody);\n\t\t\t\t}\n\t\t\t}\n\t\t\t$madlibBody = str_replace(&quot;[p]&quot;, &quot;&lt;br \/&gt;&lt;br \/&gt;&quot;, $madlibBody);\n\t\t\techo $madlibBody . &quot;&lt;br \/&gt;&quot;;\n\t\t\t$post; $post_slug = $post-&gt;post_name; \n\t\t\t\n\t\t\techo &quot;&lt;a class=&#39;ac-btn btn-biggest btn-opaque&#39; href=&#39;\/blog\/madlib\/&quot; . $post_slug . &quot;&#39;&gt;Start over&lt;\/a&gt;&quot;;<\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Now, we will cover the \u201celse\u201d condition when the user first sees the page:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>} else {\necho &quot;&lt;form action=&#39;&#39; method=&#39;POST&#39;&gt;&quot;;\n\t\t\t$allparts = explode(&quot;;&quot;,$madlibparts);\n\t\t\tforeach($allparts as $part){\n\t\t\t\t$exp = explode(&quot;:&quot;,$part);\n\t\t\t\techo &quot;&lt;div class=&#39;row form-group&#39;&gt;&lt;div class=&#39;col-sm-2&#39;&gt;&quot; . $exp[1] . &quot;&lt;\/div&gt;&quot;;\n\t\t\t\techo &quot;&lt;div class=&#39;col-sm-10&#39;&gt;&quot;;\n\t\t\t\techo &quot;&lt;input type=&#39;text&#39; placeholder=&#39;&#39; class=&#39;form-control&#39; name=&#39;part_&quot; . $exp[0] . &quot;&#39; \/&gt;&quot;;\n\t\t\t\techo &quot;&lt;\/div&gt;&lt;\/div&gt;&quot;;\n\t\t\t}\n\t\t\techo &quot;&lt;br \/&gt;&quot;;\n\t\t\techo &#39;&lt;input type=&quot;submit&quot; class=&quot;btn btn-primary&quot; value=&quot;Post the fun&quot; name=&quot;madlibsubmit&quot; \/&gt;&#39;;\n\t\t\techo &quot;&lt;hr \/&gt;&quot;;\n\t\t\techo &quot;&lt;\/form&gt;&quot;;\n}<\/code><\/pre><\/div>\n\n\n\n<p>Now when this post loads, the user will see a form with inputs for the configured parts of speech, and when \u201csubmit\u201d is clicked, the form posts to itself and gets the form variables, the script and where the variables should be and presents a madlib! Here is a result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>One day Dino decided to go for a walk. It was a sunny day and the earth was especially &nbsp;<strong><u>tacit<\/u><\/strong>&nbsp;&nbsp;Dino was really &nbsp;<strong><u>bright<\/u><\/strong>&nbsp; and was thinking about &nbsp;<strong><u>ducks<\/u><\/strong>&nbsp; (even though this was culturally inappropriate). He noticed a T-Rex in the &nbsp;<strong><u>cherries<\/u><\/strong>&nbsp;. Then he saw that the T-Rex was headed for a &nbsp;<strong><u>butter<\/u><\/strong>&nbsp;! Dino got very &nbsp;<strong><u>succinct<\/u><\/strong>&nbsp; when he saw the T Rex coming with a &nbsp;<strong><u>opinion<\/u><\/strong>&nbsp; . So he decided to &nbsp;<strong><u>test<\/u><\/strong>&nbsp; and &nbsp;<strong><u>thank<\/u><\/strong>&nbsp;. As Dino was &nbsp;&nbsp; his luck had turned. He was trembling with &nbsp;<strong><u>offices<\/u><\/strong>&nbsp;when he decided to go into the &nbsp;<strong><u>lizards<\/u><\/strong>&nbsp;. It was then that Dino saw &nbsp;<strong><u>boats<\/u><\/strong>&nbsp; and &nbsp;&nbsp;. And he thought that is was time for lunch. He found some &nbsp;<strong><u>beds<\/u><\/strong>&nbsp; and munched. For desert he had a &nbsp;<strong><u>adjustment<\/u><\/strong>&nbsp;. He then took a nap and began to deam about &nbsp;<strong><u>brothers<\/u><\/strong>&nbsp;. Oh, Dino thought, another &nbsp;<strong><u>lip<\/u><\/strong>&nbsp;!<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/corbinrose.com\/blog\/madlib\/dino-madlib\/\" target=\"_blank\" rel=\"noreferrer noopener\">Try making a madlib with this script!<\/a>&nbsp;Coming soon: a tutorial post about how to make an admin page in WordPress to edit the scripts and parts of speech!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I, like many of you, grew up playing MadLibs\u00ae \u2013 and I sure do miss them! So, I decided to try to make some of my own using WordPress and the&nbsp;Advanced Custom Fields&nbsp;plugin. The way I set this up was to make two fields in a group:&nbsp;Madlib Body:&nbsp; text field with the script of the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":5319,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[28,17],"tags":[],"class_list":["post-5002","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-technology","clearfix","post-index"],"acf":[],"jetpack_featured_media_url":"https:\/\/corbinrose.com\/blog\/wp-content\/uploads\/2023\/01\/madlibs.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/posts\/5002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/comments?post=5002"}],"version-history":[{"count":0,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/posts\/5002\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/media\/5319"}],"wp:attachment":[{"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/media?parent=5002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/categories?post=5002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/tags?post=5002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}