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?


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') ) : ?>
        <?php dynamic_sidebar('main-sidebar'); ?>
<?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.

| improve this answer | |
  • 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 '12 at 12:13
  • If there was sidebar actions they should be on the action reference page on the codex. – Pierre Jun 12 '12 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 '12 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 '12 at 6:29

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

<?php if(function_exists ('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')) {
} ?>

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


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

| improve this answer | |

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

Hooking a function onto the sidebar?

| improve this answer | |

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.