Custom meta boxes for HTML and CSS fields #8

Merged
merged 1 commit into from Feb 8, 2012

Conversation

Projects
None yet
4 participants
Contributor

scotcrop commented Feb 7, 2012

Added custom meta box for HTML and CSS fields below post body. Saves time and gives more room to enter HTML and CSS by default rather than having to create each field with each new post.

Added custom meta box for HTML and CSS fields below post body. Saves …
…time and gives more room to enter HTML and CSS by default rather than having to create each field with each new post.
Contributor

scotcrop commented Feb 7, 2012

Added the following to functions.php to make the html and css fields already have meta boxes rather than creating them for each post in the custom fields area. Screen shot below code.

add_action( 'add_meta_boxes', 'pears_add_meta_box' );
add_action( 'save_post', 'pears_save_post' );

function pears_add_meta_box() {

    add_meta_box( 
        'pears',
        'Pears',
        'pears_meta_box',
        'post',
        'normal',
        'high'
    );

}

function pears_meta_box( $post ) {
    wp_nonce_field( plugin_basename( __FILE__ ), 'pears_noncename' );

    $html = get_post_meta($post->ID,'html',true);
    $css = get_post_meta($post->ID,'css',true);

    echo '<p>These fields are for the HTML markup and CSS styles.  The post body can be used for notes.</p>';
    echo '<label for="html">HTML</label>';
    echo '<p><textarea id="html" name="html" rows="20" cols="90" />' . $html . '</textarea></p>';
    echo '<label for="css">CSS</label>  ';
    echo '<p><textarea id="css" name="css" rows="20" cols="90" />' . $css . '</textarea></p>';
}

function pears_save_post( $post_id ) {

    // Ignore if doing an autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify data came from pears meta box
    if ( !wp_verify_nonce( $_POST['pears_noncename'], plugin_basename( __FILE__ ) ) )
        return;         

    // Check user permissions
    if ( 'post' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) )
            return;
    }
    else{
        if ( !current_user_can( 'edit_post', $post_id ) )
            return;
    }

    $html_data = $_POST['html'];
    update_post_meta($post_id, 'html', $html_data);

    $css_data = $_POST['css'];
    update_post_meta($post_id, 'css', $css_data);
}

Screenshot example

Ace - get this in as soon as you can Dan, will make things much nicer.

replete commented Feb 8, 2012

Yes, please merge this, it's most welcome.

simplebits added a commit that referenced this pull request Feb 8, 2012

Merge pull request #8 from twentyniner/patch-1
Custom meta boxes for HTML and CSS fields

@simplebits simplebits merged commit dd86a40 into simplebits:master Feb 8, 2012

Owner

simplebits commented Feb 8, 2012

Superb! Thanks so much for adding this.

@scotcrop scotcrop deleted the scotcrop:patch-1 branch Apr 30, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment