0

Is there a way to detect whether a specific page contains a widget or sidebar.

I am aware of is_active_sidebar() & is_active_widget() which return true or sidebar containing the widget, but this is performed globally.

I need to detect and then perform a function based on the outcome i.e. include javascript in <head>

2
  • What isn't global about is_active_sidebar()?
    – s_ha_dum
    Commented Oct 21, 2015 at 13:01
  • It is completely global. But I am looking for something that is page specific. Commented Oct 21, 2015 at 13:08

1 Answer 1

0

Sidebars aren't assigned to pages by means of, say, database entries or any other means that PHP can detect easily. Sidebars just run where a function call tells them to run. You can't really query to see if a sidebar is on a particular page because there is nothing to query. Well, almost nothing. The only way to do this would be to load and parse your PHP template files for the presence of an appropriate call to dynamic_sidebar(). That would be quite a task.

A similar problem exists with widgets. You can find out which sidebar a widget is attached to but you have the problem, as above, of finding out where the sidebar runs.

1
  • Yeah, was checking the db out and the only entries are in the options table, but they're more relational objects between Widgets and Sidebars. Thanks for your reply. Going to have to tackle this by means of options table and hooks... possibly... :) Will create a gist and link it up here if I get it right. Commented Oct 21, 2015 at 14:44

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.