Get all WordPress sidebars - php

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.

Related

how to use seperate paramenter with shortcode_atts in wordpress

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

Putting correct php-code within eval()-string

I am working on a Wordpress project where I need to dynamically create a function (depending on which kind of template is used for a page or post) that retrieves the comments of each page in question.
So let's say I have pages within Wordpress with the IDs 100, 110, 120, 130, 140, 150 and out of these 3 are using the template called "blog" (e.g.: 100, 130 and 150).
So in order to retrieve the comments from these 3 pages with AJAX I need to create a function for each of them:
function GetComments100() { #### }
function GetComments130() { #### }
function GetComments150() { #### }
Here's the function code I need to create individually for each page (and which goes in between the function brackets above (instead of the ####):
$defaults = array( 'order' => 'DESC', 'post_id' => $functionID, 'post_type' => 'page', 'count' => false );
$comments = get_comments($defaults);
foreach($comments as $comment) :
echo "<div class='table-row' style='margin-bottom:1px'><div class='table-cell-1' style='width:110px;'>".$comment->comment_author.":</div><div class='table-cell-2'style='width:870px;'>".$comment->comment_content." <em><a>".$comment->comment_date." ... ".get_the_title($comment->comment_post_ID)." (".$comment->comment_post_ID.")</a></em></div></div>";
endforeach;
die($results);
In order to get the pages I use a loop-function which gives me the page ID as a variable (in my case its $functionID (also included in the array of my function above)).
I have already managed to dynamically create the functions with the following lines of code (I know "eval" is not a good choice but I didn't find any other solution):
$string = 'function ' . $functionName . "() {
####
}";
eval($string);
Now instead of the #### I need to integrate the actual function code starting with "$defaults = array(..." but obviously it has to be completely converted to a string - which is what I am struggling with.
Any help would be appreciated (again, I know using "eval" is not nice but so far I didn't find any other solution for this)
Have you tried using nowdoc for the function body? If you just need expanding $functionName, you can try something like this:
$string="function {$functionName}(){".<<<'END'
$defaults = array( 'order' => 'DESC', 'post_id' => $functionID, 'post_type' => 'page', 'count' => false );
$comments = get_comments($defaults);
foreach($comments as $comment) :
echo "<div class='table-row' style='margin-bottom:1px'><div class='table-cell-1' style='width:110px;'>".$comment->comment_author.":</div><div class='table-cell-2'style='width:870px;'>".$comment->comment_content." <em><a>".$comment->comment_date." ... ".get_the_title($comment->comment_post_ID)." (".$comment->comment_post_ID.")</a></em></div></div>";
endforeach;
die($results);
}
END;
eval($string);
I don't understand why you don't use one function per Template with a parameter like this:
public function getBlogComments($id){
//...
}
or one function which check the used Template
public function getComments($id){
// get Template of $id
//...
}

Save shortcode array with WordPress Options API then output in another function

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);
}

Is it possible to retrieve the order of attributes in a shortcode? (WordPress)

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.

Is it possible to page and sort two different models in the same view in CakePHP?

I want to do something very straight forward and simple. I want to have two different sets of paginated data on the same page. The two different sets depend on different models. For discussion's sake we'll say they are Image and Item.
I can set up two pagers for two models, and get the correct set of objects. I can get the correct pager links. But when it comes to actually following the links to the parameters, both pagers read the parameters and assume they apply to them.
It winds up looking something like this:
$this->paginate = array (
'Item'=>array(
'conditions'=>array('user_id'=>$id),
'limit' => 6,
'order' => array(
'Item.votes'=>'desc',
'Item.created'=>'desc'
),
'contain'=>array(
'User',
'ItemImage' => array (
'order'=>'ItemImage__imageVotes desc'
)
)
),
'Image'=>array(
'limit'=>6,
'contain'=>array(
'User',
'ItemImage'=>array('Item'),
),
'order'=>array(
'Image.votes'=>'desc',
'Image.views'=>'desc'
),
'conditions'=>array(
'Image.isItemImage'=>1,
'Image.user_id'=>$id
)
)
);
$this->set('items', $this->paginate('Item'));
$this->set('images', $this->paginate('Image'));
That's in the controller. In the view I have sort links that look like this:
<div class="control"><?php echo $this->Paginator->sort('Newest', 'Image.created', array('model'=>'Image')); ?></div>
However, that yields a link that looks like this:
http://localhost/profile/37/page:1/sort:Image.created/direction:asc
There's nothing in there to tell the paginator which model I intend to sort. So when I click on the link it attempts to sort both models by Image.created. The result is an error, because Item cannot be sorted by Image.created. Is there something I'm doing wrong? Or is this something that isn't supported by CakePHP's paginator?
You'll need to override the paginate method for the Model of the Controller of that page.
I did something similar, maybe this snippet will help:
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array())
{
$pageParams = compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive', 'group');
$this->contain('ModuleType', 'NodeDescriptor');
$pageItems = $this->find('all',$pageParams);
$pagesOut = array();
foreach($pageItems as $pageItem)
{
$status = $pageItem['SiteAdmin']['status_id'];
$moduleInfo = null;
$nodeTitle = $pageItem['NodeDescriptor']['title'];
$published = $pageItem['NodeDescriptor']['published'];
$pageitemID = $pageItem['SiteAdmin']['id'];
$moduleId = $pageItem['SiteAdmin']['module_id'];
$contName = $pageItem['ModuleType']['controller'];
if($moduleId)
{
$thisModel = ClassRegistry::getObject($moduleType);
$thisModel->contain();
$moduleInfo = $thisModel->read(null,$moduleId);
$moduleInfo = $moduleInfo[$moduleType];
}
$pagesOut[] = array(
'status'=>$status,
'node'=>$nodeTitle,
'published'=>$published,
'info'=>$moduleInfo,
'module_id'=>$moduleId,
'contName'=>$contName,
'pageitem_id'=>$pageitemID);
}
return $pagesOut;
}
By doing it this way, you gain control over the parameters passed to paginate, so you can pass model specific data, control flags etc.
The easiest solution would be to implement both grids as elements that fetch their own data and use AJAX to load the elements into the page.
The only other option would be to modify the params so you pass the params for both grids to each grid when sorting or stepping through pages. The code posted by Leo above is a good start. You can prepend the Model key from the paginate array onto each named param and make sure you pass all url params to the paginate function and you should be headed in the right direction.

Categories