WordPress.org

Forums

[resolved] Custom Metaboxes (2 posts)

  1. MaddTechWF
    Member
    Posted 4 years ago #

    I've gotten my metaboxes layed out the way that I want them. I have a a file called portfolio_metabox.php. Inside the file I have the following code.

    <?php
    
    add_action('admin_menu', 'add_portfolio_boxes');
    add_action('save_post', 'save_portfolio_postdata');
    
    function add_portfolio_boxes()
    {
    	add_meta_box('metabox','Technology Used','Technology_Used_meta','portfolio','side','default');
    	add_meta_box('metabox','Website URL','Website_URL_meta','portfolio','side','default');
    	add_meta_box('metabox','Testimonial','Testimonial_meta','portfolio','normal','high');
    }
    
    function Technology_Used_meta()
    {
    	global $post;
    	echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    	?>
        <style>
    		.my_meta_control .description { display:none; }
     		.my_meta_control label { display:block; font-weight:bold; margin:6px; margin-bottom:0; margin-top:12px; }
    		.my_meta_control label span { display:inline; font-weight:normal; }
    		.my_meta_control span { color:#999; display:block; }
    		.my_meta_control textarea, .my_meta_control input[type='text'] { margin-bottom:3px; width:99%; }
    		.my_meta_control h4 { color:#999; font-size:1em; margin:15px 6px; text-transform:uppercase; }
    	</style>
    	<div class="my_meta_control">
     		<p>Put the URL to the website here.  If left blank, a button will not appear on the front end.
            You must include the <code>http://</code>.
            </p>
    		<p>
    			<input type="text" name="Technology_Used" id="Technology_Used" tabindex="1" value="<?php echo get_post_meta($post->ID, "Technology_Used", true); ?>"/>
    		</p>
    	</div>
        <?php
    }
    
    function Website_URL_meta()
    {
    	global $post;
    	echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    	?>
        <style>
    		.my_meta_control .description { display:none; }
     		.my_meta_control label { display:block; font-weight:bold; margin:6px; margin-bottom:0; margin-top:12px; }
    		.my_meta_control label span { display:inline; font-weight:normal; }
    		.my_meta_control span { color:#999; display:block; }
    		.my_meta_control textarea, .my_meta_control input[type='text'] { margin-bottom:3px; width:99%; }
    		.my_meta_control h4 { color:#999; font-size:1em; margin:15px 6px; text-transform:uppercase; }
    	</style>
    	<div class="my_meta_control">
     		<p>Put the URL to the website here.  If left blank, a button will not appear on the front end.
            You must include the <code>http://</code>.</p>
    		<p>
    			<input type="text" name="Website_URL" id="Website_URL" tabindex="1" value="<?php echo get_post_meta($post->ID, "Website_URL", true); ?>"/>
    		</p>
    	</div>
        <?php
    }
    
    function Testimonial_meta()
    {
    	global $post;
    	echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    	?>
        <style>
    		.my_meta_control .description { display:none; }
     		.my_meta_control label { display:block; font-weight:bold; margin:6px; margin-bottom:0; margin-top:12px; }
    		.my_meta_control label span { display:inline; font-weight:normal; }
    		.my_meta_control span { color:#999; display:block; }
    		.my_meta_control textarea, .my_meta_control input[type='text'] { margin-bottom:3px; width:99%; }
    		.my_meta_control h4 { color:#999; font-size:1em; margin:15px 6px; text-transform:uppercase; }
    	</style>
        <div class="my_meta_control">
     		<p>Provide a testimonial from a client if you would like.  If one is not provided, this section will not appear on the front end.</p>
    		<p>
    			<textarea name="Testimonial" id="Testimonial" tabindex="1" value="<?php echo get_post_meta($post->ID, "Testimonial", true); ?>"></textarea>
    		</p>
    	</div>
        <?php
    }
    
    function save_portfolio_postdata( $post_id ) {
    
    	if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
        	return $post_id;
      	}
    
      	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        	return $post_id;
    
    	if ( $parent_id = wp_is_post_revision($post_id) )
    	{
    		$post_id = $parent_id;
    	}
    	// Website URL
    	if (!get_post_meta($post_id, "Website_URL")) {
    		add_post_meta($post_id, "Website_URL", $_POST["Website_URL"]);
      	}else{
      		update_post_meta($post_id, "Website_URL", $_POST["Website_URL"]);
      	}
    	if ($_POST["Website_URL"] == "") {
    		  delete_post_meta($post_id, "Website_URL");
    	}
    	// Technology Used
    	if (!get_post_meta($post_id, "Technology_Used")) {
    		add_post_meta($post_id, "Technology_Used", $_POST["Technology_Used"]);
      	}else{
      		update_post_meta($post_id, "Technology_Used", $_POST["Technology_Used"]);
      	}
    	if ($_POST["Technology_Used"] == "") {
    		  delete_post_meta($post_id, "Technology_Used");
    	}
    	// Testimonial
    	if (!get_post_meta($post_id, "Testimonial")) {
    		add_post_meta($post_id, "Testimonial", $_POST["Testimonial"]);
      	}else{
      		update_post_meta($post_id, "Testimonial", $_POST["Testimonial"]);
      	}
    	if ($_POST["Testimonial"] == "") {
    		  delete_post_meta($post_id, "Testimonial");
    	}
    }
    ?>

    I'm only getting the Testimonial metabox. Can anyone tell me why?

  2. MaddTechWF
    Member
    Posted 4 years ago #

    After looking over my code for 2 hours, I finally found it.

    Previous Code:

    function add_portfolio_boxes()
    {
    	add_meta_box('metabox','Technology Used','Technology_Used_meta','portfolio','side','default');
    	add_meta_box('metabox','Website URL','Website_URL_meta','portfolio','side','default');
    	add_meta_box('metabox','Testimonial','Testimonial_meta','portfolio','normal','high');
    }

    Fixed Code:

    function add_portfolio_boxes()
    {
    	add_meta_box('Technology_Used_meta','Technology Used','Technology_Used_meta','portfolio','side','default');
    	add_meta_box('Website_URL_meta','Website URL','Website_URL_meta','portfolio','side','default');
    	add_meta_box('metabox','Testimonial','Testimonial_meta','portfolio','normal','high');
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.