Title: Converting PHP code to shortcode help
Last modified: August 31, 2016

---

# Converting PHP code to shortcode help

 *  Resolved [RoboPope](https://wordpress.org/support/users/robopope/)
 * (@robopope)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/converting-php-code-to-shortcode-help/)
 * Hi
    I have spent a few days googling and trying to covert the following working
   php code to a shortcode so it doesn’t get lost when the other two users update
   a page with the visual editor.
 *     ```
       <?php
         $page_links = get_posts(array(
           'category' => '25',
           'post_type' => 'page',
           'posts_per_page'=>-1,
           'post_status' => 'publish',
           'orderby' => 'title',
           'order' => 'ASC'
         ));
   
         foreach($page_links as $link){
           echo '
       <li><a>ID).'">'.$link->post_title.'</a></li>
       ';
         }
       ?>
       ```
   
 * I don’t think the issue is with the array but the output but as a cut and paste
   coder as its not my primary role its somewhat beyond me. If someone could point
   out what I am not getting that would be great
 * Thanks
 * Paul

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [10 years, 3 months ago](https://wordpress.org/support/topic/converting-php-code-to-shortcode-help/#post-7188404)
 * You can’t use echo in a shortcode. Either stuff all of the output into a string
   that the function returns or use ob_start() and return ob_end_flush()
 * You can look up those two functions in the PHP manuals. String stuffing:
 *     ```
       function my_shortcode($atts, $content) {
          $page_links = get_posts(array(
           'category' => '25',
           'post_type' => 'page',
           'posts_per_page'=>-1,
           'post_status' => 'publish',
           'orderby' => 'title',
           'order' => 'ASC'
         ));
         $ret = "<ul>";
         foreach($page_links as $link){
           $ret .= '<li><a>ID).'">'.$link->post_title.'</a></li>';
           }
          $ret .= "</ul>";
          return $ret;
       ```
   
 *  Thread Starter [RoboPope](https://wordpress.org/support/users/robopope/)
 * (@robopope)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/converting-php-code-to-shortcode-help/#post-7188421)
 * Thanks for the reply. I have tried to test this code in php rather than a short
   code declaration and I get.
 * Parse error: syntax error, unexpected ‘”’ in /homepages/blah/blah/blah/blah/runtime.
   php(42) : eval()’d code on line 16 it seems to be unhappy with the single ” in
   the ‘”>’ part of the statement
    ` foreach($page_links as $link){ $ret .= ‘<li
   ><a>ID).'”>’.$link->post_title.'</a></li>’;
 *  `
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [10 years, 3 months ago](https://wordpress.org/support/topic/converting-php-code-to-shortcode-help/#post-7188423)
 * I don’t guarantee that I riff out error free code. 🙂
 * There’s probably an extra quote or prime (or a missing one). The key thing is
   in how to do it, not the exact code.
 *  Thread Starter [RoboPope](https://wordpress.org/support/users/robopope/)
 * (@robopope)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/converting-php-code-to-shortcode-help/#post-7188424)
 * lol point taken. Hopefully it has pointed me in the right direction
 * Thank you 🙂
 *  Thread Starter [RoboPope](https://wordpress.org/support/users/robopope/)
 * (@robopope)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/converting-php-code-to-shortcode-help/#post-7188495)
 * Resolved. I ended up using ob_start(); and return ob_get_clean(); around an ammended
   verison of my code

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Converting PHP code to shortcode help’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [RoboPope](https://wordpress.org/support/users/robopope/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/converting-php-code-to-shortcode-help/#post-7188495)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
