-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayFileWidget.php
More file actions
92 lines (75 loc) · 3.73 KB
/
Copy pathDisplayFileWidget.php
File metadata and controls
92 lines (75 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/*
Plugin Name: Display File Widget
Plugin URI: https://github.com/asadaqain/display_file_widget
Version: 1.0
Description: Output contents of a file via url or local /path/to/file.
Author: Ali Sadaqain
Author URI: https://github.com/asadaqain/display_file_widget
*/
class Display_File_Widget extends WP_Widget
{
function Display_File_Widget()
{
$widget_ops = array('classname' => 'Display_File_Widget', 'description' => "Output contents of a file via url or local /path/to/file.");
$this->WP_Widget('Display_File_Widget', 'Display File Widget', $widget_ops);
}
/*
This is the settings form displayed in Appearance->Widgets settings.
If your adding any new fields, make sure to also add them to update function (see next function).
Otherwise your form will display but it will not save any values.
*/
function form($instance)
{
$instance = wp_parse_args((array) $instance, array( 'title' => '', 'file_url' => '', 'other_content' => '' ));
$title = $instance['title'];
$file_url = $instance['file_url'];
$other_content = $instance['other_content'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('file_url'); ?>">Url of hours html output: <input class="widefat" id="<?php echo $this->get_field_id('file_url'); ?>" name="<?php echo $this->get_field_name('file_url'); ?>" type="text" value="<?php echo attribute_escape($file_url); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('other_content'); ?>">Custom HTML Add-On: <textarea rows="4" cols="50" class="widefat" id="<?php echo $this->get_field_id('other_content'); ?>" name="<?php echo $this->get_field_name('other_content'); ?>" placeholder="Custom html or text here"><?php echo attribute_escape($other_content); ?></textarea></label></p>
<?php
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['file_url'] = $new_instance['file_url'];
$instance['other_content'] = $new_instance['other_content'];
return $instance;
}
function widget($args, $instance)
{
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
$file_url = empty($instance['file_url']) ? '' : $instance['file_url'];
$other_content = empty($instance['other_content']) ? '' : $instance['other_content'];
if(is_front_page()) {
if (!empty($title))
echo $before_title . $title . $after_title;;
if(!empty($file_url)) {
$content = file_get_contents($file_url);
if (!empty($content)) {
echo $content;
if (!empty($other_content))
echo $other_content;
}else
echo "Widget could not read file contents. Please check the url in Appearance->Widgets ";
echo $after_widget;
}else {
echo "Please set the file with html output of the hours.";
}
} // front page check close
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("Display_File_Widget");') );
/*
Just in case you wish to override css of the html or content your loading, you can do so via widget stylesheet.
*/
function display_file_widget_enqueue_stylesheet() {
wp_enqueue_style( 'display-file-stylesheet', plugins_url( 'stylesheet.css', __FILE__ ) );
}
add_action( 'wp_enqueue_scripts', 'display_file_widget_enqueue_stylesheet' );
?>