Here’s an (untested) idea where we inject the sidebar-inject
sidebar before sidebar-target
sidebar with help of the dynamic_sidebar_before
hook :
add_action( 'dynamic_sidebar_before', 'wpse_inject_sidebar', 10, 2 );
function wpse_inject_sidebar( $index, $has_widgets )
{
// Only target front-end
if( is_admin() )
return;
// Only target 'sidebar-target'
if( 'sidebar-target' !== $index )
return;
// Make sure 'sidebar-inject' is active
if( ! is_active_sidebar( 'sidebar-inject' ) )
return;
// Avoid recursive loop
remove_action( 'dynamic_sidebar_before', 'wpse_inject_sidebar', 10 );
// Inject 'sidebar-inject'
dynamic_sidebar( 'sidebar-inject' );
// Re-hook it again
add_action( 'dynamic_sidebar_before', 'wpse_inject_sidebar', 10, 2 );
}
Here we watch out for a recursive loop, by removing the action before calling dynamic_sidebar()
inside the hook’s callback.
But by targeting only 'sidebar-target'
here and making sure it’s different from 'sidebar-inject'
, we actually avoid a recursive loop.