Override the gallery shortcode (WordPress) if has a given parameter - wordpress

I want to "override" the default "gallery" shortcode (WordPress), but only if I have used a given parameter to that gallery shortcode.
For example:
[gallery ids="1,2,3"]
It has no parameter, so it will output the standard gallery code.
[gallery mode="custom" ids="1,2,3"]
It has my "mode" parameter, so it will output another shortcode.
To achieve it, I have created a "gallery" shortcode in functions.php file:
function get_new_gallery( $atts ) {
extract( shortcode_atts( array(
'mode' => '',
'ids' => '',
), $atts ) );
$code ="";
if ($mode == "custom") {
//* Output custom shortcode
$code = '[custom_gallery ids="' . $ids . '"]';
} else {
//* Need to do nothing...but don't know how to do it
$code = '[gallery ids="' . $ids . '"]'; /* Here's the problem, it causes a loop */
}
return do_shortcode($code);
}
add_shortcode( 'gallery', 'get_new_gallery' );
It works fine when I use the mode="custom" parameter. It just output the new shortcode: [custom_gallery...]
However, when not using the parameter it breaks because it enters in an infinite loop. In the code, there's a comment with the line that breaks it.
What I want is to execute the standard "gallery" shortcode if no parameter is entered. But given I've overwritten it...don't know how to "scape" from the loop and just execute the gallery.
Any help?
Thanks in advance.

Maybe an alternative approach can help? What about a filter on the gallery shortcode. See references:
https://codex.wordpress.org/Plugin_API/Filter_Reference/post_gallery
and:
https://wpbeaches.com/filtering-gallery-image-output-wordpress/

Related

how to create shortcode using post content for each post when the post is published

I want to create short code every time when post is published using that specific post content so that i have short codes for each different post, for this i wrote this code, but its not working, can anyone please help.
add_action('publish_adsmanager_banner','create_banner_shortcode');
function create_banner_shortcode()
{
add_shortcode( 'banner_'.get_the_ID().'', 'custom_shortcode' );
}
function custom_shortcode($post_id) {
$message = get_the_content($post_id);
return $message;
}
What if you will just pass an id as an attribute to this shortcode?
add_shortcode( 'my_banner', 'custom_shortcode' ); // [my_banner id="123"]
function custom_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'id' => null,
),
$atts
);
$message = get_the_content( $atts['id'] );
return $message;
}
In your original idea, you cannot create an id-named shortcode just once, it should be registered on each wp init. But in this case, it is hard to get a post context just from the shortcode name, you have to pass an id on each shortcode registration. The solution above will allow you to keep DRY especially if you know the ID of each banner when using them.

Modifying HTML of a specific [products] shortcode instance

The HTML output of [products] shortcode can be changed using hooks as outlined in /wp-content/plugins/woocommerce/templates/content-product.php.
Is it possible to have the hook code only affect a specific shortcode output?
You can use the do_shortcode_tag filter.
The do_shortcode_tag (introduced in WordPress 4.7) filter allows you to modify the output of a shortcode before it gets mixed in with a post’s content.
Example:
function modify_shortcode_output( $output, $tag ) {
if ( $tag === 'gallery' ) {
return $output."modified!";
}
return $output;
}
add_filter('do_shortcode_tag', 'modify_shortcode_output', 10, 2);

Use Wordpress shortcode function to render Gutenberg block, sending the attributes as parameters

