Very odd issue I am having. I add the following code to just add a test shortcode to my Wordpress functions.php.
//TEST SHORTCODE
add_shortcode( 'sc_brandon_test', 'brandon_test' );
function brandon_test(){
echo 'Brandon\'s Test Shortcode file works correctly!';
}
When using the shortcode [sc_brandon_test] in the editor the page will auto-redirect to a broken preview of the page I am trying to edit.
Any thoughts would be very appreciated.
Usually you don't echo your shortcode output directly but use a return.
echo would output your content immediately.
The desired behaviour is to parse the shortcode output within your main content loop e.g calling the_content() or
apply_filters('the_content', get_post_field('post_content', $post_id));
add_shortcode( 'sc_brandon_test', 'brandon_test' );
function brandon_test(){
$output = 'Brandon\'s Test Shortcode file works correctly!';
return $output;
}
I recommend these guides
WordPress Scholar: WordPress Shortcodes
smashing magazine: WordPress Shortcodes: A Complete Guide
Related
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.
I'm trying to add a shortcode that will be placed on all single product posts. Specifically on the product's short description. I went to woocommerce templates and found a file "short-description" and I tried placed following code, but it's not working:
<?php echo do_shortcode("[wpqr-code]"); ?>
This shortcode is to supposed to generate and display a qr code on each product post where it is placed.
I found a solution if anyone needs. Place this code in functions.phpfile:
add_action( 'woocommerce_before_add_to_cart_form', 'enfold_customization_extra_product_content', 15 );
function enfold_customization_extra_product_content() {
echo do_shortcode("[wpqr-code]");
}
I would like to target the single blog post template in the WordPress file functions.php.
I have the following code:
if( is_page_template( 'single.php' ) ){
} else {
wp_deregister_script('jquery');
}
Some reason this does not target the single blog post template. I know that this code works because it works when I put another template in there instead of single.php.
Why won't single.php get targeted in this?
i found the solution
all i had to do was change is_page_template to is_single
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
I develop a new theme for wordpress 3.3.1 from scratch and shortcodes are not working on it.
As I searched until now it is a matter of filtering the content containing the shortcode, filter code added in a theme specific location(shorcodes are working with another theme).
So, my question is : What is the code for a general shortcode theme enable ?
To execute a single shortcode, run it with
echo do_shortcode('[your_short_code]');
If the shortcode(s) are in the post content, make sure you're displaying it with
<?php the_content();?>
Or
<?php echo apply_filters('the_content',$post_content);?>
Or
<?php echo apply_filters('the_content',$wp_query->post->post_content);?>
The important thing is: if you aren't using the function "the_content()" you need this line <?php echo apply_filters('the_content',$wp_query->post->post_content);?> where in the second argument you have to put the variable of the post content you want to show.
I had to save the theme's content to a variable and then use the second example.. Worked like a charm.
$mycontent = ot_get_option('rightcontent');
echo apply_filters('the_content',$mycontent);