Title: Running PHP code inside template
Last modified: August 30, 2016

---

# Running PHP code inside template

 *  [sarevoka](https://wordpress.org/support/users/sarevoka/)
 * (@sarevoka)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/running-php-code-inside-template/)
 * Hi,
 * I found that I cannot run php code inside the template, is there a way to do 
   that?
 * Thanks.
 * [https://wordpress.org/plugins/w4-post-list/](https://wordpress.org/plugins/w4-post-list/)

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

 *  Plugin Author [Shazzad Hossain Khan](https://wordpress.org/support/users/sajib1223/)
 * (@sajib1223)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/running-php-code-inside-template/#post-6376749)
 * We do not wanted to use `eval`, so php code execution is not possible inside **
   Template**.
 * However, there is alternatives. You can add a shortcode to solve your needs. 
   Shortcode return value can be dynamic.
 * To register a new shortcode for W4 Post list, use `w4pl/get_shortcodes` filters.
 *     ```
       add_filter('w4pl/get_shortcodes', 'w4pl_extra_shortcode', 30);
       function w4pl_extra_shortcode( $s ){
       	$s['myDynamicField'] = array(
       		'group' 	=> 'Post',
       		'callback' 	=> 'myDynamicFieldAction',
       		'desc' 		=> ''
       	);
       	return $s;
       }
       ```
   
    - **myDynamicField**: This is the name of the shortcode that you will use on**
      Template** field. Like `[myDynamicField]`
    - **group**: this is where it will be visible on template group. not important
    - **callback**: required. A php callback function name, Or a public static class
      method name. Which will be used to replace the content of the shortcode.
    - **desc**: textual description of the shortcode, it’s displayed on the Documentation
      page.
 * The following function is similar to WP’s basic shortcode callback function.
 *     ```
       function myDynamicFieldAction( $attrs, $content ){
       	$return = '';
       	$return .= 'Good ';
       	if( date('h') > 5 && date('h') < 11 ){
       		$return .= 'Morning';
       	}
       	elseif( date('h') < 17 ){
       		$return .= 'Afternoon';
       	}
       	elseif( date('h') < 20 ){
       		$return .= 'Evening';
       	}
       	elseif( date('h') < 20 ){
       		$return .= 'Night';
       	}
       	return $return;
       }
       ```
   
 * Hope this helps.
 *  [de_st](https://wordpress.org/support/users/de_st/)
 * (@de_st)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/running-php-code-inside-template/#post-6376870)
 * if you want to manage the output of for example the group title, you need to 
   call the w4pl-object via a third parameter in your function:
 *     ```
       function myDynamicFieldAction( $attrs, $content, $list ){
       	$string = "";
       	$title = "";
       	if(isset($list->current_group)){
       		$title=$list->current_group['title'];
       	}
       	else{
       			$title="";
       	}
       	$string = substr($title,-2).".".substr($title,5,2).".".substr($title,0,4);	
   
       	return $string;
       }
       ```
   

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

The topic ‘Running PHP code inside template’ is closed to new replies.

 * ![](https://ps.w.org/w4-post-list/assets/icon-128x128.png?rev=3034626)
 * [W4 Post List](https://wordpress.org/plugins/w4-post-list/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/w4-post-list/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/w4-post-list/)
 * [Active Topics](https://wordpress.org/support/plugin/w4-post-list/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/w4-post-list/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/w4-post-list/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [de_st](https://wordpress.org/support/users/de_st/)
 * Last activity: [10 years, 7 months ago](https://wordpress.org/support/topic/running-php-code-inside-template/#post-6376870)
 * Status: not resolved