I have a shortcode that generates a gallery, given the gallery ID.
function rb_scroll_gallery_shortcode( $atts, $content ) {
$a = shortcode_atts( array(
'id' => -1,
), $atts );
$gallery_ID = $a['id'];
$output = '';
if($gallery_ID != -1){
ob_start();
$gallery = new RB_Scroll_Gallery($gallery_ID);
$gallery->render();
$output = ob_get_clean();
}
return $output;
}
add_shortcode( 'rb_scroll_gallery', 'rb_scroll_gallery_shortcode' );
Now, I've made a Gutenberg block that works perfectly in the editor. You can select a gallery and it will save. However, I dont want to repeat code and have the html in the save property and in the php code.
So I was wondering if there is a way to use that same rb_scroll_gallery_shortcode function to render the block in the front end.
I've seen that you can use register_block_type and set the render_callback to rb_scroll_gallery_shortcode, but I need the ID selected in the block to send it to the function in the $atts array
//This uses the shortcode funtion, but doesn't gives the gallery ID
register_block_type( 'cgb/block-rb-scroll-gallery-block', array(
'render_callback' => 'rb_scroll_gallery_shortcode',
) );
You can try to Convert a Shortcode to a Gutenberg Block and after use in your theme,
Registering the Dynamic Block Callback in PHP
/**
* Register the GitHub Gist shortcode
*/
function gggb_init() {
add_shortcode( 'github-gist', 'gggb_render_shortcode' );
register_block_type( 'github-gist-gutenberg-block/github-gist', array(
'render_callback' => 'gggb_render_shortcode',
) );
}
add_action( 'init', 'gggb_init' );
When your block is rendered on the frontend, it will be processed by your render callback:
function gggb_render_shortcode( $atts ) {
if ( empty( $atts['url'] )
|| 'gist.github.com' !== parse_url( $atts['url'], PHP_URL_HOST ) ) {
return '';
}
return sprintf(
'<script src="%s"></script>',
esc_url( rtrim( $atts['url'], '/' ) . '.js' )
);
}
**Note:** this render callback is intentionally different than the Gutenberg block’s edit callback. Our preference is to use GitHub’s provided JavaScript embed code because this lets GitHub change the embed’s behavior at a future date without requiring a developer to make changes.
Refer link for get more information, https://pantheon.io/blog/how-convert-shortcode-gutenberg-block
I've found out the little thing that messed up my code. The problem wasn't that the render_callback() wasn't getting any attributes (though it wasn't), but it was that the editor wasn't saving them because I forgot to remove some testing data from the attribute galleryID
In the registerBlockType:
The save() method should return null.
The attribute should not have a selector data, since it is used to find the value on the markup return by the save(), wich in this case returns null. I've forgot to remove this data, thats why the attribute wasn't being saved.
attributes: {
galleryID: {
type: 'string',
//This data should only be set if the value can be found in the markup return by save().
//Since save() returns null, we let it out
/*source: 'attribute',
/*attribute: 'data-gallery-id',
/*selector: '.rb-scroll-gallery-holder',*/
},
}

Taxonomy - Display terms for post

I've created the custom shortcode to display terms of a custom taxonomy.
// First we create a function
function list_terms_forme_juridique_taxonomy( $atts ) {
// Inside the function we extract custom taxonomy parameter of our
shortcode
extract( shortcode_atts( array(
'custom_taxonomy' => 'forme_juridique',
),
$atts ) );
// arguments for function wp_list_categories
$args = array(
taxonomy => $custom_taxonomy,
title_li => ''
);
// We wrap it in unordered list
echo '<ul>';
echo wp_list_categories($args);
echo '</ul>';
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);
It works well, but the issue is that ALL the terms are displayed. I'd like to display only the terms corresponding to the post itself.
Any help ?
Maybe get_the_terms() will work for you - it returns the terms of the custom taxonomy that are attached to the post (you must have the ID of the current post).

how to call woocommerce product description in a post in wordpress

I want to display product description in a post. I have tried with this shortcode in my post:
[product_decrption id="1022"]
But nothing is showning in my post.Can you please help me. Is There any function needed to work with this?
As per my knowledge, I don't think there is in-built shortcode for that.
But you can definitely make your own one like this:
function rohils_product_description_function( $atts, $content=null ) {
extract(shortcode_atts( array('id' => ''), $atts));
$product_data = new WC_Product($id);
$content = $product_data->post->post_content;
return $content;
}
add_shortcode( 'product_decrption', 'rohils_product_description_function' );
And use is like : [product_decrption id="8"]

Resources