Is it possible to use variables from shortcode_atts in another function? Here is my idea:
Posting
[gallery ids="1,2,3,...n"]
Function get_gallery_ids()
//get the gallery-ID's from post
function get_gallery_ids($atts) {
extract(shortcode_atts(array(
'ids' => ''
), $atts));
return $ids;
}
Function explode_ids()
//example function with ids
function explode_ids($ids) {
$ids = explode(',' $ids);
}
How do I implement it? The return just echos.
Update
The code above is a part of my own new gallery_shortcode.
remove_shortcode('gallery', 'gallery_shortcode');
add_shortcode('gallery', 'get_gallery_ids');
I have an Idea:
I use examples from the WordPress Codex
http://codex.wordpress.org/Shortcode_API
function bartag_func( $atts ) {
global $post; // this is new
extract( shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts ) );
update_post_meta($post->ID, "gallery_used_atts", $atts); // this is new
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );
now you are able to get the $atts you are using on a specific post or page by
$att_values = get_post_meta( $post_id, "gallery_used_atts", true );
and then you can use them for whatever you want.
Related
what I want is that I have a special taxonomy and get_terms does not work without it being loaded, naturally the only way I can get it is to hook up to "init". But when this is the case, I will have to repeat this. I do not want this.
As you can see in the code below, I am doing the operation in init and trying to transfer it to "$ new_array". How can I do it?
protected function get_reactions()
{
$new_array = array();
add_action( 'init', function() use ( &$new_array ) {
$reactions = get_terms( array(
'taxonomy' => 'bp_reaction',
'hide_empty' => false
));
foreach ( $reactions as $value ) {
$priority = get_option( 'taxonomy_'.$value->term_id.'_priority' );
$image = get_option( 'taxonomy_'.$value->term_id.'_image' );
$new_array[$priority] = (object) array(
'id' => $value->term_id,
'priority' => $priority,
'slug' => $value->slug,
'name' => $value->name,
'image' => $image
);
}
}, 9 );
// Sort from largest to small
krsort( $new_array );
return $new_array;
}
I am trying to add the term to favorites via a short code. However when I add the short code [add_term term_id=840"] (for example..where term id=840 is a one particular term). It's not adding to my list of favorited terms? I don't know why...
function add_termid( $atts, $content = null) {
$atts = shortcode_atts(
array(
'term_id' => 'term_id',
), $atts
);
}
function store()
{
global $userdata;
$favorites_to_store = array();
foreach($this->favorites as $favorite)
{
$favorites_to_store[] = array('term_id' => $favorite['term_id'], 'taxonomy' => $favorite['taxonomy']);
}
if(version_compare(get_bloginfo('version'), '3.0', '<')) update_usermeta($userdata->ID, 'wp_favorites', $favorites_to_store);
else update_user_meta($userdata->ID, 'wp_favorites', $favorites_to_store);
}
add_shortcode ('add_term', 'add_termid');
I am trying to create a 'Pie Chart' shortcode to use in WP. All works fine apart from the percentage number. If it is entered into the array it works fine, but if I remove that number (ie; 100 - as seen in the code below), any number that is entered on the front-end by the user returns empty?? Quite puzzling?
function piechart_inner_shortcode( $atts ) {
extract( shortcode_atts( array(
'data_percentage' => '100',
'title' => 'Title',
), $atts ) );
$output = '<div class="chart"><div class="percentage" data-percent="'. $data_percentage .'"><span>'.$data_percentage.'%</span></div><div class="label"><strong>'.$title.'</strong></div></div>';
return $output;
}
add_shortcode( 'piechart_inner', 'piechart_inner_shortcode' );
And this is the shortcode that needs to be entered on the front-end -
[piechart_inner data-percent="45" title="WordPress"][/piechart_inner]
Which outputs nothing for the data-percent, whatever value is entered?
Many thanks
You are using the wrong variable. You are giving data-percent when you have variable data_percentage
Your shortcode should look like this:
[piechart_inner data_percentage="45" title="WordPress"][/piechart_inner]
Or change the function to following:
function piechart_inner_shortcode( $atts ) {
extract( shortcode_atts( array(
'data-percent' => '100',
'title' => 'Title',
), $atts ) );
$output = '<div class="chart"><div class="percentage" data-percent="'. $data-percent .'"><span>'.$data-percent.'%</span></div><div class="label"><strong>'.$title.'</strong></div></div>';
return $output;
}
add_shortcode( 'piechart_inner', 'piechart_inner_shortcode' );
Not sure what but the attributes from the shortcode aren't being passed to the handler function.
Shortcode in Wordpress Post
[k_recipe recipe="snack"]Here is some content[/k_recipe]
Shortcode Function
add_shortcode("k_recipe", "recipe_shortcode");
function recipe_shortcode($attributes, $content){
return "Hi " . $attributes["recipe"] . " Hi " . $content;
}
Shortcode Output
Hi Hi Here is some content
Why isnt the snack value being passed?? Any clue??
here is how to use shortcode with attributes
function bartag_func( $atts ) {
extract( shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts ) );
return "foo = {$foo}";
}
add_shortcode( 'myshortcode', 'bartag_func' );
[myshortcode foo="bar" bar="bing"]
you are missing extract thing
Maybe you need to use the extract function like documentation says:
// [bartag foo="foo-value"]
function bartag_func( $atts ) {
extract( shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts ) );
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );
http://codex.wordpress.org/Shortcode_API
I'm trying to get a shortcode to execute and pass the attribute value into a separate loop using get_template_part, the shortcode code is like this:
function test( $atts, $content = null ) {
extract( shortcode_atts( array('category' => '', 'type' => '' ), $atts ) );
ob_start();
get_template_part('loop', $type);
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
add_shortcode('test', 'test');
And then in the loop-$type.php file I have
$cat_id = get_cat_ID($category);
$args=array(
'cat' => $cat_id,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li> /* post stuff */ </li>
<?php
endwhile;
}
wp_reset_query();
But I can't get the cat_id to use the $category from the shortcode attribute. Does anyone know why the loop isn't using the shortcode attribute?
It clearly isn't passing the value on, which means I could make it global, but that is a nasty solution, there must be a clean way to do it?
(I have a post that is trying to execute the shortcode as [test category=random-category-name])
The variable $category is only within the scope of the function and isn't being passed to get_template_part().
Try making $category global.
function test( $atts, $content = null ) {
global $category;
extract( shortcode_atts( array('category' => '' ), $atts ) );
ob_start();
get_template_part('loop', $type);
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
add_shortcode('test', 'test');
Also, add global $category; to the top of your template file.
I had the same problem and I had trouble finding a good answer.
Apparently, setting a variable global like that is not the only solution. Instead, you could just 'include' the template into the php after the variables have been set and it works just as intended.
Check here for a better description and an example:
http://keithdevon.com/passing-variables-to-get_template_part-in-wordpress/