0

I'm having a hard time getting my 'Hello World' to display on the theme page I created. It all at least appears to be correct (but obviously isn't) - what am I missing here?

// Add Theme Options to menu

function add_theme_menu_item() {

add_theme_page("Theme Panel", "Theme Panel", "manage_options", "theme-panel", "initialize_theme_options", null, 99);

}

add_action("admin_menu", "add_theme_menu_item");

 /**
 * Initializes the theme options page by registering the Sections,
  * Fields, and Settings.
 *
 * This function is registered with the 'admin_init' hook.
 */

 function initialize_theme_options() {

// First, we register a section. This is necessary since all future options must belong to one.

add_settings_section(

    // ID used to identify this section and with which to register options
    'main_settings_section',

    // Title to be displayed on the administration page
    'Main Theme Options',

    // Callback used to render the description of the section

    'main_settings_callback', 

    // Page

    'theme-panel'

);

} 

add_action('admin_init', 'initialize_theme_options');

 // end initialize_theme_options

/* ------------------------------------------------------------------------ *
* Section Callbacks
* ------------------------------------------------------------------------ */

/**
* This function provides a simple description for the General Options page. 
*
* It is called from the 'initialize_theme_options' function by being passed as a parameter
* in the add_settings_section function.
*/

function main_settings_callback() {

  echo 'Hello world.';

} 
// end main_settings_callback
1

This is wrong:

add_theme_page("Theme Panel", "Theme Panel", "manage_options", "theme-panel", "initialize_theme_options", null, 99);

The last two parameters shouldn't be there. Check this function in codex here.

EDIT:

What's more, the last parameter (which should be there) initialize_theme_options is wrong too, because it should be a callback to a function which would output a content. So for example your main_settings_callback function.

SECOND EDIT:

It doesn't work you probably because in the call of add_settings_section function you pass in the last parameter theme-panel and this page probably doesn't exist. Am I right? Try to replace this parameter for example with general and it'll output at the main Setting page.

  • Is there any reason why my init theme option wouldn't trigger 'add_settings_section' which has a callback of main_settings_callback? main_settings being what would output the content? – jasenmp Aug 20 '15 at 23:00
  • Check my second edit please. – Emetrop Aug 20 '15 at 23:17
  • Hmm, you're right on that one. I'm wondering why I am unable to pass the theme page (with a slug of theme-panel) to the add _settings_function page parameter. It does in fact exist (the Theme Panel page), it's just blank. I had assumed by adding an additional page (via add_theme_page) that I could then pass the slug of this page to add_settings_section and it would work. Am I approaching this in the wrong manner? Thanks for your feedback on this by the way, much appreciated. – jasenmp Aug 20 '15 at 23:20
  • Ah, I see what I'm doing wrong. The add_theme_page has a $menu_slug parameter NOT a $page_slug paramater. Duh. But wait, this is what I was basing my assumption on: '$page (string) (required) The menu page on which to display this section. Should match $menu_slug from Function Reference/add theme page' from: codex.wordpress.org/Function_Reference/add_settings_section – jasenmp Aug 20 '15 at 23:24
0

Ok, I got it.

https://codex.wordpress.org/Function_Reference/do_settings_sections

function initialize_theme_options() {

// First, we register a section. This is necessary since all future options must belong to one.

add_settings_section(

    // ID used to identify this section and with which to register options
    'main_settings_section',

    // Title to be displayed on the administration page
    'Main Theme Options',

    // Callback used to render the description of the section

    'main_settings_callback', 

    // Page

    'theme-panel'

);

do_settings_sections('theme-panel');

} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.