Title: PHP Code per Shortcode
Last modified: August 20, 2020

---

# PHP Code per Shortcode

 *  Resolved [Neueruser](https://wordpress.org/support/users/neueruser/)
 * (@neueruser)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-code-per-shortcode/)
 * At the moment I edited the Template file and added the PHP Code there. It works
   fine, but if I want to change some text, I have to edit the Template File…
    How
   can I use a Shortcode What PHP Code in the functions.php I have to ad, that I
   can use? The two blocks of code I`m using at the moment in the Templatefile:
 *     ```
        <?php 		
       	$argsevent = array(	
       		'timespan'	=> 10000,
       		'date_format_1'	=> 'd. F Y',
       		'date_format_2'	=> 'd. F Y',
   
       		'html_list_v1'	=> '%DATE%: <a href="%URL%">%TITLE%</a>',
       		'html_list_v2'	=> '<a href="%URL%"><span style="color: #DDD;">%DATE% </span> %TITLE%</a>',
       		'html_list_v3'	=> '%DATE% bis %ENDDATE%: <a href="%URL%">%TITLE%</a>',
       		'html_list_v4'	=> '<a href="%URL%">%DATE% (multiday!!!)%TITLE%</a>,
       		'max_events'	=> 0,
       		);
   
       	rs_event_list($argsevent); 
       ?>
       ```
   
 * and
 *     ```
          <?php query_posts('category_name=featured'); ?>
       			<?php
       				// Start the Loop.
       				while ( have_posts() ) : the_post();
   
       					// Include the page content template.
       					get_template_part( 'content', 'page' );
   
   
       				endwhile;
       			?>
       ```
   
    -  This topic was modified 5 years, 9 months ago by [Neueruser](https://wordpress.org/support/users/neueruser/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fphp-code-per-shortcode%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-code-per-shortcode/#post-13289603)
 * Please refer to the Shortcodes section of the Plugin Handbook:
    [https://developer.wordpress.org/plugins/shortcodes/](https://developer.wordpress.org/plugins/shortcodes/)
 * Of note is that shortcode handlers must not generate output. All output needs
   to be collected into a single variable which the handler then returns to the 
   caller. The caller is what actually echoes out the returned value. Template parts
   generate output. If your handler gets template parts, you need to use PHP output
   buffering to capture any output. You can then return the buffer contents to the
   caller.
 *  Thread Starter [Neueruser](https://wordpress.org/support/users/neueruser/)
 * (@neueruser)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-code-per-shortcode/#post-13293462)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/) Thanks for your help!
   
   Unfortunately I dont understand much of the technical details and thought I could
   do it somehow like this:
 *     ```
       function event-list_shortcode( $atts ) {
         return <<<event-list
   
       <?php 		
       	$argsevent = array(	
       		'timespan'	=> 10000,
       		'date_format_1'	=> 'd. F Y',
       		'date_format_2'	=> 'd. F Y',
   
       		'html_list_v1'	=> '%DATE%: <a href="%URL%">%TITLE%</a>',
       		'html_list_v2'	=> '<a href="%URL%"><span style="color: #DDD;">%DATE% </span> %TITLE%</a>',
       		'html_list_v3'	=> '%DATE% bis %ENDDATE%: <a href="%URL%">%TITLE%</a>',
       		'html_list_v4'	=> '<a href="%URL%">%DATE% (multiday!!!)%TITLE%</a>,
       		'max_events'	=> 0,
       		);
   
       	rs_event_list($argsevent); 
       ?>
       add_shortcode( 'event-list', 'event-list_shortcode' );
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-code-per-shortcode/#post-13294416)
 * You cannot nest <?php ?> blocks. Everything in a heredoc (the `<<<` syntax) block
   is a string literal, any function calls are treated as strings and are not executed.
   Clever thinking though, I like it even though it doesn’t work 🙂
 * If `rs_event_list()` generates output, your only option is to use output buffering.
   
   [https://www.php.net/manual/en/ref.outcontrol.php](https://www.php.net/manual/en/ref.outcontrol.php)
 *  Thread Starter [Neueruser](https://wordpress.org/support/users/neueruser/)
 * (@neueruser)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-code-per-shortcode/#post-13294466)
 * Thanks [@bcworkz](https://wordpress.org/support/users/bcworkz/) ! So I see that
   it’s not working 😭
    Other idea: Can I “tell” the Template File to execute the
   PHP code (what it’s actually already doing) and then call a function “load the
   content of Gutenberg”?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-code-per-shortcode/#post-13297746)
 * If you edited the template itself it can pretty much do whatever you want. Some
   things may be much more difficult than others. What’s difficult on a template
   is inserting custom content in the middle of post content. That’s where shortcodes
   come in handy. If you want something to appear before or after content, find 
   where the content is output on the template and add your code before or after.
 * Some of the functions called on a template might fire off action or filter hooks.
   You can use those to output content. Some templates might even have their own
   actions or filters that are are fired off right on the template.
 * A template would typically output content from Gutenberg already. Why would you
   want to tell it to do so again? Or is the Gutenberg content you want coming from
   somewhere else besides the current post? If so, where? The “featured” category
   posts?
 *  Thread Starter [Neueruser](https://wordpress.org/support/users/neueruser/)
 * (@neueruser)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-code-per-shortcode/#post-13300812)
 * Thank You very much for your time to answer my questions.
    I found a plugin that
   does insert the code for me: “xyz code snippets”.
 * Thanks once more!!!

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

The topic ‘PHP Code per Shortcode’ is closed to new replies.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 2 participants
 * Last reply from: [Neueruser](https://wordpress.org/support/users/neueruser/)
 * Last activity: [5 years, 9 months ago](https://wordpress.org/support/topic/php-code-per-shortcode/#post-13300812)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
