{"id":135874,"date":"2015-01-10T08:00:37","date_gmt":"2015-01-10T13:00:37","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=135874"},"modified":"2018-02-22T15:57:17","modified_gmt":"2018-02-22T15:57:17","slug":"workout-diary-wordpress","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/workout-diary-wordpress\/","title":{"rendered":"How to Create Your Own Workout Diary With WordPress"},"content":{"rendered":"<p>Did you eat too much turkey and pie during the Christmas break and gain a couple of pounds? Did you make a New Year&#8217;s resolution to start exercising properly in 2015?<\/p>\n<p>For many people tracking is the key to success. While there are some great tools available like Fitbit, <a href=\"https:\/\/lifesum.com\/\" target=\"_blank\">Lifesum<\/a> and other apps and devices, we&#8217;re yet to see a real, personalized global application. For instance you may want to track the number of soft drinks you drink, record\u00a0how stressful your day was and so on.<\/p>\n<p>With the help of WordPress, the free <a title=\"Advanced Custom Fields\" href=\"http:\/\/www.advancedcustomfields.com\/\" rel=\"noopener\" target=\"_blank\">Advanced Custom Fields plugin<\/a> and a tiny bit of coding, we&#8217;re going to create a simple but powerful workout diary in today&#8217;s Weekend WordPress Project. Let&#8217;s get started.<\/p>\n<h2>Our Environment<\/h2>\n<p>I&#8217;ll be working with a completely fresh WordPress installation. If you would like to keep this diary private you can use the <a href=\"https:\/\/wordpress.org\/plugins\/registered-users-only\/\" target=\"_blank\">Registered Users Only<\/a> plugin or you can <a href=\"https:\/\/wpmudev.com\/blog\/vagrant-wordpress-test-environment\/\" target=\"_blank\">install WordPress locally<\/a>. Make sure to install the <a href=\"https:\/\/wordpress.org\/plugins\/advanced-custom-fields\/\" target=\"_blank\">Advanced Custom Fields<\/a> plugin and activate it.<\/p>\n<p>We&#8217;ll be using Twenty Fifteen, the new default WordPress theme but we&#8217;ll create a child theme since we&#8217;ll modify it a tiny bit. Create a new folder in the <code>wp-content\/themes<\/code> directory \u2013\u00a0name it workout-diary. In the new folder create two files: <code>style.css<\/code> and <code>functions.php<\/code>.<\/p>\n<p>Open the new stylesheet you created and paste the following:<\/p>\n<div class=\"gist\" data-gist=\"f420b1f6f655c53ec2ca203101c64491\" data-gist-file=\"style.css\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/f420b1f6f655c53ec2ca203101c64491.js?file=style.css\">Loading gist f420b1f6f655c53ec2ca203101c64491<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Feel free to modify the details, the only important line to keep as is is the &#8220;template&#8221; one where we define that the Twenty Fifteen theme is the parent theme. Once done, open your new functions file and paste the following:<\/p>\n<div class=\"gist\" data-gist=\"14a3341ad02a97c9d66106ecb5052b93\" data-gist-file=\"functions.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/14a3341ad02a97c9d66106ecb5052b93.js?file=functions.php\">Loading gist 14a3341ad02a97c9d66106ecb5052b93<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The code above loads the stylesheet of the parent theme. This allows us to modify looks instead of having to recreate them from scratch.<\/p>\n<h2>Planning Our Diary<\/h2>\n<p>Next let&#8217;s create the fields we&#8217;ll be using for the diary. To add them we&#8217;ll need to know what they are first. We&#8217;ll also need to decide what a post will be used for. Should we use a post for a single day and add all our information (potentially multiple workouts) to it, or should one post be a workout or some other event?<\/p>\n<p>Let&#8217;s handle each post as its own event. We&#8217;ll\u00a0split\u00a0posts into three categories: &#8220;Workouts,&#8221; &#8220;Meal&#8221; and &#8220;Sleep&#8221; \u2013 these are the three most important aspects of a healthy lifestyle.<\/p>\n<p>Each category will have its own settings. Here&#8217;s a quick breakdown:<\/p>\n<ul>\n<li><strong>Workouts<\/strong>\n<ul>\n<li>Type<\/li>\n<li>Duration<\/li>\n<li>Distance<\/li>\n<li>Calories<\/li>\n<li>Link<\/li>\n<\/ul>\n<\/li>\n<li><strong>Meal<\/strong>\n<ul>\n<li>Calories<\/li>\n<li>Fat<\/li>\n<li>Protein<\/li>\n<li>Carbohydrates<\/li>\n<li>Cholesterol<\/li>\n<\/ul>\n<\/li>\n<li><strong>Sleep<\/strong>\n<ul>\n<li>Start<\/li>\n<li>End<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Based on this list we can set up our fields using ACF. I&#8217;ll create an option group for each category and make sure to set up ACF to only show the group for the correct category.<\/p>\n<p>Be sure to create the categories before creating your fields so you can select them in the display rules.<\/p>\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-135876\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/01\/fields.png\" alt=\"Custom Workout Fields\" width=\"799\" height=\"629\" \/><\/div>\n<\/div>\n<p>Next, create a post and select any of your categories. You should see the list of fillable fields appear under the post editor. Go ahead and fill them out now.<\/p>\n<p>As an optional extra, find a nice featured image for your post and add that, too.<\/p>\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-135877\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/01\/post-fields.png\" alt=\"Post Fields\" width=\"665\" height=\"607\" \/><\/div>\n<\/div>\n<p>If you preview your post it should already look pretty good but it doesn&#8217;t have any of the info we entered. Let&#8217;s change that and make sure that everything is displayed.<\/p>\n<p>There are a few ways to go about this, the most obvious is to modify the file that displays our content. You could also use the <code>functions.php<\/code> file to hook into the <code>the_content<\/code> filter or you could write a plugin. Let&#8217;s go with using the functions file.<\/p>\n<p>We&#8217;ll need to add a single function, which prepends our workout details underneath our workout posts. Here goes:<\/p>\n<div class=\"gist\" data-gist=\"8eb5c2aa7bbee37185db833fdde6c2ae\" data-gist-file=\"workout.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/8eb5c2aa7bbee37185db833fdde6c2ae.js?file=workout.php\">Loading gist 8eb5c2aa7bbee37185db833fdde6c2ae<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This function must return the full content of the post. In our case this would be the content of the post editor with our additional data prepended. First of all, I check if this is indeed a workout post. If it is I gather all the data from ACF into a table. Then I return the original content and prepend our diary to it.<\/p>\n<p>The important function here is <code>get_field()<\/code>. Pass this any field name (from our custom fields backend) to grab the data for that field. You can use this method for the meal and sleep posts as well:<\/p>\n<div class=\"gist\" data-gist=\"908bc43d394682bbd195116538e162d7\" data-gist-file=\"full.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/908bc43d394682bbd195116538e162d7.js?file=full.php\">Loading gist 908bc43d394682bbd195116538e162d7<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>And here&#8217;s what our workout diary looks like on the front-end:<\/p>\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-135878\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/01\/workout.png\" alt=\"Workout Post\" width=\"839\" height=\"1101\" \/><\/div>\n<\/div>\n<h2>Conclusion<\/h2>\n<p>Using this method and modifying it to your needs lets you\u00a0create a flexible and information-rich platform for yourself where you can record all the things important in your life.<\/p>\n<p>What I like most about creating mini-diaries like this is the personalization, flexibility and future uses it offers. First of all, no one is telling you what information should be important to you. If you want to record the number of birds you counted while skating it&#8217;s easy.<\/p>\n<p>This simple system is actually pretty flexible. Since you are storing everything as discrete data your information can be plotted easily on graphs, bar charts, maps and other visualization tools.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you ate way too many fruit mince pies during the Christmas holidays, check out today&#8217;s Weekend WordPress Project.<\/p>\n","protected":false},"author":344049,"featured_media":136028,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[9798],"tutorials_categories":[],"class_list":["post-135874","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-weekend-wordpress-projects"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/135874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/344049"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=135874"}],"version-history":[{"count":4,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/135874\/revisions"}],"predecessor-version":[{"id":209764,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/135874\/revisions\/209764"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/136028"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=135874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=135874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=135874"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=135874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}