0

I am looking for inserting a function's output before sidebar's first widget, and one in the end after the last widget

Is it possible?

3 Answers 3

1

You can create your own actions using do_action, like this:

<?php do_action('before_sidebar'); // you can name it whatever you want ?>
<?php if ( is_active_sidebar('main-sidebar') ) : ?>
    <ul>
        <?php dynamic_sidebar('main-sidebar'); ?>
    </ul>
<?php endif; ?>
<?php do_action('after_sidebar'); ?>

and use add_action() normally:

add_action('before_sidebar', 'before_sidebar_function');
add_action('after_sidebar', 'after_sidebar_function');

UPDATE: depending on the codex you can use wp_meta hook:

Runs when the sidebar.php template file calls the wp_meta function, to allow the plugin to insert content into the sidebar.

but it should be added by theme authors.

This page might be helpful Full Action Hooks Reference.

4
  • k so that means there isn't an already made action for it. i am working on a plugin this might not work as the user wud have to add the sidebar name or add it before sidebar.. etc.
    – Ayaz Malik
    Jun 12, 2012 at 12:13
  • If there was sidebar actions they should be on the action reference page on the codex.
    – Pierre
    Jun 12, 2012 at 14:03
  • i already went through it, can't find anything like what i need and stated above. i am actually making my 1st plugin.. so new to this. as i see a way around is i would need to register a widget with required function in ti ? and the user wud need to add it from widgets area right ? or is there a way around?
    – Ayaz Malik
    Jun 12, 2012 at 15:19
  • 1
    yes widgets will do the job, it's not a big deal if the user will have to activate the widget.
    – Pierre
    Jun 13, 2012 at 6:29
0

Yes. Put your function, and anything else you want, before and after the dynamic_sidebar function. For example.

<?php if(function_exists ('my_function')) {
    my_function();
} ?>

<p>This is above the first widget.</p>

<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>

<?php endif; // end sidebar widget area ?>

<?php if(function_exists ('my_second_function')) {
    my_second_function();
} ?>

<div>
<p><a href="http://www.agreatsite.com">Blog Link</a></p>
</div>

http://codex.wordpress.org/Function_Reference/dynamic_sidebar

Put the function inside a plugin, or even easier, inside your theme's functions.php file.

0

There are two options that may not yield the results you are looking for, but it's something.

Hooking a function onto the sidebar?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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