Using wordpress plugin, I can easily insert php script into the back-end of wp-admin. But, this time, I don't want to use plugin. Is there a way to do this? I already searched on google but haven't found any answer to this. Most of those sites uses plugin.
1 Answer
There is two way to do this.
If you want to use PHP code in your widget text editor then you can do that by adding below code in your function.php file.
function php_execute($html){
if(strpos($html,"<"."?php")!==false){ ob_start(); eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
add_filter('widget_text','php_execute',100);
Clear cache if you are using any cache plugin and then try to add PHP code to your widget area like:
<?php echo 'Hello!!!' ?>
If you want to add PHP code in your page or post editor then the best way is to create custom shortcode in function.php file something like:
function list_pages_function( $atts ) {
return wp_list_pages('sort_column=menu_order');
}
add_shortcode( 'output_pages', 'list_pages_function' );
then you can use [output_pages] as shortcode in your page or post editor. See the shortcode API.
Hope you will find this helpful for you.
Thanks.
3 Comments
Ahmed Ginani
@smzvax, Just Add "php_execute" function in your function.php then go to Appearance->Widget select Text widget and add <?php echo 'Hello!' ?> code inside. It's working fine in my side for the widget. Check and if you have installed any plugin for that then disable it first.
smzapp
Ah okay.thanks., How about adding PHP into the PAGES of wordpress?
Ahmed Ginani
For that, I think a good way is to add your PHP code in function.php file with add_shortcode function and then use that custom shortcode in your PAGES to run function and PHP script. Please see this link for the tutorial: webdesignerdepot.com/2013/06/… or you can generate custom shortcode function using shortcode generator: codex.wordpress.org/Function_Reference/add_shortcode and use that shortcode also.