{"id":1432,"date":"2020-05-16T23:02:25","date_gmt":"2020-05-16T16:02:25","guid":{"rendered":"https:\/\/lab.wptips.dev\/?p=1432"},"modified":"2020-07-15T20:58:40","modified_gmt":"2020-07-15T13:58:40","slug":"simplify-wp-customizer-api","status":"publish","type":"post","link":"https:\/\/pixelstudio.id\/blog\/simplify-wp-customizer-api\/","title":{"rendered":"Simplify WordPress Ch.1 &#8211; The Customizer API"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>&#8220;Simplify WordPress&#8221; is our series of creating shortcut methods.<\/p><p><a href=\"https:\/\/pixelstudio.id\/blog\/simplify-wp-customizer-api\/\">Ch.1 &#8211; Customizer API<\/a><br>Ch.2 &#8211; Custom REST API<br>Ch.3 &#8211; Post Table (coming soon)<\/p><\/blockquote>\n\n\n\n<p>Have you ever tried modifying the Customizer section? If yes, you definitely noticed how unnecessarily complex it is.<\/p>\n\n\n\n<p>In this tutorial we will take a look at how to simplify it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Terminology<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"568\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/simplify-customizer-terminology.jpg\" alt=\"\" class=\"wp-image-1439\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/simplify-customizer-terminology.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/simplify-customizer-terminology-480x364.jpg 480w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><figcaption>The terminology used in WP Customizer<br><\/figcaption><\/figure>\n\n\n\n<ul><li><strong>SECTION<\/strong> &#8211; Group of settings.<\/li><li><strong>CONTROL<\/strong> &#8211; The input method of your setting.<\/li><li><strong>SETTING<\/strong> &#8211; What&#8217;s saved in the database. There are two types of setting:<ul><li><strong>THEME MOD<\/strong> &#8211; Bound to the theme. Changing theme will reset this.<\/li><li><strong>OPTION<\/strong> &#8211; Bound to the site. Changing theme will not affect this.<\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">The Original Method<\/h2>\n\n\n\n<p>Let&#8217;s say we want to create a Theme Options section that contains phone number and footer&#8217;s logo. Here&#8217;s what it looks like and the code:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"409\" height=\"394\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/simplify-customizer-result1.jpg\" alt=\"\" class=\"wp-image-1440\"\/><figcaption>Theme Options section with 2 controls<\/figcaption><\/figure><\/div>\n\n\n\n<pre title=\"\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_action( 'customize_register', 'create_theme_options' );\n\nfunction create_theme_options( $customize ) {\n  \/\/ Create section\n  $customize->add_section( 'theme_options', [\n    'title' => __( 'Theme Options' ),\n  ] );\n  \n  \/\/ Create setting for Phone Number\n  $customize->add_setting( 'phone_no', [\n    'type' => 'theme_mod',\n  ] );\n  \n  \/\/ Create text control, assign 'phone_no' setting\n  \/\/ and place it in 'theme_options' section\n  $customize->add_control( 'phone_no_control', [\n    'label' => __( 'Phone Number' ),\n    'type' => 'text',\n    'section' => 'theme_options',\n    'settings' => 'phone_no',\n  ] );\n\n  \/\/ Footer logo\n  $customize->add_setting( 'footer_logo', [\n    'type' => 'theme_mod',\n  ] );\n\n  $customize->add_control(\n    new WP_Customize_Image_Control(\n      $customize,\n      'footer_logo_control',\n      [\n        'label' => __( 'Footer Logo' ),\n        'section' => 'theme_options',\n        'settings' => 'footer_logo',\n      ]\n    )\n  );\n} \/\/ close function<\/code><\/pre>\n\n\n\n<p>In my opinion, those methods are too wordy and repetitive. Having to define the Setting and Control method separately is also questionable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Our Simplified Methods<\/h2>\n\n\n\n<p>We have created a helper class that mimics the functionality of the original:<\/p>\n\n\n\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-background has-blue-background-color\" href=\"https:\/\/gist.github.com\/hrsetyono\/ac02b00e38c437de7ad8a5fb9ac8d164\" target=\"_blank\" rel=\"noreferrer noopener\">Get the Helper Class code here<\/a><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">require_once __DIR__ . '\/my-customizer.php';\nadd_action( 'customize_register', 'create_theme_options' );\n\nfunction create_theme_options( $customize ) {\n  $customize = new MyCustomizer( $customize );\n\n  \/\/ No need to define title, it will be derived from the slug\n  $customize->add_section( 'theme_options' );\n\n  \/\/ Setting and Control method are combined\n  $customize->add_setting( 'phone_no', [\n    'type' => 'theme_mod',\n    'control' => [\n      'type' => 'text',\n      'section' => 'theme_options'\n    ]\n  ] );\n\n  \/\/ No longer need to pass in a Class object to define Image control\n  $customize->add_setting( 'footer_logo', [\n    'type' => 'theme_mod',\n    'control' => [\n      'type' => 'image',\n      'section' => 'theme_options'\n    ]\n  ] );\n}<\/code><\/pre>\n\n\n\n<p>Available input types:<\/p>\n\n\n\n<ul><li><code>text<\/code><\/li><li><code>select<\/code> &#8211; require <code>choices<\/code> argument that contains <code>values =&gt; labels<\/code> array. <\/li><li><code>radio<\/code> &#8211; also require <code>choices<\/code> argument.<\/li><li><code>checkbox<\/code> &#8211; just a single on\/off checkbox.<\/li><li><code>textarea<\/code><\/li><li><code>dropdown-pages<\/code><\/li><li><code>email<\/code><\/li><li><code>url<\/code><\/li><li><code>number<\/code><\/li><li><code>hidden<\/code><\/li><li><code>date<\/code><\/li><li><code>image<\/code><\/li><li><code>cropped_image<\/code> &#8211; Image that allows cropping. <a rel=\"noreferrer noopener\" aria-label=\"Detail here \u00bb (opens in a new tab)\" href=\"https:\/\/make.wordpress.org\/core\/2015\/07\/16\/new-customizer-media-controls-in-4-3-and-4-2\/\u00bbhttps:\/\/make.wordpress.org\/core\/2015\/07\/16\/new-customizer-media-controls-in-4-3-and-4-2\/\" target=\"_blank\">Detail here \u00bb<\/a><\/li><li><code>color<\/code><\/li><li><code>upload<\/code><\/li><li><code>visual_editor<\/code> &#8211; textarea with bold, italic, etc.<\/li><li><code>code_editor<\/code> &#8211; require <code>code_language<\/code> argument with possible value: clike, css, diff, htmlmixed, http, javascript, jsx, markdown, gfm, nginx, php, sass, shell, sql, xml, and yaml<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Customizer is an underused feature in WordPress. There are many reasons, but being harder to modify than a traditional Setting page is one of them.<\/p>\n\n\n\n<p>Hopefully with this helper class, you can give Customizer a try and see how useful it is.<\/p>\n\n\n\n<p>In Chapter 2, we will take a look at the dreaded Post Admin Table API.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Let me know in the comment below if you have feedback or question regarding Customizer API \ud83d\ude42<\/p><\/blockquote>\n\n\n\n<p>Useful Links:<\/p>\n\n\n\n<ul><li><a rel=\"noreferrer noopener\" aria-label=\"Section arguments list (opens in a new tab)\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_customize_section\/__construct\/\" target=\"_blank\">Section arguments list<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\"Setting arguments list (opens in a new tab)\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_customize_setting\/__construct\/\" target=\"_blank\">Setting arguments list<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\"Control arguments list (opens in a new tab)\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_customize_control\/__construct\/\" target=\"_blank\">Control arguments list<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/gist.github.com\/hrsetyono\/ac02b00e38c437de7ad8a5fb9ac8d164\" target=\"_blank\">MyCustomizer helper class<\/a> <\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever tried modifying the Customizer section? It is unnecessarily complex. Learn about a shortcut to simplify them here.<\/p>\n","protected":false},"author":1,"featured_media":1446,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[31],"class_list":["post-1432","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","tag-php"],"blocksy_meta":"","_links":{"self":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/comments?post=1432"}],"version-history":[{"count":9,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1432\/revisions"}],"predecessor-version":[{"id":1761,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1432\/revisions\/1761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media\/1446"}],"wp:attachment":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media?parent=1432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/categories?post=1432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/tags?post=1432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}