• Resolved mr_D

    (@mr_d)


    Hi,
    I know that, this issue was hundrets of times on furum. I’m still cant do this in my plugin.

    This is a code:

    <?php
    
    function nimble_portfolio_create_section() {
        add_meta_box('nimble-portfolio-section-options', __('Opcje', 'nimble_portfolio_context'), 'nimble_portfolio_section_options', 'portfolio', 'side', 'high');
    }
    
    add_action('admin_menu', 'nimble_portfolio_create_section');
    
    function nimble_portfolio_section_options() {
     global $post;
    $meta_box_value = get_post_meta($post->ID, $mydata['nimble-portfolio_show_c'] , true);  
    
        ?>
    
    <ul>
    <li>
    			<label for="nimble_portfolio"><?php _e('Cena: ', 'nimble_portfolio_context')?></label>
    			<input type="text" id="nimble_portfolio" name="nimble_portfolio" value="<?php echo htmlspecialchars(nimble_portfolio_get_meta('nimble-portfolio')); ?>" />
    			<span><?php _e('z?')?></span>
    		</li>
    <li>
    			<label for="nimble_portfolio_time"><?php _e('Czas: ', 'nimble_portfolio_context')?></label>
    			<input type="text" id="nimble_portfolio_time" name="nimble_portfolio_time" value="<?php echo htmlspecialchars(nimble_portfolio_get_meta('nimble-portfolio_time')); ?>" />
    		</li>
    <li>
    			<?php if($meta_box_value == "yes") $checked = 'checked="checked"';?>
    			<label for="nimble_portfolio_show_c"><?php _e('Poka? w cenniku: ', 'nimble_portfolio_context')?></label>
    			<input type="checkbox" id="nimble_portfolio_show_c" name="nimble_portfolio_show_c" value="yes" <?php echo $checked ?>/>
    		</li>
    <li>
    			<label for="nimble_portfolio_show_u"><?php _e('Poka? w us?ugach: ', 'nimble_portfolio_context')?></label>
    			<input type="checkbox" id="nimble_portfolio_show_u" name="nimble_portfolio_show_u" value="<?php echo htmlspecialchars(nimble_portfolio_get_meta('nimble-portfolio_show_u')); ?>" />
    		</li>
    </ul>
    	<input type="hidden" name="nimble_portfolio_noncename" id="nimble_portfolio_noncename" value="<?php echo wp_create_nonce(plugin_basename(__FILE__)); ?>" />
        <?php
    }
    
    add_action('save_post', 'nimble_portfolio_save_data', 1, 2);
    function nimble_portfolio_save_data($post_id, $post) {
    
        // 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['nimble_portfolio_noncename'], plugin_basename(__FILE__)))
            return $post->ID;
    
        if ($post->post_type == 'revision')
            return; //don't store custom data twice
    
        if (!current_user_can('edit_post', $post->ID))
            return $post->ID;
    
        // OK, we're authenticated: we need to find and save the data
        // We'll put it into an array to make it easier to loop though.
        $mydata = array();
        $mydata['nimble-portfolio'] = $_POST['nimble_portfolio'];
    	$mydata['nimble-portfolio_time'] = $_POST['nimble_portfolio_time'];
    	$mydata['nimble-portfolio_show_c'] = $_POST['nimble_portfolio_show_c'];
    	$mydata['nimble-portfolio_show_u'] = $_POST['nimble_portfolio_show_u'];
    
        // Add values of $mydata as custom fields
        foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!
            update_post_meta($post->ID, $key, $value);
            if (!$value)
                delete_post_meta($post->ID, $key); //delete if blank
        }
    }
    ?>

  • The topic ‘[plugin nimble portfolio] Meta box checkbox’ is closed to new replies.