• 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 10 replies - 16 through 25 (of 25 total)
  • Thread Starter pultz

    (@pultz)

    thanks it’s almost there the only problem i have left now is i can’t uncheck and update it. then it will always remember that it was checked ad stay checked.

    what am i doing wrong there?

    Try this alternative method

    <?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>
    			<select style="width:425px;" name="meta[check_1]">
    				<option value="True" <?php if( get_post_meta( $portfolio->ID, 'check_1', true ) == "True" ) {?>select<?php }?>>True</option>
    				<option value="False" <?php if( get_post_meta( $portfolio->ID, 'check_1', true ) == "False" ) {?>select<?php }?>>False</option>
    			</select>
    		</td>
            </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 );
                }
            }
        }
    }
    ?>
    Thread Starter pultz

    (@pultz)

    well that kinda worked i can see on my front end that it change to false but the dropdown still puts it on true even when its false.

    and really want it to be a checkbox if you have a way to things this problem for the checkbox ??

    I am not sure about your issue, Its working for me

    If is possible share your theme file to my mail [redacted]

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    @rajanit2000, Please don’t contact people off the forums. If you want to see a file you can simply ask for it to be shared via PasteBin

    Thread Starter pultz

    (@pultz)

    here is the link til my function file

    Function file

    and the link til my single file

    single file

    Thread Starter pultz

    (@pultz)

    @anevins just so you know. but even though you redacted the mail i still got it in my mail ?? so is that something the forum should look into maybe?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Yea but the best solution is to ensure it doesn’t happen again.

    Thread Starter pultz

    (@pultz)

    Thanks so much for the help @rajanit2000, if anyone need help ask him.

    Here is the result that made it work for me.

    my function.php code:

    add_action( 'admin_init', 'my_admin_portfolio' );
    function my_admin_portfolio() {
        add_meta_box( 'portfolio_meta_box', 'Programs used', 'display_portfolio_meta_box','Portfolio', 'normal', 'high' );
    }
    function display_portfolio_meta_box( $portfolio ) {
        ?>
        <h4>2D Programs</h4>
        <table width="100%">
        <tr>
            <td style="width: 25%">Photoshop</td>
            <td><input type="checkbox" style="width:425px;" name="meta[Photoshop]" value="True" <?php if( get_post_meta( $portfolio->ID, 'Photoshop', true ) == "True" ) {?>checked<?php }?> />
            </td>
        </tr>
        <tr>
            <td style="width: 25%">Illustrator</td>
            <td><input type="checkbox" style="width:425px;" name="meta[Illustrator]" value="True" <?php if( get_post_meta( $portfolio->ID, 'Illustrator', 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 (  $_POST['meta']['Photoshop'] == "True" ) {
                update_post_meta( $portfolio_id, 'Photoshop', 'True' );
            }else{
    			update_post_meta( $portfolio_id, 'Photoshop', 'False' );
    		}
        }
    }

    this is my single.php code (where i print it):

    <?php
    			if (get_post_meta(get_the_ID(), 'Photoshop', 'Illustrator', 'Maya', 'zbrush', 'sDesigner', 'After', 'Premiere', 'Nuke') == null) {
    				echo "<div class='nosoftware'><h3>No software has been picked!</h3></div>";
    			}
    			else
    			{
    
    			if (get_post_meta( get_the_ID(), 'Photoshop', true ) == "True")
    			{
    				echo "<div class='software'><img src='"; ?><?php bloginfo('template_directory'); ?><?php echo "/img/logos/Photoshop.png' /><h3>Photoshop</h3></div>";
    
    			}
    
    			if (get_post_meta(get_the_ID(), 'Illustrator', true) == "True")
    			{
    				echo "<div class='software'><img src='"; ?><?php bloginfo('template_directory'); ?><?php echo "/img/logos/ill.png' /><h3>Illustrator</h3></div>";
    
    			}
    }

    Cheers ??

Viewing 10 replies - 16 through 25 (of 25 total)
  • The topic ‘Custom Meta Box Checkbox not printing’ is closed to new replies.