0

I have text widget with default ads on it ( google ads for example ).

Sometime, depend on condition like location, time, I need to change the default ads from code, just temporary only.

Is there anyway to do that?

  • Have you looked into shortcodes? – birgire Nov 8 '15 at 10:54
  • I looked into add_filter and widget_text work perfectly, but you have to know exactly which widget to change, Thank. – Ken Jennings Nov 9 '15 at 11:29
0

It should be pretty easy to alter the text for the Core text widget via the widget_text filter:

function conditional_text_widget_wpse_207957($text) {
  if (is_single()) {
    $text = "Whoa, d00d! I'm single";
  }
  return $text;
}
add_filter('widget_text','conditional_text_widget_wpse_207957');

You do not specify what conditions you need exactly, nor provide any detail about the changes you need.

  • Thank for the help. The code work perfectly, I used something like this for the condition, using preg_match to know exactly which widget I modify: <code> function conditional_text_widget_wpse_207957($text) { if (preg_match( $found, $replace, $text )) { $text = "Whoa, d00d! I'm single"; } return $text; } add_filter('widget_text','conditional_text_widget_wpse_207957'); </code> – Ken Jennings Nov 9 '15 at 11:23

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.