Add a checkbox to post screen that adds a class to the title

I’m needing some posts to have different title colors than others, and I figured this would be most conveniently accomplished by some sort of check box on the post creation/edit screen. How would I go about adding the check box, then retrieving the value in the template?

Thanks

Related posts

Leave a Reply

2 comments

  1. Yes, that’s right, you need a Custom Meta Box that’ll create the checkboxes in the Post Editing screen.

    post custom meta box

    In this case, they are radio buttons, as we need only one value.

    The following is the code that creates it. Put it in your theme’s functions.php file or create a simple plugin so this becomes theme independent.

    /* Define the custom box */
    add_action( 'add_meta_boxes', 'wpse_61041_add_custom_box' );
    
    /* Do something with the data entered */
    add_action( 'save_post', 'wpse_61041_save_postdata' );
    
    /* Adds a box to the main column on the Post and Page edit screens */
    function wpse_61041_add_custom_box() {
        add_meta_box( 
            'wpse_61041_sectionid',
            'Title Color',
            'wpse_61041_inner_custom_box',
            'post',
            'side',
            'high'
        );
    }
    
    /* Prints the box content */
    function wpse_61041_inner_custom_box($post)
    {
        // Use nonce for verification
        wp_nonce_field( 'wpse_61041_wpse_61041_field_nonce', 'wpse_61041_noncename' );
    
        // Get saved value, if none exists, "default" is selected
        $saved = get_post_meta( $post->ID, 'title_color', true);
        if( !$saved )
            $saved = 'default';
    
        $fields = array(
            'red'       => __('Red', 'wpse'),
            'green'     => __('Green', 'wpse'),
            'blue'      => __('Blue', 'wpse'),
            'default'   => __('Default', 'wpse'),
        );
    
        foreach($fields as $key => $label)
        {
            printf(
                '<input type="radio" name="title_color" value="%1$s" id="title_color[%1$s]" %3$s />'.
                '<label for="title_color[%1$s]"> %2$s ' .
                '</label><br>',
                esc_attr($key),
                esc_html($label),
                checked($saved, $key, false)
            );
        }
    }
    
    /* When the post is saved, saves our custom data */
    function wpse_61041_save_postdata( $post_id ) 
    {
          // verify if this is an auto save routine. 
          // If it is our form has not been submitted, so we dont want to do anything
          if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
              return;
    
          // verify this came from the our screen and with proper authorization,
          // because save_post can be triggered at other times
          if ( !wp_verify_nonce( $_POST['wpse_61041_noncename'], 'wpse_61041_wpse_61041_field_nonce' ) )
              return;
    
          if ( isset($_POST['title_color']) && $_POST['title_color'] != "" ){
                update_post_meta( $post_id, 'title_color', $_POST['title_color'] );
          } 
    }
    

    code based on this Answer

    How to use it in the theme

    With the proper CSS rules in place (h1.default, h1.red, etc), wherever you want to apply the color class to the title (index.php, single.php, etc), use something like:

    <?php $title_color = get_post_meta( get_the_ID(), 'title_color', true); ?>
    <h1 class="entry-title <?php echo esc_attr($title_color); ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    
  2. @brasofilo work is great but if you do not want to do much coding, you can do same work from Custom Fields. It is hidden by default. You can make is visible from Screen Options. Create new field & name it color and put color name in value field. Then put this code in you theme’s header.php below </head> tag.

    <style type="text/css">
    /*Replace [color-name] with you color example: red*/
    #red {color:"red";}
    #green {color:"green";}
    #[color-name] {color:"[color-name]";}
    #[color-name] {color:"[color-name]";}
    </style>
        <?php
    global $post;
    $color = get_post_meta(get_the_ID(), 'color', true);
    $id = "id="
    ?>
    

    Then search for <h1 class="entry-title" and put this code inside it <?php echo $id, $color;?>. Now when evey you want to change title color, choose color in custom fields drop down & write color name in value.