Recently I was working with one of application and I have created shortcode for some functionality and I want to display shortcode data after the content of posts so I have place shortcode after the post content from admin editor.
But in frontend, shortcode data was displaying in top of content even I put shortcode anywhere in editor, It displays top of page so today I am going to share how I solve issue of it
You should check out this ShortCode in WordPress to learn everything about Shortcode in WordPress with examples
I have used ob_start and ob_get_clean function to clear the buffer and it starts working. That will return the content rather than echo it directly and that you have to do in the case of a WordPress shortcode
Solution
1 2 3 4 5 6 7 8 9 10 11 12 | if ( ! function_exists( 'change_background_color' ) ) { function change_background_color( $atts ) { ob_start(); 'id' => 'value', ), $atts ); // your desired code return ob_get_clean(); } } |
This works for me like a charm. Hopefully works for you also. Please share this article if this works for you.Solution: Shortcode moves to the top of the content issue.
You may also like:
Programmatically Insert Content in WordPress
SSL with WordPress
Create Own Widget in WordPress