Wordpress plugin shortscodes in other plugins? - wordpress

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

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.

Is there some way to insert custom archive loop to any theme from plugin?

I'm coding plugin that create custom post type with custom fields. Now I need to build custom archive/category/tag templates which should contain custom fields.
The problem is that I couldn't insert template part inside archive loop from plugin.
Is there some hook to insert custom loop inside activated theme? Something like this:
add_filter('the_content', 'change_content_in_single_post');
Now I'm using this hook:
add_filter( 'template_include', 'change_whole_cpt_archive_template' );
... but when I use template_include hook it changes whole template and I need to do something like the_content filter. That hook get template of current theme and replace standart content template with my custom template - make it part of activated theme. It makes my CPT single page template compatible with any wp theme. Now I need to replace standart archive loop with my custom loop. For example: on archive page show posts without images... It must be something that replace result of standard get_template_part() function with custom template from plugin. That's what I`m searching for...
Anyway, maybe you know some better ways to make plugin (archive template) compatible with wp themes?
Huge thanks for any help!
you need to introduce some logic to only run on your cpt archive page.
add_filter( 'template_include', 'change_all_archive_template' );
function change_all_archive_template($template){
if ( is_post_type_archive('cptname') ) {
$template= 'find the template!';
//if( file_exists ) --> look in theme 1st??
//else --> load from plugin..
}
return $template;
}
There are quite a few plugins that use this technique to look in the active theme 1st and if not found, find the file in your plugin.
I found solution in woocommerce plugin files.
So... the best way to build custom archive template from plugin is to use template_include hook and in custom template file set header and footer from activated theme:
<?php get_header();
// custom archive loop
get_footer(); ?>
To make it more compatible with any wp themes use stanard wordpress and bootstrap classes.

How to use wordpress shortcode in template

I need help with wordpress shortcode. I have written shortcode function in functions.php in my theme files. Now I create page in wordpress administration and then I need to place this shortcode into special place in my theme but with content which I wrote in administration.
I hope that you understand what I mean.
Thank you for your help.
In your template code:
<?php echo do_shortcode('[your-shortcode]'); ?>
Replace [your-shortcode] with whatever your shortcode is, including parameters/attributes and/or closing tags if required.

how to use wordpress "Post Views Count" plugin?

http://wordpress.org/extend/plugins/baw-post-views-count/screenshots/
i'm installed and active "Post Views Count" plugin but when i'm use shortcode [post_view] like this, it's doesn't woek....
<li><em>[most_view]</em></li>
OR:
<li><em><?php [most_view] ?></em></li>
[most_view] is short code and must place in post content if you want to put post counts in your template use this:
<?php bawpvc_views_sc(); ?>
Did you activate the plugin?
[most_view] must be placed in a post or page via the text-editor of the admin. Looks like you are making a template?
If you would like to use shortcodes in your PHP code, you should check the Wordpress' do_shortcode() function: https://developer.wordpress.org/reference/functions/do_shortcode/
Example using the shortcode you provided:
<li><em><?php echo do_shortcode( '[most_view]' ); ?></em></li>

Resources