I am new to wordpress and trying build shortcode with parameter using shortcode_atts
I have two array element foo
function bartag_func( $atts ) {
$a = shortcode_atts( array(
'name' => 'vaibhav',
'lname' => 'kulkarni',
), $atts );
return $a;
}
add_shortcode( 'bartag', 'bartag_func' );
and i want to use name and lname as parameter from below code
[bartag name="Alex'" lname="joe"]
if nothing is passed then it should print name as vaibhav and lname as kulkarni
am I doing in correct way? it is printing Array not as individual element.
Please let me know how to access each array element as parameter like this [bartag name="Alex'" lname="joe"]
As i am new to wordpress I think I was doing something in wrong way. return $a returning the whole array so instated of returning array I choose return $a['name']." ".$a['lname']; now with this code i can able to see as per my expected output
Related
I'm trying to get a list with all registered sidebars using $wp_registered_sidebars but the global variable returns an empty array.
function get_sidebars() {
global $wp_registered_sidebars;
$sidebar_options = array();
foreach ($wp_registered_sidebars as $sidebar)
{
$sidebar_options[$sidebar['id']] = $sidebar['name'];
}
return $sidebar_options;
}
$fields['sidebar_settings'] = array(
'active' => array(
...
'values' => get_sidebars(),
...
),
);
Why is the global variable empty and is there another way to store all registered sidebars in an array?
Please try this
https://wordpress.stackexchange.com/questions/13450/list-all-sidebar-names
you get all the sidebar name lists
Heres some functions that you could be looking for:
dynamic_sidebar
wp_get_sidebars_widgets
See this: http://codex.wordpress.org/Sidebars . There's also some good information.
Is it possible to make something like that:
$array = array('id' => '5', 'something_else' => 'hi');
function some_function($id, $something_else)
{
echo $something_else;
}
some_function(extract($array));
This code is giving me true/false and not $id,$something_else, etc..
It is important for me to do something like that, because I have different list of variables for each function ( I'm working on my ( let's call it ) "framework" and I want to pass list of variables instead of array with variables. This code is actually going in my router so it's not quite that simple, but in the end it comes to that ).
I assume you know how your array is built. So, why do not you just pass an array as a parameter and then use it in your function ?
If you do something like this you can access to your array's values :
echo $array['something_else'];
And build your function like this :
$array = array('id' => '5', 'something_else' => 'hi');
function some_function($an_array)
{
echo $an_array['something_else']; // value for something_else
}
some_function($array);
Or if you don't want to change function's parameters, call it like this :
some_function($array['id'], $array['something_else']);
Note: While this is probably a simple fix, I'm new to using arrays and am completely stumped.
I'm trying to save the data from a shortcode array via the Options API in WordPress, then call that array and use the data to create another array to hook into a plugin's function. It's a responsive slider plugin and I'm basically trying to attach a shortcode to it so I can create the slider on the backend and display it on the front end with a shortcode that looks like: [responsive_slider slider_name="imageslider"].
The implementation documentation can be found here, and here's my code:
function responsive_gallery_shortcode($atts, $content=null) {
extract(shortcode_atts( array('slider_name' => 'product_page') , $atts));
foreach ($slider_name as $value) {
update_option('_unique_slider_name', $value );
}
if(function_exists('show_flexslider_rotator'))
echo show_flexslider_rotator( $slider_name );
add_image_size( $slider_name , '550', '250', true );
}
add_shortcode('responsive_gallery', 'responsive_gallery_shortcode');
if (!function_exists('custom_set_flexslider_hg_rotators')) {
function custom_set_flexslider_hg_rotators() {
$slider_name = get_option('_unique_slider_name');
foreach ($slider_name as $value) {
$rotators = array();
$rotators[ $value ] = array( 'size' => $value );
return $rotators;
}
}
}
add_filter('flexslider_hg_rotators', 'custom_set_flexslider_hg_rotators', 9999);
I'm getting an "Invalid argument supplied for foreach()" error on both foreach functions. On the page where I have two shortcodes both errors show up twice. It seems as though $slider_name is a string instead of an array, but there's got to be a way to save it in the update_option() function so that it returns an array. I'm quite new to arrays, and I'm definitely struggling here. I've spent hours on this and have already received a little help on the WordPress side, but I'm not quite getting it.
As the shortcode attribute will arrive as a string, you need to convert it to an array first.
At the same time, as it has to be passed as a string, you'll need to use a separator so you can manage this.
And for all that, you'll need the PHP function explode.
$string = "one,two";
$array = explode( ',', $string );
var_dump( $array );
Results in:
array (size=2)
0 => string 'one' (length=3)
1 => string 'two' (length=3)
And
$string = "one";
$array = explode( ',', $string );
var_dump( $array );
Results in:
array (size=1)
0 => string 'one' (length=3)
PS: It's always worth to consult the PHP Manual and also the comments in each of its pages : http://www.php.net/manual/en/language.types.array.php
[update]
There are many issues with your original code, check the comments of this revised version:
function responsive_gallery_shortcode($atts, $content=null) {
extract(shortcode_atts( array('slider_name' => 'product_page') , $atts));
// Convert string into array
// Using comma as separator when writing the shortcode in the post
$array_slider = explode( ',', $slider_name );
// YOU DON'T NEED A `foreach` HERE
//foreach ($array_slider as $value) {
update_option('_unique_slider_name', $array_slider );
//}
// I DON'T KNOW WHAT THIS FUNCTIONS DOES
// But in any case, being $array_slider an array, maybe it should be inside a `foreach`
if(function_exists('show_flexslider_rotator'))
echo show_flexslider_rotator( $array_slider );
// THIS DOESN'T MAKE SENSE
// You're not supposed to be adding images sizes at each Shortcode call
// And you are dealing with an array
add_image_size( $slider_name , '550', '250', true );
}
add_shortcode('responsive_gallery', 'responsive_gallery_shortcode');
if (!function_exists('custom_set_flexslider_hg_rotators')) {
function custom_set_flexslider_hg_rotators() {
// The option was already saved as array, so we can work directly with it
$slider_name = get_option('_unique_slider_name');
// YOU DON'T WANT TO DECLARE THE VARIABLE AT EACH STEP OF THE LOOP
$rotators = array();
foreach ($slider_name as $value) {
$rotators[ $value ] = array( 'size' => $value );
}
// RETURN THE VALUE ONLY AFTER COMPLETING THE LOOP
return $rotators;
}
// PUT THE FILTER HOOK INSIDE if(!function_exists())
add_filter('flexslider_hg_rotators', 'custom_set_flexslider_hg_rotators', 9999);
}
If I have a shortcode like this:
[shortcode att1="true" att2="true"]
Is there any way to determine which attribute (att1 or att2) comes first? So if the shortcode looked like this it would give a different output than the first example:
[shortcode att2="true" att1="true"]
I have not tested this, and I guess it depends on how the Shortcode API handles the parameters internally, but as long as the shortcodes are added to an array in the order they are encountered when the shortcode is parsed you could probably check the order of the arguments in the atteibutes array supplied to your shortcode handler callback. Something like this might work:
// [bartag foo="foo-value" bar="bar-value"]
function bartag_func( $atts ) {
$first_param = null;
// Loop through $atts to check which parameter comes first
foreach ($atts as $att_key => $att_value) {
switch ($att_key) {
case 'foo':
case 'bar':
$first_param = $att_key;
break 2;
}
}
// Perform filtering/modifying content, settings defaults etc. according to parameter order
if ($first_param == 'foo') {
// foo came first
} else if ($first_param != null) {
// bar came first
}
// Supply defaults and extract parameters
extract( shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts ) );
// Return accordingly
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );
EDIT: I would try to really think things through before implementing such a function, as it is potentially a bit confusing for users unless it's clearly communicated that parameter order does in fact matter.
So, in order to get this code working, I need to output the function "get_flickr_rss" via a return rather than an echo... I believe the reason this code is not working for me is because the "get_flickr_rss" function itself is somehow defaulting to an echo rather than a return. How can I call the function to force it to return rather than echoing?
function generate_flickr_rss($atts, $content = null) {
// default parameters
extract(shortcode_atts(array(
'set' => '72157625809767439',
'photos' => '20'
), $atts));
// Call FLickrRSS Hook
return get_flickrRSS(array('set' => $set, 'num_items' => $photos, 'type' => 'set'));
}
I tried the following code to reverse engineer it as a return, but unfortunately, no dice.
function generate_flickr_rss($atts) {
// default parameters
extract(shortcode_atts(array(
'set' => '72157625809767439',
'photos' => '20'
), $atts));
// Call FLickrRSS Hook
$flickr_rss_return = get_flickrRSS(array('set' => $set, 'num_items' => $photos, 'type' => 'set'));
return $flickr_rss_return;
}
add_shortcode('flickr_rss', 'generate_flickr_rss');
add_shortcode('flickr_rss', 'generate_flickr_rss');
With output buffering you can catch the output of the function.
ob_start();
get_flickrRSS(...);
return ob_get_clean();
Of course you should rather change the function to return it's output, but I guess that's impossible for a reason. ;-)
I took a look at the WP plugin flickrRSS and the changes aren't that great.
In the function printGallery, there are a number of echo statements. Replace these with lines that concatenate the echoed string to a $result variable that your define as an empty string at the beginning of the function.
Then, return $result at the end of the printGallery function. Then, modify get_flickrRSS to return or echo this string according to your needs (as defined by another input parameter).
Of course, all of this will get overwritten the next time the plugin is updated, so I suggest pointing the plugin writer to this Stack Overflow page and asking him to modify the plugin to support your needs long term :)
$foo = get_flickrRSS(.....;
return $foo;