Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type set up and I want to add a checkbox option. If the box is checked, I want to use a php if/else statement to show one div or another. Can someone take a look at my code and help me figure out how to add the checkbox option?

/* Register Projects Post Type */
add_action('init', 'create_projects');

function create_projects() {

    $labels = array(
        'name' => __('Projects', 'post type general name'),
        'singular_name' => __('Project', 'post type singular name'),
        'add_new' => __('Add New', 'project item'),
        'add_new_item' => __('Add New Project'),
        'edit_item' => __('Edit Project'),
        'new_item' => __('New Project'),
        'view_item' => __('View Project'),
        'search_items' => __('Search Projects'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','excerpt','thumbnail')
      ); 

    register_post_type( 'projects' , $args );
}

/* Register Filter Taxonomy */
register_taxonomy("Filter", array("projects"), array("hierarchical" => true, "label" => "Filters", "singular_label" => "Filter", "rewrite" => true));

/* Add Fields */
add_action("admin_init", "add_projects_fields");

function add_projects_fields(){
    add_meta_box("project_title", "Project Title", "project_title", "projects", "side", "low");
    add_meta_box("website_url", "Website URL", "website_url", "projects", "side", "low");
    add_meta_box("screenshot_name", "Screenshot Name", "screenshot_name", "projects", "side", "low");
    add_meta_box("thumbnail_name", "Thumbnail Name", "thumbnail_name", "projects", "side", "low");
    add_meta_box("thumbnail_alt", "Thumbnail Alt", "thumbnail_alt", "projects", "side", "low");
}

function project_title(){
    global $post;
    $custom = get_post_custom($post->ID);
    $project_title = $custom["project_title"][0];
    ?>
    <label>Project Title:</label>
    <input size="50" name="project_title" value="<?php echo $project_title; ?>" />
    <?php
}

function website_url(){
    global $post;
    $custom = get_post_custom($post->ID);
    $website_url = $custom["website_url"][0];
    ?>
    <label>Website URL:</label>
    <input size="50" name="website_url" value="<?php echo $website_url; ?>" />
    <?php
}

function screenshot_name() {
    global $post;
    $custom = get_post_custom($post->ID);
    $screenshot_name = $custom["screenshot_name"][0];
    ?>
    <label>Screenshot Name:</label>
    <input name="screenshot_name" value="<?php echo $screenshot_name; ?>" />
    <?php
}

function thumbnail_name() {
    global $post;
    $custom = get_post_custom($post->ID);
    $thumbnail_name = $custom["thumbnail_name"][0];
    ?>
    <label>Thumbnail Name:</label>
    <input name="thumbnail_name" value="<?php echo $thumbnail_name; ?>" />
    <?php
}

function thumbnail_alt() {
    global $post;
    $custom = get_post_custom($post->ID);
    $thumbnail_alt = $custom["thumbnail_alt"][0];
    ?>
    <label>Thumbnail Alt:</label>
    <input name="thumbnail_alt" value="<?php echo $thumbnail_alt; ?>" />
    <?php
}

add_action('save_post', 'save_projects_details');

function save_projects_details(){
    global $post;

    update_post_meta($post->ID, "project_title", $_POST["project_title"]);
    update_post_meta($post->ID, "website_url", $_POST["website_url"]);
    update_post_meta($post->ID, "screenshot_name", $_POST["screenshot_name"]);
    update_post_meta($post->ID, "thumbnail_name", $_POST["thumbnail_name"]);
    update_post_meta($post->ID, "thumbnail_alt", $_POST["thumbnail_alt"]);
}

/* Custom Columns */
add_action("manage_posts_custom_column",  "projects_custom_columns");
add_filter("manage_edit-projects_columns", "projects_edit_columns");

function projects_edit_columns($columns){
    $columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "title" => "Project Title",
        "description" => "Description",
    );

    return $columns;
}

function projects_custom_columns($column){
    global $post;

    switch ($column) {
        case "description":
        the_excerpt();
        break;
    }
}

EDIT: To further elaborate, below is an image of how I would like this to look. Just a simple checkbox that I can toggle on/off.

enter image description here

Then in my HTML, I can just use something like this:

if box is checked, don't show this div
share|improve this question
    
You should use all the esc_*() magic on those $_POST values. Else everyone could add whatever malicious code and save it into your database. This is a home made vulnerability. –  kaiser Apr 24 '13 at 11:04
    
I'm not sure how to fix that. Do you mean replace every instance of $_POST with esc_*()? –  Desi Apr 24 '13 at 11:08
    
It's not that hard –  kaiser Apr 24 '13 at 11:48
    
Is this a Javascript question? Do you want checking the box to toggle divs dynamically? –  s_ha_dum Apr 24 '13 at 13:53
    
@s_ha_dum I added an edit to the question to further elaborate my question. I'm just looking for a simple checkbox to include in the edit post section. Then, through php, I want to control what div shows if the box is checked. –  Desi Apr 24 '13 at 20:46
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.