0

I'm trying to achieve the following functionality.

I have several widget 'areas' that I can place widgets into. I programmed these widget areas into the theme of my site and I can turn them on and off in the admin panel. The widget areas are basically sections on my site and I place text widgets into them and ads into those text widgets. The widget areas are also filled with other assorted content (newsletter sign up form, most popular posts, ect..).

I am using a plugin to do my ad management ( http://wordpress.org/plugins/ads-by-datafeedrcom/ ) and it has very nice functionality. I place the shortcode into my text widgets and the ads show up as expected.

I need to be able to exclude ads from the db queries that this plugin makes. I can pass in the ads to be excluded as attributes in the shortcode - this is not an issue.

My problem is I need to get the ID of the ad being displayed by the shortcode. I want the shortcode to return both the shortcode textual content and an array of excluded ads containing the current ad along with any previous ads (this would prevent duplicate ads from appearing on the site).

I would then pass this array of excluded IDs into the next shortcode via attributes and a global variable.

My question is this: Can shortcodes return anything other than strings? Is my setup (shortcodes to call ads, text widgets, managing ad placement by enabling widget areas) untenable?

I've gotten as far as this

[dfads params='groups=1234&ad_class=sidebar-ad&limit=1&orderby=random&excluded_ads=1,4,6']

which calls this

function dfads_shortcode( $atts ) { return dfads( $atts['params'] ); }

dfads returns get_ads and get_ads returns this

return array($this->output( $ads, $this->args ), $excluded_ads);

So I am returning an array containing a string ($this->output) & array ($excluded_ads) and I want to display the string and capture the array to pass on to the next shortcode.

Should I be doing this all in code in the loop? Is it possible to get this functionality out of widgets & shortcode?

-- Update 1

I noticed there are [methods]http://www.emanueleferonato.com/2011/04/11/executing-php-inside-a-wordpress-widget-without-any-plugin/ for running php code inside widgets.

Is this a safe if it's an admin only feature? Do I still have access to global variables? Is the execution flow basically the same as if I had the php code in my page flow where the widget appears instead of in the widget?

I could execute all this within the page being displayed but it seems a lot cleaner for administration by having this stuff in the widgets/dashboard.

  • Quoting from the Codex's add_shortcode() page: "Shortcode functions should return the text that is to be used to replace the shortcode." Nothing is said about returning an array. I suspect you'll have to find a different way to do what you're attempting to accomplish. – Pat J Sep 27 '13 at 2:40
  • Yeah, I saw that in the Codex but I wasn't sure if that meant there was a way but it's not standard or I shouldn't go that route at all. – seangwright Sep 27 '13 at 3:11
0

I think that your proposed solution, or plan of attack, is far more complicated than it needs to be. You can use a static variable to manage this. Proof of concept:

function shortcode_with_static_data($atts,$content) {
  static $ids;
  $ids[] = time();
  var_dump($ids); // you should return a string not dump data in a real shortcode
}
add_shortcode('scwsc','shortcode_with_static_data');

You would need to create your own shortcode, similar to the above, and use it as a wrapper around the add data shortcode or related functions. Without seeing the plugin code I can't really say exactly what that would look like.

  • Thanks, I will look into this method of static variables to keep track of this information. – seangwright Sep 28 '13 at 17:49

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.