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.
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.