Last active
March 7, 2017 16:04
-
-
Save iledcom/726722d7221b568560813a4e0ecdbab9 to your computer and use it in GitHub Desktop.
Add a block of options to the basic page "General" WP
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
// ------------------------------------------------------------------ | |
// Вешаем все блоки, поля и опции на хук admin_init | |
// ------------------------------------------------------------------ | |
// | |
add_action( 'admin_init', 'site_name_api_init' ); | |
function site_name_api_init() { | |
// Добавляем блок опций на базовую страницу "Общие" | |
add_settings_section( | |
'site_name_section', // секция | |
'Настройки шапки сайта', | |
'site_name_section_callback_function', | |
'general' // страница | |
); | |
add_settings_section( | |
'header_links_section', // секция | |
'Название ссылок в шапке сайта', | |
'header_links_section_callback_function', | |
'general' // страница | |
); | |
// Добавляем поля опций. Указываем название, описание, | |
// функцию выводящую html код поля опции. | |
add_settings_field( | |
'site_name', | |
'Название сайта', | |
'site_name_callback_function', // можно указать '' | |
'general', // страница | |
'site_name_section' // секция | |
); | |
add_settings_field( | |
'site_name_2', | |
'Название сайта продолжение', | |
'site_name_callback_function2', | |
'general', // страница | |
'site_name_section' // секция | |
); | |
add_settings_field( | |
'header_links_text', | |
'Текст ссылки_1', | |
'header_links_text_callback_function', // можно указать '' | |
'general', // страница | |
'header_links_section' // секция | |
); | |
add_settings_field( | |
'header_links', | |
'Ссылка_1', | |
'header_links_callback_function', // можно указать '' | |
'general', // страница | |
'header_links_section' // секция | |
); | |
add_settings_field( | |
'header_links_text_2', | |
'Текст ссылки_2', | |
'header_links_text_callback_function2', | |
'general', // страница | |
'header_links_section' // секция | |
); | |
add_settings_field( | |
'header_links_2', | |
'Ссылка_2', | |
'header_links_callback_function2', | |
'general', // страница | |
'header_links_section' // секция | |
); | |
// Регистрируем опции, чтобы они сохранялись при отправке | |
// $_POST параметров и чтобы callback функции опций выводили их значение. | |
register_setting( 'general', 'site_name' ); | |
register_setting( 'general', 'site_name_2' ); | |
register_setting( 'general', 'header_links_text' ); | |
register_setting( 'general', 'header_links' ); | |
register_setting( 'general', 'header_links_2' ); | |
register_setting( 'general', 'header_links_text_2' ); | |
} | |
// ------------------------------------------------------------------ | |
// Сallback функция для секции | |
// ------------------------------------------------------------------ | |
// | |
// Функция срабатывает в начале секции, если не нужно выводить | |
// никакой текст или делать что-то еще до того как выводить опции, | |
// то функцию можно не использовать для этого укажите '' в третьем | |
// параметре add_settings_section | |
// | |
function site_name_section_callback_function() { | |
echo '<p>Укажите имя сайта, которое должно отображаться в шапке сайта</p>'; | |
} | |
// ------------------------------------------------------------------ | |
// Callback функции выводящие HTML код опций | |
// ------------------------------------------------------------------ | |
// | |
// Создаем text input теги | |
// | |
function site_name_callback_function() { | |
echo '<input | |
name="site_name" | |
type="text" | |
value="' . get_option( 'site_name' ) . '" | |
class="code" | |
/>'; | |
} | |
function site_name_callback_function2() { | |
echo '<input | |
name="site_name_2" | |
type="text" | |
value="' . get_option( 'site_name_2' ) . '" | |
class="code2" | |
/>'; | |
} | |
function header_links_section_callback_function() { | |
echo '<p>Напишите короткое предложение, которое должно отображаться как ссылка в шапке сайта</p>'; | |
} | |
function header_links_text_callback_function() { | |
echo '<input | |
name="header_links_text" | |
type="text" | |
value="' . get_option( 'header_links_text' ) . '" | |
class="code2" | |
/>'; | |
} | |
function header_links_callback_function() { | |
echo '<input | |
name="header_links" | |
type="text" | |
value="' . get_option( 'header_links' ) . '" | |
class="code2" | |
/>'; | |
} | |
function header_links_text_callback_function2() { | |
echo '<input | |
name="header_links_text_2" | |
type="text" | |
value="' . get_option( 'header_links_text_2' ) . '" | |
class="code2" | |
/>'; | |
} | |
function header_links_callback_function2() { | |
echo '<input | |
name="header_links_2" | |
type="text" | |
value="' . get_option( 'header_links_2' ) . '" | |
class="code2" | |
/>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment