Shortcode displaying strangely - wordpress

I'm trying to get a shortcode to add an id to a div, and that part works fine, but it adds some extra stuff and I'm not sure if it's because I did something wrong, or if it's something else. Here is the actual shortcode:
function jump_function($params){
//Extract parameters and supply default values
extract( shortcode_atts( array(
'id' => ''
), $params ) );
//The parameters are now stored as variables
return do_shortcode('<div id="' . $id . '"</div>');
}
add_shortcode( 'jump', 'jump_function' );
I'm trying to get it to display as
<div id="id-here"></div>
but on the page it's displaying as this:
<div div="" <="" id="id-here" style="position: relative;"><div></div></div>
Did I do anything wrong?

do_shortcode doesn't work like this...
you don't need it.
function jump_function($params){
//Extract parameters and supply default values
extract( shortcode_atts( array(
'id' => ''
), $params ) );
//The parameters are now stored as variables
return '<div id="' . $id . '"</div>';
}
add_shortcode( 'jump', 'jump_function' );
http://codex.wordpress.org/do_shortcode

Related

How to ensure shortcode result doesn't break formatting?

I want to add a shortcode which will execute a DB query and return the result.
Here's my functions.php:
function get_posts_count($cat){
global $wpdb;
$a = shortcode_atts( array(
'id' => ''
), $cat );
$id=$a['id'];
$count=$wpdb->get_results( "SELECT `count` FROM `wpmy_term_taxonomy` WHERE `term_id`=$id");
foreach($count as $row)
echo '('.$row->count.')';
}
add_shortcode( 'postCount', 'get_posts_count' );
This is the shortcode in the editor:
And here's the end result:
The value in this case 1 appears above the text Real Estate. How can I make sure it is displayed within the line?
Thanks in advance
the shortcode accepts parameters (attributes) and return a result (the shortcode output). If the shortcode produces a HTML then ob_start can be used to capture output and convert it to a string as follows:-
function get_posts_count( $cat ) {
ob_start();
global $wpdb;
$a = shortcode_atts( array(
'id' => '',
), $cat );
$id = $a['id'];
$count = $wpdb->get_results( "SELECT `count` FROM `wpmy_term_taxonomy` WHERE `term_id`=$id" );
foreach ( $count as $row ) {
echo '(' . $row->count . ')';
}
return ob_get_clean();
}
add_shortcode( 'postCount', 'get_posts_count' );

Use Shortcode in Shortcode (insert post meta field value)

i have a shortcode from a plugin which i cant modify... This shortcode has some arguments ex. [some_shortcode value=""] - I tried to input the value from post meta as argument for this shortcode, but its not working - here is the code...
This is the code from shortcode i created ( it returns values from post meta )
function test_shortcode( $string ) {
extract( shortcode_atts( array(
'string' => 'string'
), $string));
// check what type user entered
switch ( $string ) {
case 'first':
return get_post_meta( get_the_ID(), 'post_meta_one', true );
break;
case 'second':
return get_post_meta( get_the_ID(), 'post_meta_two', true );
break;
}
}
add_shortcode('test', 'test_shortcode');
Now i want to insert this shortcode in the existing shortcode from the plugin on my page.
For example: [some_shortcode value='[test string="first"]']
It isnt working like this. Thanks for help!
It will not work to insert the shortcode in the existing shortcode like you provided. Your shortcode should have opportunity to process the provided shortcode as attribute.
You should to use the do_shortcode() in your shortcode. You have
[some_shortcode value='[test string="first"]']
and want to use returned value of [test string="first"] which is first in your shortcode. Your code will be:
function some_shortcode($atts){
$atts = shortcode_atts(array(
'value' => ''
), $atts);
$second_shortcode_value = do_shortcode($atts['value']);
//some code
return $something;
}
add_shortcode('some_shortcode', 'some_shortcode');
The variable $second_shortcode_value will containt the output of the [test string="first"] shortcode.
P.S. Avoid of using exctract() function just because it can make your code hard readable.
EDIT:
Here is solution dinamically add attributes to [some_shortcode] value attribute.
function my_shortcode($atts){
$atts = shortcode_atts(array(
'str' => ''
), $atts);
switch ( $atts['str'] ) {
case 'first':
$modified = get_post_meta( get_the_ID(), 'leweb_gender', true );
break;
default:
$modified = '';
}
if(!empty($modified)) {
$second_shortcode_with_value = do_shortcode("[some_shortcode value='$modified']");
}else{
$second_shortcode_with_value = do_shortcode('[some_shortcode]');
}
return $second_shortcode_with_value;
}
add_shortcode('some_shrt', 'my_shortcode');
What we are doing: instead of calling [some_shortcode value='something'] we are generating something into our shortcode and getting content like
[some_shrt str="first"]

add class to widget_shopping_cart_content in woocommerce

