Created
February 11, 2021 08:48
-
-
Save roscabgdn/1d00e18f5fa26c2601a55d2e0d4376ab to your computer and use it in GitHub Desktop.
Add elements in customizer: panels, sections, settings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// In folderul themes creati un folder inc daca nu-l aveti deja. | |
// In acest folder creati un fisier customizer.php | |
// ( el poate sa fie salvat si sub alt nume, eu doar am pus acest nume pentru ca e sugestiv). | |
// In interiorul acestui fisier (customizer.php) copy paste la functia prefix_customizer_register. | |
// prefix_ ar trebui inlocuit cu numele theme-ului vostru. Ex: bogdan_customizer_register | |
// La fel si textdomain-ul. | |
function prefix_customizer_register( $wp_customize ) { | |
/** | |
* Adaugam un panel. | |
* Panels sunt o metoda de a grupa mai multe sectiuni. | |
* Un panel poate sa contina una sau mai multe sectiuni | |
*/ | |
$wp_customize->add_panel( 'optiuni_theme', array( | |
'priority' => 10, | |
'capability' => 'edit_theme_options', | |
'theme_supports' => '', | |
'title' => __('Optiuni ThemeName', 'textdomain'), | |
'description' => __('Several settings pertaining my theme', 'textdomain'), | |
) ); | |
/** | |
* Adaugam o sectiune. | |
* Pentru a adauga o sectiune este nevoie ca valoarea atributului 'panel' sa fie | |
* identica cu id-ul panelului declarat. In cazul nostru "header_settings". | |
*/ | |
$wp_customize->add_section( 'header_settings', array( | |
'priority' => 10, | |
'capability' => 'edit_theme_options', | |
'theme_supports' => '', | |
'title' => __('Header Settings', 'textdomain'), | |
'description' => __('Header elements configuration', 'textdomain'), | |
'panel' => 'optiuni_theme', | |
) ); | |
/* | |
* Adaugam o setare la nivel de section. | |
* La fel ca in cazul sectiunilor, putem sa adaugam mai multe elemente de tip settings. | |
* | |
*/ | |
$wp_customize->add_setting( 'mesaj_header', array( | |
"default" => "", | |
"transport" => "postMessage", | |
) ); | |
$wp_customize->add_control( new WP_Customize_Control( $wp_customize, "mesaj_header", | |
array( | |
"label" => __("Mesaj global", "farmec"), | |
"section" => "header_settings", | |
"settings" => "mesaj_header", | |
"type" => "text", | |
) | |
) ); | |
/** | |
* Adauga ca settings un numar de telefon. | |
*/ | |
$wp_customize->add_setting( 'numar_telefon', array( | |
"default" => "", | |
"transport" => "postMessage", | |
) ); | |
$wp_customize->add_control( new WP_Customize_Control( $wp_customize, "numar_telefon", | |
array( | |
"label" => __("Numar telefon", "textdomain"), | |
"section" => "header_settings", | |
"settings" => "numar_telefon", | |
"type" => "text", | |
) | |
) ); | |
} | |
/** | |
* Functia noastra va rula in momentul in care action-ul customize_register va rula. | |
* Adica in momentul in care se incarca WordPress. | |
*/ | |
add_action( 'customize_register', 'prefix_customizer_register' ); | |
// in functions.php va trebui sa includem fisierul customizer.php | |
// Adaugati codul de mai jos in functions.php. | |
// In acest moment in zona de Appearance / Customizer sau Aspect / Customizer in functie de limba ar trebui | |
// sa apara campurile adaugate mai sus. | |
/** | |
* Customizer additions. | |
*/ | |
require get_template_directory() . '/inc/customizer.php'; | |
// Pentru a prelua campurile salvate in baza de date in theme-ul vostru va trebui sa folositi functia get_theme_mod: | |
// Noi mai sus am declarat numar_telefon get_theme_mod( 'numar_telefon' ) | |
echo get_theme_mod( 'id_setting' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment