Categoria: English
Well, in this article, I assume you have read the WP Engineer's excellent post Add WordPress Dashboard Widgets, because his code will be our start point.
So, we have this code:
// Load up the localization file if we're using WordPress in a different language // Place it in this plugin's folder and name it "MainFunction-[value in wp-config].mo" load_plugin_textdomain( 'MainFunction', '/wp-content/plugins/MainFunction' ); /** * Content of Dashboard-Widget */ function MainFunction() { echo 'Test Add Dashboard-Widget'; } /** * add Dashboard Widget via function wp_add_dashboard_widget() */ function MainFunction_Init() { wp_add_dashboard_widget( 'MainFunction', __( 'MainFunction Widget Title' ), 'MainFunction' ); } /** * use hook, to integrate new widget */ add_action('wp_dashboard_setup', 'MainFunction_Init');
OK, now, remenber the wp_add_dashboard_widget function?
function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null )
The $control_callback parameter (obviously optional) is our main target. It gives the 'Configure' option to our spiffy widget.
So, we need to add a 4th parameter to wp_add_dashboard_widget:
wp_add_dashboard_widget( 'MainFunction', __( 'MainFunction Widget Title' ), 'MainFunction', 'MainFunction_Setup');
The MainFunction_Setup function handles two things: the options (set the default options, recolect new values, etc) and the option's presentation (i.e. the HTML part)
So, here goes the code:
function MainFunction_Options() { $defaults = array( 'items' => 5, 'boolean' => 1); if ( ( !$options = get_option( 'MainFunction' ) ) || !is_array($options) ) $options = array(); return array_merge( $defaults, $options ); } function MainFunction_Setup() { $options = MainFunction_Options(); if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'MainFunction' == $_POST['widget_id'] ) { foreach ( array( 'items', 'boolean' ) as $key ) $options[$key] = $_POST[$key]; update_option( 'MainFunction', $options ); } ?> <label for="items"><PHP _e('How many items?', 'MainFunction' ); ?> <select id="items" name="items"> <PHP for ( $i = 5; $i <= 20; $i = $i + 1 ) echo "<option value='$i'" . ( $options['items'] == $i ? " selected='selected'" : '' ) . ">$i</option>"; ?> </select> </label> <label for="boolean"> <input id="boolean" name="boolean" type="checkbox" value="1"<PHP if ( 1 == $options['boolean'] ) echo ' checked="checked"'; ?> /> <PHP _e('Activate boolean?', 'MainFunction' ); ?> </label> <PHP }
As you can see, MainFunction_Options puts the default values (if necessary, of course) and MainFunction_Setup show the current values and let change them. You can merge this two, of course...
Now, we have to get the options and use them in our MainFunction function. Change MainFunction like that:
/** * Content of Dashboard-Widget */ function MainFunction() { $widget_options = MainFunction_Options(); echo 'Test Add Dashboard-Widget'; echo "You have selected $widget_options['items'] items"; echo "boolean is $widget_options['boolean']"; }
The complete code here:
<PHP // Load up the localization file if we're using WordPress in a different language // Place it in this plugin's folder and name it "MainFunction-[value in wp-config].mo" load_plugin_textdomain( 'MainFunction', '/wp-content/plugins/MainFunction' ); /** * Content of Dashboard-Widget */ function MainFunction() { $widget_options = MainFunction_Options(); echo 'Test Add Dashboard-Widget'; echo "You have selected $widget_options['items'] items"; echo "boolean is $widget_options['boolean']"; } /** * add Dashboard Widget via function wp_add_dashboard_widget() */ function MainFunction_Init() { wp_add_dashboard_widget( 'MainFunction', __( 'MainFunction Widget Title' ), 'MainFunction' , 'MainFunction_Setup' ); } function MainFunction_Options() { $defaults = array( 'items' => 5, 'boolean' => 1); if ( ( !$options = get_option( 'MainFunction' ) ) || !is_array($options) ) $options = array(); return array_merge( $defaults, $options ); } function MainFunction_Setup() { $options = MainFunction_Options(); if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) && isset( $_POST['widget_id'] ) && 'MainFunction' == $_POST['widget_id'] ) { foreach ( array( 'items', 'boolean' ) as $key ) $options[$key] = $_POST[$key]; update_option( 'MainFunction', $options ); } ?> <label for="items"><PHP _e('How many items?', 'MainFunction' ); ?> <select id="items" name="items"> <PHP for ( $i = 5; $i <= 20; $i = $i + 1 ) echo "<option value='$i'" . ( $options['items'] == $i ? " selected='selected'" : '' ) . ">$i</option>"; ?> </select> </label> <label for="boolean"> <input id="boolean" name="boolean" type="checkbox" value="1"<PHP if ( 1 == $options['boolean'] ) echo ' checked="checked"'; ?> /> <PHP _e('Activate boolean?', 'MainFunction' ); ?> </label> <PHP } /** * use hook, to integrate new widget */ add_action('wp_dashboard_setup', 'MainFunction_Init'); ?>
You can view a PHPS version (without PHP crippled tags) here.
i18n
Always use i18n functions (like _e() and __()), even if you don't know more languages, so others can do the l10n job for you.
Templates
At least, a template for archives, author, links, search and 404 must be provided.
Alternate CSS file
In your header.php link your style.css and a alternate-style.css in order to allow users to add personal CSS code without fear to loose it in the next theme upgrade.
Supported plugins
Add support to 3rd parties plugins is good. But always check if they exists using function_exists() function to avoid PHP errors.
Time and Date format
Don't use your own time/date format with the_date() and the_time() functions. Use get_option() function, like the_date(get_option('date_format')) and the_time(get_option('time_format')) instead. And remenber, if you call the_date() function twice or more in one page, only the first will echo the date. the_time() function don't have this behavior.
Lightweight
Use WordPress own resources if is possible: i.e. don't add jquery.js, use wp_enqueue_script() function, like wp_enqueue_script('jquery'), and so on...
Widgets
Even if widgets are not your thing (like me), make your theme widget-ready. It's so easy, and sooner or later someone will ask you to do it.
Ils sont idiots! Les filles voient l'écriture sur le mur, qui est que les gars vont voter les uns après les autres, parce qu'ils pensent que ce sera une équipe plus forte. Les h…
is there a google reader shared items rss 2.0 feed? or only an atom one?
Genial el artículo, ahora ya sé por donde empezar a echarle mano :)
Mira por donde he encontrado tu blog. Creo que seguimos coincidiendo en esto del castellano pero para nuestra desgracia existen unos cuantos elementos bastante radicales que no est…
Muy de acuerdo en lo de Twitter. Pero para mí el Buzz no es otra cosa que un Friendfeed que funciona mal.