{"id":430,"date":"2015-12-03T18:49:26","date_gmt":"2015-12-03T18:49:26","guid":{"rendered":"https:\/\/possibleapp.com\/blog\/?p=430"},"modified":"2015-12-03T18:49:26","modified_gmt":"2015-12-03T18:49:26","slug":"wordpress-materialized-creating-a-material-design-theme-pt-1-theme-setup","status":"publish","type":"post","link":"https:\/\/possibleapp.com\/blog\/2015\/12\/wordpress-materialized-creating-a-material-design-theme-pt-1-theme-setup\/","title":{"rendered":"WordPress Materialized: Creating a Material Design Theme, pt. 1 \u2013 Theme Setup"},"content":{"rendered":"<p>Google\u2019s Material Design\u00a0is a comprehensive design language intended to be both beautiful and functional across all resolutions and devices, so it\u2019s a perfect match for WordPress theme developers who want to create modern responsive designs without having to reinvent the wheel. 34SP.com WordPress Hosting is providing this article to help those who want to explore Material Design in depth. In this tutorial, we\u2019re going to use\u00a0Materialize\u00a0to build a fully functional Material Design powered theme that looks like this:<\/p>\n<p><a href=\"https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/material-theme1-600x302.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-431\" src=\"https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/material-theme1-600x302-300x151.png\" alt=\"material-theme1-600x302\" width=\"475\" height=\"239\" srcset=\"https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/material-theme1-600x302-300x151.png 300w, https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/material-theme1-600x302.png 600w\" sizes=\"auto, (max-width: 475px) 100vw, 475px\" \/><\/a><\/p>\n<p>To begin with, we\u2019re going to generate a blank theme from Underscores. For those not familiar, Underscores is an essentially blank starter theme from Automattic, the company that created WordPress. It allows you to generate the basic files needed to create a theme, all namespaced according to your input. For anyone doing theme development, it\u2019s a big timesaver. Give your theme a name, and click \u201cGenerate\u201d. This will result in a zip file download containing your theme files.<\/p>\n<p>Next, <a href=\"http:\/\/materializecss.com\/bin\/materialize-v0.96.1.zip\" target=\"_blank\">download the Materialize package<\/a>, and extract both zip files into your working directory. (Note: Materialize supports both NPM and Bower, and also supports SASS development. This tutorial will cover traditional CSS, but if you prefer SASS, visit the Materialize Getting Started page and grab the SASS supported\u00a0package). In the Materialize folder, you\u2019ll see\u00a0three directories: css, font, and js. Copy all three to your new theme directory. This should leave you with a directory structure that looks like this:<\/p>\n<p><a href=\"https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/theme-structure3-600x302.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-433\" src=\"https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/theme-structure3-600x302-300x151.png\" alt=\"theme-structure3-600x302\" width=\"465\" height=\"234\" srcset=\"https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/theme-structure3-600x302-300x151.png 300w, https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/theme-structure3-600x302.png 600w\" sizes=\"auto, (max-width: 465px) 100vw, 465px\" \/><\/a><\/p>\n<p>Next, we need to add\u00a0the scripts and styles from Materialize into our theme. Open up the functions.php, and find your enqueue function. If you\u2019ve been following along exactly, it should start on line 102.\u00a0Because we\u2019re going to be using the Material based navigation, you\u2019ll want to remove the following line in that function:<\/p>\n<p><strong>wp_enqueue_script( &#8216;yourthemename-navigation&#8217;, get_template_directory_uri() . &#8216;\/js\/navigation.js&#8217;, array(), &#8216;20120206&#8217;, false );<\/strong><\/p>\n<p>Delete the associated script (js\/navigation.js), and create a new file called material-custom-scripts.js in the same\u00a0directory to hold your custom JavaScript. Add the following code to it:<\/p>\n<p><strong>jQuery(document).ready(function ($) {<\/strong><\/p>\n<p><strong> $(&#8216;.button-collapse&#8217;).sideNav({<\/strong><br \/>\n<strong> menuWidth: 300, \/\/ Default is 240<\/strong><br \/>\n<strong> edge: &#8216;left&#8217;, \/\/ Choose the horizontal origin<\/strong><br \/>\n<strong> closeOnClick: true \/\/ Closes side-nav on &lt;a&gt; clicks, useful for Angular\/Meteor<\/strong><br \/>\n<strong> }<\/strong><br \/>\n<strong> );<\/strong><br \/>\n<strong>});<\/strong><\/p>\n<p>This initializes the JQuery needed to trigger the side sliding navigation for mobile. We need to use a newer version of JQuery than what is bundled in WordPress, so we\u2019re going to setup a conditional load of JQuery when not in the admin screens, and then enqueue our scripts and styles:<\/p>\n<p><strong>\/\/ Add Material scripts and styles<\/strong><br \/>\n<strong> if( !is_admin()){<\/strong><\/p>\n<p><strong> wp_deregister_script(&#8216;jquery&#8217;);<\/strong><br \/>\n<strong> wp_enqueue_script( &#8216;material-jquery&#8217;, &#8216;http:\/\/code.jquery.com\/jquery-2.1.3.min.js&#8217;, array(), &#8216;1.0&#8217;, false );<\/strong><\/p>\n<p><strong> }<\/strong><br \/>\n<strong> wp_enqueue_style( &#8216;material-style&#8217;, get_template_directory_uri() . &#8216;\/css\/materialize.css&#8217; );<\/strong><br \/>\n<strong> wp_enqueue_script( &#8216;material-script&#8217;, get_template_directory_uri() . &#8216;\/js\/materialize.js&#8217;, array(), &#8216;1.0&#8217;, false ); <\/strong><br \/>\n<strong> wp_enqueue_script( &#8216;material-custom&#8217;, get_template_directory_uri() . &#8216;\/js\/material-custom-scripts.js&#8217;, array(), &#8216;1.0&#8217;, false );<\/strong><\/p>\n<p>Next\u00a0we need to\u00a0make some changes to the theme header file. First, open header.php and\u00a0modify the viewport settings to make sure that users don\u2019t have the ability to artificially zoom the screen and break our mobile patterns:<\/p>\n<p><strong>&lt;meta name=\u00bbviewport\u00bb content=\u00bbwidth=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\u00bb\/&gt;<\/strong><\/p>\n<p>Then,\u00a0we need to modify the navigation calls to use our side nav on mobile. Find and replace this:<\/p>\n<p><strong>&lt;nav id=\u00bbsite-navigation\u00bb class=\u00bbmain-navigation\u00bb role=\u00bbnavigation\u00bb&gt;<\/strong><br \/>\n<strong> &lt;button class=\u00bbmenu-toggle\u00bb aria-controls=\u00bbprimary-menu\u00bb aria-expanded=\u00bbfalse\u00bb&gt;&lt;?php _e( &#8216;Primary Menu&#8217;, &#8216;materialized&#8217; ); ?&gt;&lt;\/button&gt;<\/strong><br \/>\n<strong> &lt;?php wp_nav_menu( array( &#8216;theme_location&#8217; =&gt; &#8216;primary&#8217;, &#8216;menu_id&#8217; =&gt; &#8216;primary-menu&#8217; ) ); ?&gt;<\/strong><br \/>\n<strong>&lt;\/nav&gt;&lt;!&#8211; #site-navigation &#8211;&gt;<\/strong><\/p>\n<p>With this:<\/p>\n<p><strong>&lt;div class=\u00bbm-container nav\u00bb&gt;<\/strong><br \/>\n<strong> &lt;div class=\u00bbcontainer\u00bb&gt;<\/strong><br \/>\n<strong> &lt;nav id=\u00bbsite-navigation\u00bb class=\u00bbmain-navigation\u00bb role=\u00bbnavigation\u00bb&gt;<\/strong><br \/>\n<strong> &lt;a href=\u00bb#\u00bb data-activates=\u00bbmobile-nav\u00bb class=\u00bbbutton-collapse\u00bb&gt;&lt;i class=\u00bbmdi-navigation-menu\u00bb&gt;&lt;\/i&gt;&lt;\/a&gt;<\/strong><\/p>\n<p><strong>&lt;?php wp_nav_menu( array( &#8216;theme_location&#8217; =&gt; &#8216;primary&#8217;, &#8216;menu_class&#8217; =&gt; &#8216;menu side-nav&#8217;, &#8216;menu_id&#8217; =&gt; &#8216;mobile-nav&#8217;,&#8217;items_wrap&#8217; =&gt; &#8216;&lt;ul id=\u00bb%1$s\u00bb class=\u00bb%2$s\u00bb&gt;&lt;li class=\u00bbmobile-header\u00bb&gt;&lt;p&gt;Menu&lt;\/p&gt;&lt;\/li&gt;%3$s&lt;\/ul&gt;&lt;div class=\u00bbclear\u00bb&gt;&lt;\/div&gt;&#8217;, ) ); ?&gt;<\/strong><\/p>\n<p><strong> &lt;?php wp_nav_menu( array( &#8216;theme_location&#8217; =&gt; &#8216;primary&#8217;, &#8216;menu_class&#8217; =&gt; &#8216;hide-on-med-and-down&#8217; ) ); ?&gt;<\/strong><\/p>\n<p><strong>&lt;\/nav&gt;&lt;!&#8211; #site-navigation &#8211;&gt;<\/strong><br \/>\n<strong> &lt;div class=\u00bbclear\u00bb&gt;&lt;\/div&gt;<\/strong><br \/>\n<strong>&lt;\/div&gt;<\/strong><\/p>\n<p>This accomplishes a couple of things:<\/p>\n<ul>\n<li>It wraps the nav in a stand alone container we\u2019ll use for styling.<\/li>\n<li>It attaches the data-activates attribute, which Jquery will use to trigger\u00a0the mobile menu.<\/li>\n<li>It wraps the menu items as needed to use the Material display.<\/li>\n<li>It adds a second iteration of the menu which is hidden on Medium size and down, which is when the slide out menu is displayed.<\/li>\n<\/ul>\n<p>We also need to add a wrapper to float our content to a centered fix width in full screen displays. At the very last line of header.php, add the following:<\/p>\n<p><strong>&lt;div class=\u00bbcontainer\u00bb&gt;<\/strong><\/p>\n<p>Open your footer.php, clear any stray floats, and close the container (add above the content closing div):<\/p>\n<p><strong>&lt;div class=\u00bbclear\u00bb&gt;&lt;\/div&gt;<\/strong><br \/>\n<strong>&lt;\/div&gt;&lt;!&#8211; .container &#8211;&gt;<\/strong><\/p>\n<p>Next, let\u2019s add some CSS to support some of the elements we\u2019ve just put in place. Open up your style.css, and add the following:<\/p>\n<p><strong>.container {<\/strong><br \/>\n<strong> padding: 0 .5rem;<\/strong><br \/>\n<strong> margin: 0 auto;<\/strong><br \/>\n<strong> max-width: 1280px;<\/strong><br \/>\n<strong> width: 100%;<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p>I selected 1280px as a max-width based on what I\u2019ve seen Google use in their various applications for a 1920\u00d71080 resolution. You may want to modify that based on your individual needs. Next, we need to modify the default menu styles in a few ways.\u00a0\u00a0The default mobile styles which control menu visibility need to be removed. Find and delete the following:<\/p>\n<p><strong>\/* Small menu. *\/<\/strong><br \/>\n<strong>.menu-toggle {<\/strong><br \/>\n<strong> display: none;<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p><strong>@media screen and (max-width: 600px) {<\/strong><br \/>\n<strong> .menu-toggle,<\/strong><br \/>\n<strong> .main-navigation.toggled .nav-menu {<\/strong><br \/>\n<strong> display: block;<\/strong><br \/>\n<strong> }<\/strong><\/p>\n<p><strong>.main-navigation ul {<\/strong><br \/>\n<strong> display: none;<\/strong><br \/>\n<strong> }<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p>Next, we need to over ride the default material treatment for the nav element, since we\u2019re going to be styling the containing div instead:<\/p>\n<p><strong>.m-container nav {<\/strong><br \/>\n<strong> color: #fff;<\/strong><br \/>\n<strong> background-color: transparent;<\/strong><br \/>\n<strong> width: 100%;<\/strong><br \/>\n<strong> height: 56px;<\/strong><br \/>\n<strong> line-height: 56px;<\/strong><br \/>\n<strong> overflow: hidden;<\/strong><br \/>\n<strong> box-shadow: 0 0 0;<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p>And let\u2019s add a little padding around the site title to bring it in from the edges:<\/p>\n<p><strong>.site-branding {<\/strong><br \/>\n<strong> padding: 20px 30px;<\/strong><br \/>\n<strong>}<\/strong><\/p>\n<p>At this point, you have all of the basics in place to run the theme. Load it up in your staging area and view it in the browser. You should see something like this:<\/p>\n<p><a href=\"https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/theme2-600x302.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-436\" src=\"https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/theme2-600x302-300x151.png\" alt=\"theme2-600x302\" width=\"461\" height=\"232\" srcset=\"https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/theme2-600x302-300x151.png 300w, https:\/\/possibleapp.com\/blog\/wp-content\/uploads\/2015\/12\/theme2-600x302.png 600w\" sizes=\"auto, (max-width: 461px) 100vw, 461px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>It\u2019s not pretty yet, but we\u2019re now ready to begin the process of making it both beautiful and interactive. In part 2, we\u2019re going to use the <a href=\"https:\/\/codex.wordpress.org\/Theme_Customization_API\" target=\"_blank\">Theme Customization API<\/a> to add colour options to our theme, allowing the user to select from one of several palettes based on the <a href=\"http:\/\/materializecss.com\/color.html\" target=\"_blank\">Material Design colour schema<\/a>. We\u2019ll also implement Material UI elements, including Card objects, collections, and transition effects.<\/p>\n<p>See something in here that could be done better?\u00a0Tell us about it in the comments.<\/p>\n<p>&nbsp;<\/p>\n<h4>Read more in this series<\/h4>\n<p><a href=\"https:\/\/www.34sp.com\/hosting-news\/blog\/wordpress-materialized-creating-material-design-theme-pt-2-customization-colors-styles\/\">\u2018WordPress Materialized: Creating a Material Design Theme, pt. 2 \u2013 Customization, Colors, and Styles\u2019<\/a><\/p>\n<p><a href=\"https:\/\/www.34sp.com\/hosting-news\/blog\/wordpress-materialized-creating-material-design-theme-pt-3-sidebars-polish\/\">\u2018WordPress Materialized: Creating a Material Design Theme, pt. 3 \u2013 Sidebars and Polish\u2019<\/a><\/p>\n<p>&nbsp;<\/p>\n<h4>Are you a WordPress Guru?<\/h4>\n<p><a href=\"https:\/\/www.34sp.com\/hosting-news\/careers\/34sp-com-is-hiring-wordpress-guru\/\" target=\"_blank\">34sp.com are hiring a WordPress Guru<\/a><\/p>\n<p>Fuente: <a href=\"https:\/\/www.34sp.com\/hosting-news\/blog\/wordpress-materialized-creating-material-design-theme-pt-1-theme-setup\/\" target=\"_blank\">34sp.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google\u2019s Material Design\u00a0is a comprehensive design language intended to be both beautiful and functional across all resolutions and devices, so it\u2019s a perfect match for WordPress theme developers who want to create modern responsive designs without having to reinvent the wheel. 34SP.com WordPress Hosting is providing this article to help those who want to explore [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":437,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-430","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/posts\/430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/comments?post=430"}],"version-history":[{"count":2,"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/posts\/430\/revisions"}],"predecessor-version":[{"id":438,"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/posts\/430\/revisions\/438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/media\/437"}],"wp:attachment":[{"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/media?parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/categories?post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possibleapp.com\/blog\/wp-json\/wp\/v2\/tags?post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}