Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Shazzad Hossain Khan

    (@sajib1223)

    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.

    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.