short code not working in wordpress text editor - php

I wanted to add shortcode from the text editor of wordpress post.I have added following short code in the post of wordpress:
<img alt="" src="[template_url]/images/ic_sol_1a.png" />
Here [template_url] is the shortcode i wanted to use it was not working. when i see it in the post page it render the text not the short code response. Somewhere i saw a solutions like adding following line to functions.php of theme:
add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');
But still after adding these lines i am unable to make shortcode work. What could be the possible reason?
my shortcode function is like this:
function my_template_url() {
return get_bloginfo('template_url');
}
add_shortcode("template_url", "my_template_url");

You will need to use the get_bloginfo() to return the value as in your example and make sure to use the_content() to print the value to the page. If you use one of the API methods such as get_the_content() then the do_shortcode filter is not run. If you want to apply it use the following:
function my_template_url() {
// This will echo the 'template_url'
return get_bloginfo('template_url');
}
add_shortcode("template_url", "my_template_url");
$shortcoded = apply_filters( 'the_content', get_the_content() );
You do not need the add_filter() lines.

After lots of headache and with the help of #doublesharp and #Nathan Dawson comments/answers we figured out Problem was inside single.php file of theme we were getting the content of post using get_the_content function. By changing it to the_content() function sortcodes start working. Please note that shortcode will not work in the visual editor of WordPress post on admin site but when you see the post actually in the browser by going to that post you can see it working.

Related

Wordpress shortcodes not rendering

I'm attempting to add a shortcode wordpress sites using a custom plugin. No matter how I try to register or call this, the shortcode does not seem to render.
I am using the default 2020 theme from Wordpress to test this.
This is the code that adds the shortcode:
add_shortcode(
'test',
function () {
return 'test output';
}
);
If I manually check this on the template:
var_dump(shortcode_exists('test'));
the following is generated on a page load:
boolean true
However, if the following is added to the page content, it does not load:
[test]
I have also added the following to the theme functions.php
add_filter( 'widget_text', 'do_shortcode' );
However, as this is not within a widget (just post text) I did this to eliminate this possibility. It did not help.
Edit:
I also added the following to the page:
do_shortcode(['test']);
This produces the required output.
Every single tutorial does the above, pretty much verbatim.
What am I missing?
To be clear: Using [test] in content to show the shortcode inside post body content is the required behaviour.
Try to change the test name, suddenly it is reserved, and secondly, print the entire shortcode
print do_shortcode('[mytest]'); // [test]
<?php
/*
Plugin Name: Test
Description: La la la fa
Version: 0.1.0
*/
function question($attrs, $content = null) {
return '???';
}
add_shortcode('question', 'question');
add_action('init', function() {
d(do_shortcode('Are you sure [question]'));
});
Methods to output content with handling of shortcodes
the_content();
get_the_content();
apply_filters('the_content', $post->post_content);
Example:
add_action('init', function() {
d(apply_filters('the_content', 'Are you sure [question]')); // $post->post_content
});
This was a custom testing template, and the content of a post is not automatically parsed.
I've actually created a huge amount of custom meta data per post that I was arranging and presenting outside of the content, so I had used the $post->post_content property.
To be clear... $post->post_content is not parsed, the_content() is.
Things now work.
This is a good gotcha.

what is "do_shortcode" in wordpress and how it works?

I'm currently learning wordpress theme development and for better understanding I have downloaded couple of wordpress theme and seeing their code.
But a line of code I can't understand. here it is-->
<?php echo do_shortcode(sq_option( 'footer_text ' ' ')); ?>
maybe its for footer but how this works and how i can use this function when I will create my own.
do_shortcode is a function you have to use if you want to execute a shortcode inside a custom theme template page see more info on the function here: https://developer.wordpress.org/reference/functions/do_shortcode/
Generally do_shortcode(); is use to get data from shortcode or its attribute to your PHP code or to filter any shortcode returning data. Shortcode can be provided by any plugin.
Not sure about it but in your theme's code sq_option("footer_text"); could be a function which is filtering some text from footer.
The code could be as:
<?php echo do_shortcode( '[contact-form-7 id="91" title="quote"]' ); ?>
Reference Link
do_shortcode () is usefull function-> for excute shortcode in template file

WordPress shortcodes not working in posts

I am making a custom theme with shortcodes. The theme is ready, but now I need to make the shortcodes.
The problem is that the shortcodes don't work in my posts, they are only visible on my pages. When I add a shortcode in my post, the post displays nothing.
Here is the shortcode code:
function rss_subscribe_link() {
return 'Enjoy what you\'re reading? Subscribe to our RSS feed here now.'; }
add_shortcode('rss-subscribe', 'rss_subscribe_link');
add_filter('widget_text', 'do_shortcode');
Does anyone know how to solve this problem?
Greets,
Joren
This could happen if your post content is displayed via get_the_content() which is a function that returns the content without applying the default filters (wpautop, do_shortcode etc) that are applied normally when you use the_content(). Could you confirm if this is the case?

Line break not woking with Wordpress TinyMCE Advanced

I have a word document, with all my posts already write on it.
So I just have to copy/paste all these posts on my tinyMCE advanced Editor in my Wordpress 4.1
The only problem is that it doesn't take care about line break when I echo them in my blog page.. Whereas I can see these line break on tinyMCE visual preview. And if I check in the tinyMCE text mode, I don't see any <br /> or any <p> tags.
Does anyone had this problem ?
I have created my own template. And it is my first time on Wordpress so I could have miss something, but everyone seems to have a problem with remove_filter ('the_content', 'wpautop'); in their function.php But I don't have this function and I try with add_filter ('the_content', 'wpautop'); but it doesn't change anything.
Thank you in advance!
In your themes template where you output the_content try this:
$getPost = get_the_content();
$postformatted = wpautop( $getPost );
echo $postformatted;
You may want to remove the filter you added if it does not initially work.

Wordpress php widget

I'm using the wordpress php widget but I am unable to process Wordpress shortcode - the php just renders the shortcode instead of processing it. HEre is what I did
Added filters to my active theme function.php file
add_filter('widget_text', 'shortcode_unautop');
add_filter('widget_text', 'do_shortcode');
// Allow shortcodes in php code widget
add_filter('widget_execphp', 'shortcode_unautop');
add_filter('widget_execphp', 'do_shortcode');
Added the following to the php widget
<?php
$id = get_the_ID();
$amazon_product_asin_value = get_post_meta($id, 'amazon_product_asin', true);
echo do_shortcode('<div> [amazon asin=' . $amazon_product_asin_value . '&template=buynowamazon_widget&chan=default] </div>');
?>
I also tried without do_shortcode and same result.
2 properly outputs the shortcode of
[amazon asin=B008I20FT8&template=buynowamazon_widget&chan=default]
which works fine if I just enter this in the standard text widget
I'm using the Amazon Link plugin which generates the shortcodes
Any ideas?
The asin parameter value might be confusing the shortcode processor; try putting quotes around it.
echo do_shortcode('<div> [amazon asin="' . $amazon_product_asin_value . '&template=buynowamazon_widget&chan=default"] </div>');
Alternatively, does the shortcode accept each argument to that parameter as a separate parameter?
echo do_shortcode('<div> [amazon asin="' . $amazon_product_asin_value . '" template="buynowamazon_widget" chan="default"] </div>');
Edit: Looking at the plugin's code, I don't think you need these lines:
add_filter('widget_execphp', 'shortcode_unautop');
add_filter('widget_execphp', 'do_shortcode');
They will try to process the shortcode, before the PHP is executed, and thus you won't get the shortcode correctly processed. Comment them out and see what the plugin does now.

Categories