How echo HTML with plugin in sidebar?
-
Hello,
Im writing plugin, that should (after activating) output message (html) into existing (not necessary) sidebar (top of the page).
Is it possible? Or is there better way for such situation?Thanks you
-
It really depends on the theme. Sidebars in WP aren’t necessarily on the side. They are better called “widget_areas”. Themes could place widget content and hence “sidebars” anywhere they like. You could hook an action like ‘dynamic_sidebar_before’ and output something. Presumably almost all themes would be calling dynamic_sidebar() somewhere.
But themes could have multiple sidebars on one page. Using that action would result in output on every widget area defined unless you took measures to ensure the output only occurs once per request. You have no way of knowing where the output will actually occur.
IMO, plugins should not output anything on the front end by default. It’s rather presumptuous. Instead, provide shortcodes and widgets to generate your content so users can chose where they want your output to occur.
Thanks you, I ended up with custom hook > function > add_action.
Is it dynamic_sidebar_before better way?I know that plugins should not output anything on the front end by default, but this function is sense of this private plugin.
“dynamic_sidebar_before” is an action you would need to add. I think we’re more or less talking about similar things. The difference is exactly which action to hook. It determines where the output occurs. Which ever is closest to the position you want is the one to use.
If this is a site specific plugin, you should consider a child theme instead. Then define a template tag function that outputs the desired content. Call this function from whatever template is appropriate, placed where you want the output to occur. Then you are not restricted to where actions fire.
If you cannot create a child theme because your theme is a child of a framework (many commercial themes are), it’s possible to override templates from a plugin, though it can be rather cumbersome. Some themes may have some provision for template customization that is safe from theme updates.
Thanks you. Im using Undercore _S theme, so I can change everything I want, but this plugin is for situation when we cant work… so, anyone from our staff simply activate plugin and than message (and rest of functions) “We are not available now” appear on top of the page.
I see. You could add a
if ( function_exists('my_plugin_function'))
conditional to theme template(s) where you want the content to appear. Within the conditional call the named plugin function that echoes out the content.Interesting way to make adding a message easy for unsophisticated users! FWIW, I’d probably do it differently, maybe adding a menu item to the left hand admin menu that saves a boolean option value. What’s displayed both front and back end all depends on the value’s existing and being either true or false. It’s easier to manage who can do this by capability than adding plugins offers.
Just a suggestion, no pressure. Do what works best for you and your users.
Thanks you. I was googling for your suggested “menu item/boolean option” and Im affraid I was not successful.
Did you mean add_menu_page and build page for plugin? Does exist an tutorial for that?No, sorry for any confusion. It’s just coder jargon, nothing specific. No plugin AFAIK, a custom coded solution. I’m suggesting using add_menu_page() to add a menu item to the left hand admin menu. Such items are meant to be links to a settings page of some sort. That page request ends up running a callback function specified in add_menu_page() that generates the settings page output.
What that callback could do besides generating output is toggle an option value back and forth between
true
andfalse
— AKA “boolean” values. Options are updated with update_option(). And you find out what the current option value is with get_option(). Such clever function names ??The supposed settings page could simply display a message like “Message will now [be|not be] displayed.” with a “Return” link to take the user back to their referring page (where ever they came from before clicking the menu link) by using a bit of JavaScript. On the front end, code uses get_option() to check that boolean value to determine whether to display the message or not.
Alternately, the menu item callback does not toggle the boolean value itself, rather it outputs a form with a button that does the toggling. Or, this button could be added to another settings page using the Settings API if you preferred. Of course that would make the option more difficult to find by novice users. They’d have to know to go to, let’s say, the general settings page for example. Or, (the possibilities are endless) you could add a dashboard widget (or add to an existing widget maybe) to put up a button of some sort to toggle the option back and forth.
Just about anything is possible if you’re willing and able to code up the solution!
It sounds interesting, Ill try that. Thanks you Very much ??
- The topic ‘How echo HTML with plugin in sidebar?’ is closed to new replies.