{"id":3393249,"date":"2013-01-23T16:01:23","date_gmt":"2013-01-23T16:01:23","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/shortcode-function-replacing-echo-with-return-need-help\/"},"modified":"2016-08-20T20:52:51","modified_gmt":"2016-08-20T20:52:51","slug":"shortcode-function-replacing-echo-with-return-need-help","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/shortcode-function-replacing-echo-with-return-need-help\/","title":{"rendered":"Shortcode function &#8211; replacing echo with return &#8211; need help"},"content":{"rendered":"<p>Hello,<br \/>\n<em>( I am beginer with creating shortcodes &#8230; and still learning php javascript etc&#8230;)<\/em><\/p>\n<p>I managed to make a simple shortcode, and it does what I need, but &#8230;<\/p>\n<p>I recently foound out that I should use &#8220;return&#8221; instead of &#8220;echo&#8221; if I want to place the shortcode output <strong>inside the content<\/strong> of the post and <strong>not allways on top<\/strong>.<br \/>\nI went through the code of my function, replaced echo with return, but it doesn&#8217;t work (nothing gets returned at all&#8230;)<br \/>\nI must be missing something, some difference between echo and return, I guess I can not replace it as simple as that &#8230;<br \/>\nHere is the code I put in my functions.php :<\/p>\n<pre><code>&lt;?php\n\/**\n* MY SHORTCODE\n**\/\nfunction display_images_in_list($atts) {\n\n\textract(shortcode_atts(array(\n      &#039;velicina&#039; =&gt; &#039;thumbnail&#039;,\n   ), $atts));\n\necho &quot;&lt;div class=&#039;slideshow pics&#039;&gt;&quot;;\n\n\tif($images = get_posts(array(\n\t\t&#039;post_parent&#039;    =&gt; get_the_ID(),\n\t\t&#039;post_type&#039;      =&gt; &#039;attachment&#039;,\n\t\t&#039;numberposts&#039;    =&gt; -1, \/\/ show all\n\t\t&#039;post_status&#039;    =&gt; null,\n\t\t&#039;post_mime_type&#039; =&gt; &#039;image&#039;,\n                &#039;orderby&#039;        =&gt; &#039;menu_order&#039;,\n                &#039;order&#039;           =&gt; &#039;ASC&#039;,\n\t))) {\n\t\tforeach($images as $image) {\n\t\t\t$attimg = wp_get_attachment_image($image-&gt;ID,$velicina);\n\t\t\t$attimgpageurl = get_attachment_link($image-&gt;ID);\n\n   echo &quot;&lt;div class=&#039;slajd&#039;&gt;&lt;a href=&#039;&quot;.$attimgpageurl.&quot;&#039;&gt;&quot;.$attimg.&quot;&lt;\/a&gt;&lt;\/div&gt;&quot;;\n\n\t\t}\n\t}\n  echo &quot;&lt;\/div&gt;&lt;div id=&#039;nav&#039;&gt;&lt;\/div&gt;&quot;;\n}\n?&gt;\n&lt;?php\nfunction register_shortcodes(){\n   add_shortcode(&#039;galerija-cycle&#039;, &#039;display_images_in_list&#039;);\n}\n?&gt;\n&lt;?php\nadd_action( &#039;init&#039;, &#039;register_shortcodes&#039;);\n?&gt;<\/code><\/pre>\n<p>It works <em>( for example this is how I used it:  <\/em>[galerija-cycle velicina=&#8221;galerija-thumb-2&#8243;]  <em>)<\/em><br \/>\nbut shortcode output is always on top of the other content,<br \/>\neven if it is placed inside text of the post.<br \/>\nSo I did this:<\/p>\n<pre><code>&lt;?php\n\/**\n* MY SHORTCODE\n**\/\nfunction display_images_in_list($atts) {\n\n\textract(shortcode_atts(array(\n      &#039;velicina&#039; =&gt; &#039;thumbnail&#039;,\n   ), $atts));\n\nreturn &#039;&lt;div class=&quot;slideshow pics&quot;&gt;&#039;;\n\n\tif($images = get_posts(array(\n\t\t&#039;post_parent&#039;    =&gt; get_the_ID(),\n\t\t&#039;post_type&#039;      =&gt; &#039;attachment&#039;,\n\t\t&#039;numberposts&#039;    =&gt; -1, \/\/ show all\n\t\t&#039;post_status&#039;    =&gt; null,\n\t\t&#039;post_mime_type&#039; =&gt; &#039;image&#039;,\n                &#039;orderby&#039;        =&gt; &#039;menu_order&#039;,\n                &#039;order&#039;           =&gt; &#039;ASC&#039;,\n\t))) {\n\t\tforeach($images as $image) {\n\t\t\t$attimg = wp_get_attachment_image($image-&gt;ID,$velicina);\n\t\t\t$attimgpageurl = get_attachment_link($image-&gt;ID);\n\n   return &#039;&lt;div class=&quot;slajd&quot;&gt;&lt;a href=&quot;&#039;.$attimgpageurl.&#039;&quot;&gt;&#039;.$attimg.&#039;&lt;\/a&gt;&lt;\/div&gt;&#039;;\n\n\t\t}\n\t}\n  return &#039;&lt;\/div&gt;&lt;div id=&quot;nav&quot;&gt;&lt;\/div&gt;&#039;;\n}\n?&gt;\n&lt;?php\nfunction register_shortcodes(){\n   add_shortcode(&#039;galerija-cycle&#039;, &#039;display_images_in_list&#039;);\n}\n?&gt;\n&lt;?php\nadd_action( &#039;init&#039;, &#039;register_shortcodes&#039;);\n?&gt;<\/code><\/pre>\n<p>As you can see I just replaced echo with return and changed &#8216; and &#8221;   &#8230;<br \/>\nIt doesnt work at all &#8211; shortcode output is mostly blank &#8230;<br \/>\nIt does a strange thing: text from post content is ok, it is before the shortcode, but after that the output of the shortcode is just:<br \/>\n&lt;div class=&#8221;slideshow pics&#8221;&gt;<br \/>\n..missing the images that should be here &#8211; instead it somehow wraps two divs from template after shortcode inside this div &#8230;<br \/>\n&lt;\/div&gt;<br \/>\nYou help is very much appreciated and needed.<br \/>\nThanks.<\/p>\n","protected":false},"template":"","class_list":["post-3393249","topic","type-topic","status-closed","hentry","topic-tag-shortcode"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/3393249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/3393249\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=3393249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}