Categories
Wordpress

WYSIWYG Editor for your custom box

If you have a custom text box in the edit post page, and you want to have the built-in editor, it’s quite easy to do.
<?php
the_editor($content,’id’);

deprecated

use

wp_editor($content,’id’ . $post->ID);
?>

To get ajax data, you can call global tinyMCE object
tinyMCE.get(node_id).getContent();

Categories
Wordpress

Creating a custom widget

Here is the basic code to use to create a widget.

class myWidget extends WP_Widget {
function myWidget() {
// widget actual processes
parent::WP_Widget(false, $name = ‘myWidget’);
}

function form($instance) {
// outputs the options form on admin
}

function update($new_instance, $old_instance) {
// processes widget options to be saved
}

function widget($args, $instance) {
// outputs the content of the widget
?>
<li> …
</li>
<?
}

}
register_widget(‘myWidget’);

Categories
Wordpress

Custom Boxes in Posts

You can create a custom box in post/page/custom-post-type by adding a couple of actions and telling it to trigger for the post-type.

1. add_action( ‘add_meta_boxes’, ‘people_add_custom_box’ ); // this hooks into the ‘add_meta_boxes’ action, and calls the function people_add_custom_box

2.     function people_add_custom_box() {
add_meta_box(
‘people-quote’,
__( ‘Quote’, ‘quote_textdomain’ ),
‘people_inner_custom_box’,
‘people’
);
Each add_meta_box creates a separate box, in this case it calls function people_inner_custom_box() to generate its HTML

3. function people_inner_custom_box($post). Use this to generate the form fields. You can use a nonce to prevent accidental posting

$collection_link = get_post_meta($post->ID,’collection_link’,true);
$template = get_post_meta($post->ID,’template’,true);
$video = get_post_meta($post->ID,’video’,true);
?>

function designer_page_template($post) {
$collection_link = get_post_meta($post->ID,’collection_link’,true);
$template = get_post_meta($post->ID,’template’,true);
$video = get_post_meta($post->ID,’video’,true);
?>
Page Type: <select name=”template”>
<option value=”landing” <?php echo ($template==”landing”) ? ‘selected=”selected”‘:”;?>>Designer Landing Page</option>
<option value=”history” <?php echo ($template==”history”) ? ‘selected=”selected”‘:”;?>>History Page</option>
<option value=”inspiration” <?php echo ($template==”inspiration”) ? ‘selected=”selected”‘:”;?>>Inspiration Page</option>
<option value=”video” <?php echo ($template==”video”) ? ‘selected=”selected”‘:”;?>>Video Page</option>
</select><br />
Collection Link: <input type=”text” name=”collection_link” id=”collection_link” value=”<?php echo $collection_link;?>” style=”width:400px”/><br />
Video embed code:<br />
<textarea style=”width:500px;height:100px;” name=”video”><?php echo stripslashes($video);?></textarea>
<?
}

4. add_action( ‘save_post’, ‘people_save_postdata’ ); // hooks into save_post and calls function people_save_postdata(). You can then use update_post_meta() to store the data.

5.function people_save_postdata($post_id) {
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
return;
$address = $_POST[‘address’];
if ($address) update_post_meta($post_id,’address’,$address);

}

Categories
Wordpress

Theme Options

If you want to create a menu in wp-admin for Theme Options ( Appearance->Theme Options), you can start with this

http://themeshaper.com/2010/06/03/sample-theme-options/

Just download the zip file, search and replace “sample” of theme-options.php with your theme name.

in functions.php add

require_once ( get_template_directory() . ‘/theme-options.php’ );

then in your index.php ( and other template files) put the following at the top

$theme_options = get_option(‘sample_theme_options’);

For each include ( header.php, footer.php, sidebar.php, etc), do a

global $theme_options;

Then you can use $theme_options[‘option_name’]

Categories
Wordpress

WordPress post formats

WordPress 3.1 introduces post-formats. You have to activate this “feature” and it gives you a new admin box in the posts type. Although this isn’t a big new feature, it is quite common for sites to have quotes, videos, links, etc. Loop retrieves the post-format for each post, you can then use that to style the post.

Here is the link to codex documentation.

http://codex.wordpress.org/Post_Formats

Categories
Wordpress

WordPress custom post type

Here is a great resource for creating custom post types for wordpress. Use this to generate the custom post type code. The documentation is sparse, and its not entirely clear but its still pretty good.

http://themergency.com/generators/wordpress-custom-post-types/