• Resolved janburg

    (@janburg)


    I’ve added the php code to functions.php to see my new widget box in the Appearance > Widgets page but it is not showing up on the website. I have some CSS for the size and background color in the style.css. What else do I need to do to get this to show up?

    css: “.extra-widget-area {
    min-height: 90px;
    margin-bottom: 10px;
    background: #bddadb;
    color: #653535;”

    php: “// Adds a widget area.
    if (function_exists(‘register_sidebar’)) {
    register_sidebar(array(
    ‘name’ => ‘Extra Header Widget Area’,
    ‘id’ => ‘extra-widget-area’,
    ‘description’ => ‘Extra widget area before the header’,
    ‘before_widget’ => ”,
    ‘after_widget’ => ”,
    ‘before_title’ => ”,
    ‘after_title’ => ”
    ));
    }

    // Place the widget area after the header
    add_action (‘__before_header’, ‘add_my_widget_area’, 10);
    function add_my_widget_area() {
    if (function_exists(‘dynamic_sidebar’)) {
    dynamic_sidebar(‘Extra Header Widget Area’);
    }
    }”

    Using Genesis Frameworks theme, child theme (of course).

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @janburg.

    Make sure that the hook you’re using (_before_header) actually gets triggered on the page you’re trying to do this on.

    You can figure that out by installing the Query Monitor plugin, and seeing what hooks fire on that page.

    Otherwise, you could try a different hook (such as “genesis_before_header”)

    Thread Starter janburg

    (@janburg)

    I’m not at your skill level, so bear with me. I installed Query Monitor, but am not sure what I’m looking at. I did not see anything about “genesis_before_header” there. It is in the header.php file, I think: “do_action(‘genesis_before’)” and “do_action( ‘genesis_before_header’ );
    do_action( ‘genesis_header’ );
    do_action( ‘genesis_after_header’ );”

    Does the order matter in functions.php? I have the code to add the widget before the code for adding support for a custom header – should that be after instead?

    And if you have any resources you can point me to, to understand hooks and actions, I’d greatly appreciate it! So much to learn…

    Moderator bcworkz

    (@bcworkz)

    More on filters and actions: https://developer.wordpress.org/plugins/hooks/

    The order on functions.php usually does not matter. There are exceptions though. What usually matters is what order the various hooks fire in, and secondarily, what order callbacks are called in for a specific hook. Obviously you cannot do something before a thing it’s dependent on is first set up. You cannot add a child menu page until the parent menu page exists. You cannot manage what order hooks fire in, but you can manage which ones you use.

    For callback order on the same hook, you can use the priority parameter when adding a callback to mange execution order. The larger the priority number, the later the associated callback is called relative to others on the same hook.

    Most important though is using a hook that actually fires 🙂

    Thread Starter janburg

    (@janburg)

    Yeah, I have no idea how to do this, making sure the hook is firing.

    Thread Starter janburg

    (@janburg)

    Wait a second, I almost got it! The new widget is on the page, but it is below the header, not above. But I see it – that’s a start!

    Moderator bcworkz

    (@bcworkz)

    Then the action is firing 🙂 But you may need a different action that fires earlier. I believe the query monitor tells you which are firing. A manual way to tell for a specific hook is to add an error_log() call to its callback. If the passed arbitrary message appears in the error log, the hook fired. Simply generating output can also demonstrate that a hook fires, but some hooks fire when the browser is not expecting output, so direct output is not always reliable. Error logging should always work.

    I usually just look at the source code to identify potential hooks to use, but the query monitor is a lot easier. I just prefer to not introduce a layer of abstraction. Reading source code is not for everyone though 😉

    Thread Starter janburg

    (@janburg)

    Thru trial and error I got it to work. The more I play the more I learn, but it sure is a slow way to get things done. Reading thru the code to me helps me understand what is going on, with a little help from websites that explain in very simple terms. Thanks for your help too, I have marked this resolved.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘I see my new widget in the Widgets page, but not on the site’ is closed to new replies.