Wordpress shortcodes working in php but not in text editor - wordpress

I seem to be having a problem outputting shortcodes from the wordpress text editor. The shortcodes work fine when echoing from php but it is only in the text editor they simply produce what is written.
I have added the following lines to my functions.php:
add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');
But this has no effect. This is happening across multiple plugins. Does anyone know why this might be happening?
Thanks!

So I've solved this. I figured out that this was only happening on custom templates. I then queried that issue and found that you have to use the_content() function to extract the content within your custom template and have shortcodes work. This is because the_content() parses the content in a manner that will make your shortcodes work.

Related

Shortcode not working in custom wordpress theme

I installed a Plugin in a wordpress site which uses some shortcode (like metaslider). But when I insert the shortcode in the page it is not displayed the content of the shortcode but only the string [shortcode].
Have I to insert something in the function.php file of the theme I use? (it is a custom theme)
Thanks in advance
If only the string [shortcode] is shown instead of the actual plugin output, it seems that the output of your pages does not use the do_shortcode filter properly. Without exactly knowing what's happening, try the following:
function filter_content_example($content) {
$content = do_shortcode($content);
return $content;
}
add_filter('the_content', 'filter_content_example');
This is only a first guess. Without further investigation i could not solve that problem.

Woocommerce best_selling_products shortcode not working

Trying to add:
<?php echo do_shortcode('[products best_selling="true" limit="4"]'); ?>
In a template returns nothing. I see that no hooks are present for this but woocommerce says it should be supported.
Please Try this shortcode
[best_selling_products per_page="12"]
Please refer below link for Woocommerce Shortcodes
https://docs.woocommerce.com/document/woocommerce-shortcodes/
Coming back to this one because I came across another shortcode that wasn't working.
It turned out that the theme was the culprit. After switching to the default theme all shortcodes were working correctly.

Where can WordPress (self-installed) shortcodes be rendered?

I've looked around for this but can't seem to find a definite answer. I want to know where WordPress shortcodes are supported within the (self-hosted) platform - meaning where can I safely place shortcode content and expect it to be rendered? I know I can use them in post and page content, and some widgets that output something. But can I use them in other plugins (that also output something), and which widgets are supported? Do custom plugins need to have something enabled that allows them to render shortcode content?
This page says:
By default, WordPress does not support shortcodes within Sidebar Widgets. It only expands the shortcodes within the content of a Post, Page, or custom post type.
... although I've gotten shortcodes to work in the arbitrary text widget, so that information doesn't seem accurate. It also suggests I install this plugin that hasn't been updated in years.
Is there any clarification somewhere on this that I've missed?
You can use:
<?php do_shortcode('name_of_shortcode'); ?>
e.g echo do_shortcode('[gallery autoplay=no]');
and it will render this shortcode. You can place it in functions.php, header.php, footer.php.
EDIT:
If you want it to work in Text Widget, all you need is to add this line of code in functions.php file:
add_filter('widget_text', 'do_shortcode');

Wordpress plugin shortscodes in other plugins?

I'm developing a plugin that creates custom wordpress pages, however I ran into a problem when trying to get shortcodes from other plugins to integrate into my plugin, well more like I couldn't find any info on it.
For example if someone uses my custom page builder and adds a short code [gallery] that is associated with an enabled plugin, I want the shortcode to do it's thing.
Could anyone point me in the right direction for this?
Try wordpress
http://wordpress.org/plugins/synved-shortcodes/
while this plugin let's you integrate shortcodes on every wordpress page , I suppose that you issue is with wordpress functions.php file where you can have a look at , with this plugin installed !
You can check if a shortcode exist before printing it with the function shortcode_exists() (https://codex.wordpress.org/Function_Reference/shortcode_exists)
Example:
<?php
if ( shortcode_exists( 'gallery' ) ) {
// The [gallery] short code exists.
}
?>
Then, if it exist and you want to print that shortcode with PHP code you should use do_shortcode() function (https://developer.wordpress.org/reference/functions/do_shortcode/)
Example:
<?php echo do_shortcode('[gallery]); ?>
Always use in WordPress's in-built
the_content
hook to print your custom page content.
WordPress by default process all shortcodes before outputting any stuff from this hook.
More info can be found here: https://codex.wordpress.org/Function_Reference/the_content

Wordpress shortcode not working in Retro theme

I am using wordpress retro theme, and using a plugin to show hide text "[wpex Read more]hidden text[/wpex]" but it is not working, data is displaying by custom function i went there and change the code to "retun do_shortcode( $result );" but is working for simple shortcode without parameters, not working with shortcode have partmeters like i mentioned before.
can anyone guide me what to do ?
Thank you very much
You missed the parameter in shortcode, assign the Read More text to a parameter like this
[wpex parameter="Read more"]hidden text[/wpex]

Resources