Turn a snippet of HTML and PHP into a shortcode

When you want to turn a snippet into a shortcode, you have to return a string. You cannot use echo or print anything in any other way. Also, always escape your data, and check if there are actually values. I have used the HEREDOC syntax here, because it is easy to read: add_shortcode( ‘taobao’, ‘taobao_shortcode’ … Read more

Shortcode to insert menu in page body?

That code should work. Are you usign “myclass” as the class and not “.myclass”? Is this for a specific use where class will always be the same? If you’re only looking to ever use this on one place, you can do this: function print_menu_shortcode($atts, $content = null) { extract(shortcode_atts(array( ‘name’ => null, ‘class’ => null … Read more

How to display the comment_form with a shortcode while removing it from its default position?

Version #1 The following seems to work for the Twenty Fifteen theme: /** * Display the comment form via shortcode on singular pages * Remove the default comment form. * Hide the unwanted “Comments are closed” message with CSS. * * @see http://wordpress.stackexchange.com/a/177289/26350 */ add_shortcode( ‘wpse_comment_form’, function( $atts = array(), $content=”” ) { if( is_singular() … Read more

Dynamic HTML not displaying at respective place

You’re getting the output there because dynamic_sidebar() displays/echoes the output (widgets in the specific sidebar), so the output is echoed right when the shortcode function is called, which is before the $data is actually echoed. And you can fix that using output buffering: Either append the output to $data: $data.='</div>’; $data.='<div class=”col-xl-4 col-lg-4 col-md-4 col-sm-12 … Read more

Add/echo div with Analytics-Code to function in functions.php

echo (‘<div onClick=”ga(‘send’, ‘event’, ‘will-ich-haben’, ‘klick’, ‘test-klick’);”>’); I haven’t looked any further but as a first step, this string isn’t concatenated correctly. echo “<div onClick=’ga(‘send’, ‘event’, ‘will-ich-haben’, ‘klick’, ‘test-klick’)’></div>”;

How to protect own PHP code from WordPress updates

NEVER EVER alter any template or file in a theme or plugin that you did not author, and this goes for any core file as well. There is no way to protect the code that you alter or add in any of those files, except maybe changing file permissions, but then again, you will run … Read more