smzapp
smzapp

Reputation: 829

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

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.

Upvotes: 5

Views: 16197

Answers (1)

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

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.

Upvotes: 7

Related Questions