I want to add class to widget_shopping_cart_content in WooCommerce without JS and Jquery. I want to filter fragment in function file.
JS and jquery are not working on this file.
I have found this code in class-wc-ajax.php file and I want to modify this code.
public static function get_refreshed_fragments() {
ob_start();
woocommerce_mini_cart();
$mini_cart = ob_get_clean();
$data = array(
'fragments' => apply_filters( 'woocommerce_add_to_cart_fragments', array(
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>',
)
),
'cart_hash' => apply_filters( 'woocommerce_add_to_cart_hash', WC()->cart->get_cart_for_session() ? md5( json_encode( WC()->cart->get_cart_for_session() ) ) : '', WC()->cart->get_cart_for_session() ),
);
wp_send_json( $data );
}
I want to add dropdown-menu class in this <div class="widget_shopping_cart_content dropdown-menu">' . $mini_cart . '</div>'; but how can I didn't know.
Can please any one help me.
Option 1: str_replace();
add_filter('woocommerce_add_to_cart_fragments', 'mm_string_replace_add_to_cart_fragments');
function mm_string_replace_add_to_cart_fragments($fragments){
return $fragments['div.widget_shopping_cart_content'] = str_replace('class="widget_shopping_cart_content"', 'class="widget_shopping_cart_content dropdown-menu"', $fragments['div.widget_shopping_cart_content']);
}
This does not require additional call to woocommerce_mini_cart() and is much faster but less flexible then other method mentioned below.
Option 2: Overwrite the get_refreshed_fragments function
add_filter('woocommerce_add_to_cart_fragments', 'mm_change_add_to_cart_fragments');
function mm_change_add_to_cart_fragments(){
ob_start();
woocommerce_mini_cart();
$mini_cart = ob_get_clean();
return array(
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content dropdown-menu">' . $mini_cart . '</div>',
)
}
Essentially we are recalling the entire function but with some modifications. So first run on get_refreshed_fragments goes to waste. This is not ideal solution but this is what we have at hand. Use this if you want to modify the html in more detail.

Wordpress : ShortCode and variable

I'm trying to get a value from a ShortCode into a variable for use it in my template file. How can i do that?
Here is my code :
In the post, the short code :
[reference_prix]1-214eddz[/reference_prix]
My plugin code :
$bl_reference_prix = "";
add_shortcode('reference_prix', 'get_blref_prix');
function get_blref_prix( $atts, $reference = null ) {
global $bl_reference_prix;
$bl_reference_prix = $reference;
}
But $bl_reference_prix is still empty.
I've try with $GLOBAL[] but i've the same issu.
What is the best practice for get a value write by the user in a wordpress post and display (or use it) in the template file?
I think the best practice is to use the atts parameter.
// Add Shortcode
function get_blref_prix( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'bl_reference_prix' => '',
), $atts )
);
}
add_shortcode( 'reference_prix', 'get_blref_prix' );
The user of the shortcode will just have to do the following in the editor:
[reference_prix bl_reference_prix="some value by the user"]
And then maybe you can try using the Options API. Add and delete after use.
I've do this and it's working now as :
//Plugin
function get_blref_prix( $atts ) {
global $bl_plugin_refprix, $bl_plugin_refprix_up;
// Attributes
extract( shortcode_atts(
array(
'reference' => '',
'up' => '',
), $atts )
);
$bl_plugin_refprix = $reference;
$bl_plugin_refprix_up = $up;
}
add_shortcode( 'bl_refprix', 'get_blref_prix' );
In the template file (Important : After the function "the_content"!) :
while(have_posts()):the_post();
echo the_content();
endwhile;
echo $bl_plugin_refprix;
In the Post :
[bl_refprix reference="123" up="456"]

Shortcode for button with custom style

I have already added an link in my widget
eg:
<a target="_blank" href="google.com" class="a-button">Learn More</a>
I need an shortcode like this
[button link="google.com" value="Learn More"]
If i paste this shortcode in widget, page and post, link must appear
Style must be same as above tag
Current Code:
function button_shortcode($atts, $content = null) {
extract( shortcode_atts( array( 'url' => '#' ), $atts ) );
return '' . do_shortcode($content) . '';
}
add_shortcode('button', 'button_shortcode');
How can i do this ?
Basic shortcode will look like this:
function a_button_shortcode( $atts, $content = null ) {
extract($atts);
return '<a target="_blank" href="' . esc_attr($link) . '" class="a-button">' . esc_attr($value) . '</a>';
}
add_shortcode( 'button', 'a_button_shortcode' );
You can read more on shortcode api on: http://codex.wordpress.org/Shortcode_API
In order to make your widget with shortcodes should use do_shortcode( $content ) function inside update method of your widget.
Like this:
function update( $old_instance, $new_instance) {
$new_instance['content'] = do_shortcode($new_instance['content']);
return $new_instance;
}
Or use a plugin that will make it for default widgets like this https://wordpress.org/plugins/shortcodes-in-sidebar-widgets/
add_shortcode("init" ,"add_custom_shortcode");
function add_custom_shortcode()
{
add_shortcode('button', 'buttonShortcode');
}
function buttonShortcode($atts, $text='') {
extract( shortcode_atts( array( 'url' => '#' ), $atts ) );
return '' . do_shortcode($text) . '';
}
thank you all, i tried out this one
function button_shortcode($atts, $content = null) {
extract( shortcode_atts( array(
'url' => '#'
), $atts ) );
return '' . do_shortcode($content) . '';
}
add_shortcode('button', 'button_shortcode');\
For shortcode support in widget paste below line in functions.php
add_filter('widget_text', 'do_shortcode');
created shortcode:
[button url="google.com"]Download[/button]

Resources