• Resolved pultz

    (@pultz)


    I’m creating my own theme and wan’t to make a checkbox on every post that says something like if this is “checked” print “this image” my problems is that i cant seem to get anything from my custom meta box data on my frontend heres my custom meta box code:

    // Add the Meta Box
    function add_custom_meta_box() {
        add_meta_box(
            'custom_meta_box', // $id
            'Programs used', // $title
            'show_custom_meta_box', // $callback
            'portfolio', // $page
            'normal', // $context
            'high'); // $priority
    }
    add_action('add_meta_boxes', 'add_custom_meta_box');
    // Field Array
    $prefix = 'custom_';
    $custom_meta_fields = array(
    array (
        'label' => '2D Programs',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'checkbox2D_group',
        'type'  => 'checkbox_group',
        'options' => array (
            'one' => array (
                'label' => 'Photoshop',
                'value' => 'one'
            ),
            'two' => array (
                'label' => 'Illustrator',
                'value' => 'two'
            )
        )
    ),
    array (
        'label' => '3D Programs',
        'desc'  => 'A description for the field.',
        'id'    => $prefix.'checkbox3D_group',
        'type'  => 'checkbox_group',
        'options' => array (
            'three' => array (
                'label' => 'Maya',
                'value' => 'three'
            ),
            'four' => array (
                'label' => 'zBrush',
                'value' => 'four'
            )
        )
    ),
    );
    // The Callback
    function show_custom_meta_box() {
    global $custom_meta_fields, $post;
    // Use nonce for verification
    echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
    
        // Begin the field table and loop
        echo '<table class="form-table">';
        foreach ($custom_meta_fields as $field) {
            // get value of this field if it exists for this post
            $meta = get_post_meta($post->ID, $field['id'], true);
            // begin a table row with
            echo '<tr>
                    <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                    <td>';
                    switch($field['type']) {
                        // checkbox
    case 'checkbox_group':
        foreach ($field['options'] as $option) {
            echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />
                    <label for="'.$option['value'].'">'.$option['label'].'</label><br />';
        }
        echo '<span class="description">'.$field['desc'].'</span>';
    break;
                    } //end switch
            echo '</td></tr>';
        } // end foreach
        echo '</table>'; // end table
    }
    // Save the Data
    function save_custom_meta($post_id) {
        global $custom_meta_fields;
    
        // verify nonce
        if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
            return $post_id;
        // check autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
            return $post_id;
        // check permissions
        if ('page' == $_POST['post_type']) {
            if (!current_user_can('edit_page', $post_id))
                return $post_id;
            } elseif (!current_user_can('edit_post', $post_id)) {
                return $post_id;
        }
    
        // loop through fields and save the data
        foreach ($custom_meta_fields as $field) {
            $old = get_post_meta($post_id, $field['id'], true);
            $new = $_POST[$field['id']];
            if ($new && $new != $old) {
                update_post_meta($post_id, $field['id'], $new);
            } elseif ('' == $new && $old) {
                delete_post_meta($post_id, $field['id'], $old);
            }
        } // end foreach
    }
    add_action('save_post', 'save_custom_meta', 10, 2);
    /*

    I followed a tutorial and it did not tell how to get it out on the front-end. and it could be something with my saev function too thats wrong maybe.

Viewing 15 replies - 1 through 15 (of 25 total)
  • Try this code for example

    add_action( 'admin_init', 'my_admin_car' );
    function my_admin_car() {
    	add_meta_box( 'car_meta_box', 'Car Details', 'display_car_meta_box','car', 'normal', 'high' );
    }
    function display_car_meta_box( $car ) {
    	?>
    	<h4>General Details</h4>
    	<table width="100%">
            <tr>
                <td style="width: 25%">Monthly Paymeny</td>
                <td><input type="text" style="width:425px;" name="meta[payment]" value="<?php echo esc_html( get_post_meta( $car->ID, 'payment', true ) );?>" />
                </td>
    		</tr>
            <tr>
                <td>Price ($)</td>
                <td><input type="text" style="width:425px;" name="meta[price]" placeholder="$" value="<?php echo esc_html( get_post_meta( $car->ID, 'price', true ) );?>" />
                </td>
    		</tr>
            <tr>
                <td>Milage</td>
                <td><input type="text" style="width:425px;" name="meta[milage]" value="<?php echo esc_html( get_post_meta( $car->ID, 'milage', true ) );?>" />
                </td>
    		</tr>
    	</table>
    <?php
    }
    add_action( 'save_post', 'add_car_fields', 10, 2 );
    function add_car_fields( $car_id, $car ) {
    	if ( $car->post_type == 'car' ) {
    		if ( isset( $_POST['meta'] ) ) {
    			foreach( $_POST['meta'] as $key => $value ){
    				update_post_meta( $car_id, $key, $value );
    			}
    		}
    	}
    }
    Thread Starter pultz

    (@pultz)

    i might have forgotten to say that my code is my function.php file. but gonna try that code

    Thread Starter pultz

    (@pultz)

    i tryed your code and it gives me text fields and text fields are not my problem, its that i need it to be check boxes and how to see if its true or false on my single.php page.

    here is what i get with your code.

    Picture of it

    Replace the table code

    <table width="100%">
    	<tr>
    		<td style="width: 25%">Checkbox Sample</td>
    		<td><input type="checkbox" style="width:425px;" name="meta[check_1]" value="True" <?php if( get_post_meta( $car->ID, 'check_1', true ) == "True" ){?>checked<?php}?> />
    		</td>
    	</tr>
    </table>
    Thread Starter pultz

    (@pultz)

    think there is something wrong with this line

    <input type="checkbox" style="width:425px;" name="meta[check_1]" value="True" <?php if( get_post_meta( $car->ID, 'check_1', true ) == "True" ) {?>checked<?php}?> />

    it give me an error that says

    unexpected end of file

    and tried it out it something abou the {?>checked<?php}?> that does not work

    <input type=”checkbox” style=”width:425px;” name=”meta[check_1]” value=”True” <?php if( get_post_meta( $car->ID, ‘check_1’, true ) == “True” ) {?>checked<?php }?> />

    Give space <?php and }

    Thread Starter pultz

    (@pultz)

    LoL my bad. but now i have the problem i had a long time ago.

    when i update my item it wont stay checked so guess there’s something in the save option that needs to be change?

    and can you send me some code that so i can print it out on the front page?

    if its check i want to be able to do something els do nothing like that?

    i appreciate all your help.

    You can get option from following code on your theme file

    <?php echo get_post_meta( get_the_ID(), 'check_1', true );?>

    Thread Starter pultz

    (@pultz)

    first think i still cant get it to save on my admin page. if i update the product after checking it it goes back to not checked.

    2nd i dont get the <?php echo get_post_meta( get_the_ID(), ‘check_1’, true );?>

    could you maybe make it in to an if

    like

    if(check_1 == checked)
    do something
    else
    dont do it.

    something like that?

    Sorry pultz

    Its working for me. I am not sure what is the issue

    Thread Starter pultz

    (@pultz)

    the issue is that when i check the box and then press update. it’s unchecked again

    Thread Starter pultz

    (@pultz)

    i fixed it my bad. now i just need a way to make the if statment on my single.php

    like
    if(check_1 == checked)
    do something
    else
    dont do it.

    do you have anything?

    I need to see your function

    Can you please put it on https://github.com/

    Thread Starter pultz

    (@pultz)

    here is the function as said i made it work now i just need to be able to have more then one and get the checked vale on my single.php

    Paste bin

    this is my single.php code

    <?php 
    
    		if (have_posts() ):
    
    			while(have_posts() ) : the_post(); ?>
    
    		<h3><?php the_title(); ?></h3>
    		<p><?php the_content(); ?></p>
    		<?php echo get_post_meta( get_the_ID(), 'check_1', true );?>

    Please replace this code

    <?php
    add_action( 'admin_init', 'my_admin_portfolio' );
    function my_admin_portfolio() {
        add_meta_box( 'portfolio_meta_box', 'Portfolio Details', 'display_portfolio_meta_box','Portfolio', 'normal', 'high' );
    }
    function display_portfolio_meta_box( $portfolio ) {
        ?>
        <h4>General Details</h4>
        <table width="100%">
        <tr>
            <td style="width: 25%">Checkbox Sample</td>
            <td><input type="checkbox" style="width:425px;" name="meta[check_1]" value="True" <?php if( get_post_meta( $portfolio->ID, 'check_1', true ) == "True" ) {?>checked<?php }?> />
            </td>
        </tr>
    </table>
    <?php
    }
    add_action( 'save_post', 'add_portfolio_fields', 10, 2 );
    function add_portfolio_fields( $portfolio_id, $portfolio ) {
        if ( $portfolio->post_type == 'portfolio' ) {
            if ( isset( $_POST['meta'] ) ) {
                foreach( $_POST['meta'] as $key => $value ){
                    update_post_meta( $portfolio_id, $key, $value );
                }
            }
        }
    }
    ?>
Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Custom Meta Box Checkbox not printing’ is closed to new replies.