Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert PHP script into a wordpress without using a plugin

Tags:

wordpress

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.

like image 987
smzapp Avatar asked Mar 10 '23 02:03

smzapp


1 Answers

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.

like image 183
Ahmed Ginani Avatar answered Mar 11 '23 15:03

Ahmed Ginani