2 Step use of WordPress 4 Customizer


/ Published in: PHP
Save to your folder(s)

To use WordPress 4 Customizer API I'd need just to do 2 steps: define a data in the array, and call a custom registering function.


Copy this code and paste it in your HTML
  1. <?php
  2. if (strtolower (realpath (__FILE__)) == strtolower (realpath ($_SERVER['SCRIPT_FILENAME']))) { header ('Location: http://www.bing.ca/'); die; }
  3.  
  4. // --------------------------------------------------------
  5. // Customize the theme
  6. //
  7. function HERE_theme_customize ($wp_customize)
  8. {
  9.  
  10. // STEP 1: Define the configuration
  11.  
  12. // --------------------------------------------------------
  13. $sections = array (array ('section' => 'theme_header',
  14. 'title' => __('Header', 'www'),
  15. 'description' => __('Configure the Header', 'www'),
  16. 'settings' => array (array ('setting' => 'theme_logo', 'label' => __('Site Logo', 'www'), 'control' => 'image'),
  17. array ('setting' => 'theme_head_text', 'label' => __('Text in Header', 'www'), 'control' => 'textarea'))),
  18. array ('section' => 'theme_footer',
  19. 'title' => __('Footer', 'www'),
  20. 'description' => __('Text in the footer', 'www'),
  21. 'settings' => array (array ('setting' => 'theme_footer_text_id', 'label' => __('Text Block: ID or Slug', 'www')),
  22. array ('setting' => 'theme_footer_privacy_url', 'label' => __('Link to the Privacy Page', 'www')),
  23. array ('setting' => 'theme_footer_copyright', 'label' => __('Copyright note at left', 'www')))),
  24. array ('section' => 'theme_home_page',
  25. 'title' => __('Home Page', 'www'),
  26. 'description' => __('Configure the Home Page', 'www'),
  27. 'settings' => array (array ('setting' => 'theme_home_slider', 'label' => __('Front slider: ID or Slug', 'www')),
  28. array ('setting' => 'theme_home_featured_title', 'label' => __('Featured posts: Title', 'www')),
  29. array ('setting' => 'theme_home_featured_cat', 'label' => __('Featured posts: Category Slug ', 'www')),
  30. array ('setting' => 'theme_home_portfolio_title', 'label' => __('Portfolio gallery: Title', 'www')),
  31. array ('setting' => 'theme_home_portfolio_id', 'label' => __('Portfolio gallery: ID or Slug', 'www')),
  32. array ('setting' => 'theme_home_why_us_title', 'label' => __('Why Us Block: Title', 'www')),
  33. array ('setting' => 'theme_home_why_us_image', 'label' => __('Why Us Block: Image', 'www'), 'control' => 'image'),
  34. array ('setting' => 'theme_home_why_us_id', 'label' => __('Why Us Block: ID or Slug', 'www')),
  35. array ('setting' => 'theme_home_contact_title', 'label' => __('Contact Block: Title', 'www')),
  36. array ('setting' => 'theme_home_contact_id', 'label' => __('Contact Block: ID or Slug', 'www')))),
  37. array ('section' => 'theme_socials',
  38. 'title' => __('Social', 'www'),
  39. 'description' => __('Links to your Social Sites', 'www'),
  40. 'settings' => array (array ('setting' => 'theme_social_facebook', 'label' => __('Facebook URL', 'www')),
  41. array ('setting' => 'theme_social_facebook_appid', 'label' => __('Facebook App ID', 'www')),
  42. array ('setting' => 'theme_social_twitter', 'label' => __('Twitter URL', 'www')),
  43. array ('setting' => 'theme_social_googleplus', 'label' => __('Google+ URL', 'www')),
  44. array ('setting' => 'theme_social_pinterest', 'label' => __('Pinterest URL', 'www')),
  45. array ('setting' => 'theme_social_etsy', 'label' => __('Etsy URL', 'www')),
  46. array ('setting' => 'theme_social_youtube', 'label' => __('Youtube URL', 'www')),
  47. array ('setting' => 'theme_social_instagram', 'label' => __('Instagram URL', 'www')))),
  48. array ('section' => 'theme_counters',
  49. 'title' => __('Counters', 'www'),
  50. 'description' => __('Code for the counters', 'www'),
  51. 'settings' => array (array ('setting' => 'theme_counter_google', 'label' => __('Google Analytics', 'www'), 'control' => 'textarea'))));
  52.  
  53. // STEP 2: Call the custom function, see below the source
  54.  
  55. HERE_customizer_register ($wp_customize, $sections);
  56.  
  57. }
  58.  
  59. // --------------------------------------------------------
  60. // Customize the theme
  61. // Supports text, textarea and image controls so far
  62. function HERE_customizer_register ($wp_customize, $sections)
  63. {
  64. $priority = 100;
  65.  
  66. if (is_array ($sections))
  67. {
  68. foreach ($sections as $section)
  69. {
  70. if (!isset ($section['builtin']) || !$section['builtin'])
  71. {
  72. $wp_customize->add_section ($section['section'],
  73. array ('title' => $section['title'],
  74. 'description' => $section['description'],
  75. 'priority' => (isset ($section['priority']) ? $section['priority'] : $priority++)));
  76. }
  77.  
  78. if (is_array ($section['settings']))
  79. {
  80. foreach ($section['settings'] as $setting)
  81. {
  82. $wp_customize->add_setting ($setting['setting']);
  83.  
  84. $setting['control'] = isset ($setting['control']) ? $setting['control'] : 'text';
  85.  
  86. switch ($setting['control'])
  87. {
  88. case 'image':
  89. $wp_customize->add_control (new WP_Customize_Image_Control ($wp_customize,
  90. $setting['setting'],
  91. array ('label' => $setting['label'],
  92. 'section' => $section['section'],
  93. 'settings' => $setting['setting'])));
  94. break;
  95.  
  96. case 'textarea':
  97. $wp_customize->add_control (new WP_Customize_Control ($wp_customize,
  98. $setting['setting'],
  99. array ('type' => $setting['control'],
  100. 'label' => $setting['label'],
  101. 'section' => $section['section'],
  102. 'settings' => $setting['setting'])));
  103. break;
  104.  
  105. default:
  106. $wp_customize->add_control ($setting['setting'], array ('label' => $setting['label'],
  107. 'section' => $section['section']));
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.