正如您所言,在文本框中运行可执行PHP不是一个好主意。这不仅不是一个好主意,而且是一个糟糕的主意。这在您的安全性中创建了一个循环漏洞,很容易被黑客利用。
我建议您为这个功能编写自己的自定义小部件。在索引顶部添加自定义侧栏非常简单。php的特定用途,您只需将自定义小部件放入其中即可。看看register_sidebar()
. 关于如何在主题中添加自定义侧栏,还有很多关于这个特定主题的教程
至于小部件,请查看Widget API. 我(在@birgire;-)的帮助下)也做了recent widget which displays categories 您可以将其用作小部件的基础。您只需将所选服务中的自定义代码编码到widget
方法您可以使其非常动态。
这是您可以使用的基本骨架。要使其正常工作,只需更换// ADD YOUR CUSTOM PHP CODE HERE FOR EXECUTION TO DISPLAY ON FRONT END
使用您的自定义php代码widget()
方法(注意:需要PHP 5.4+)
class Custom_Services extends WP_Widget
{
public function __construct()
{
parent::__construct(
\'widget_custom_service\',
_x( \'Custom Service Widget\', \'Custom Service Widget\' ),
[ \'description\' => __( \'Displays information from a custom service.\' ) ]
);
$this->alt_option_name = \'widget_custom_service\';
add_action( \'save_post\', [$this, \'flush_widget_cache\'] );
add_action( \'deleted_post\', [$this, \'flush_widget_cache\'] );
add_action( \'switch_theme\', [$this, \'flush_widget_cache\'] );
}
public function widget( $args, $instance )
{
$cache = [];
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( \'widget_services\', \'widget\' );
}
if ( ! is_array( $cache ) ) {
$cache = [];
}
if ( ! isset( $args[\'widget_id\'] ) ) {
$args[\'widget_id\'] = $this->id;
}
if ( isset( $cache[ $args[\'widget_id\'] ] ) ) {
echo $cache[ $args[\'widget_id\'] ];
return;
}
ob_start();
$title = ( ! empty( $instance[\'title\'] ) ) ? $instance[\'title\'] : __( \'Category Posts\' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( \'widget_title\', $title, $instance, $this->id_base );
// ADD YOUR CUSTOM PHP CODE HERE FOR EXECUTION TO DISPLAY ON FRONT END
echo $args[\'after_widget\'];
if ( ! $this->is_preview() ) {
$cache[ $args[\'widget_id\'] ] = ob_get_flush();
wp_cache_set( \'widget_services\', $cache, \'widget\' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$this->flush_widget_cache();
$alloptions = wp_cache_get( \'alloptions\', \'options\' );
if ( isset($alloptions[\'widget_custom_service\']) )
delete_option(\'widget_custom_service\');
return $instance;
}
public function flush_widget_cache()
{
wp_cache_delete(\'widget_services\', \'widget\');
}
public function form( $instance )
{
$title = isset( $instance[\'title\'] ) ? esc_attr( $instance[\'title\'] ) : \'\';
?>
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<?php
}
}
add_action( \'widgets_init\', function ()
{
register_widget( \'Custom_Services\' );
});