Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Update my custom write/post code WordPress

  • SOLVED

I am working on a new child theme but noticed that a new tutorial was out on this custom write panel I have in my functions.php (attached). Here is the tutorial on the new site. I have already tried adding this and tweaking it but I cannot get it. I believe it is rather easy, I am just more of a css/html person and struggle sometimes with php.

The tut: http://wefunction.com/2009/10/revisited-creating-custom-write-panels-in-wordpress/

Current functions.php attached.

Answers (1)

2012-05-29

Arnav Joy answers:

what you want to do , can you explain?


Jagst3r15 comments:

Okay so you see in functions.php the code at the buttom under the commented credits? I am trying to use the updated code that is in the tutorial and simply plug in my existing stuff like web-url, designed-by, and the full path to image/icon. This new code is apparently faster and I am trying to keep my theme as up to date as possible.

The new code is this:


$key = "key";
$meta_boxes = array(
"image" => array(
"name" => "image",
"title" => "Image",
"description" => "Using the \"<em>Add an Image</em>\" button, upload an image and paste the URL here. Images will be resized. This is the Article's main image and will automatically be sized."),
"tinyurl" => array(
"name" => "tinyurl",
"title" => "Tiny URL",
"description" => "Add a small URL of this post that will be used to track tweets, and share the post.")
);

function create_meta_box() {
global $key;

if( function_exists( 'add_meta_box' ) ) {
add_meta_box( 'new-meta-boxes', ucfirst( $key ) . ' Custom Post Options', 'display_meta_box', 'post', 'normal', 'high' );
}
}

function display_meta_box() {
global $post, $meta_boxes, $key;
?>

<div class="form-wrap">

<?php
wp_nonce_field( plugin_basename( __FILE__ ), $key . '_wpnonce', false, true );

foreach($meta_boxes as $meta_box) {
$data = get_post_meta($post->ID, $key, true);
?>

<div class="form-field form-required">
<label for="<?php echo $meta_box[ 'name' ]; ?>"><?php echo $meta_box[ 'title' ]; ?></label>
<input type="text" name="<?php echo $meta_box[ 'name' ]; ?>" value="<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>" />
<p><?php echo $meta_box[ 'description' ]; ?></p>
</div>

<?php } ?>

</div>
<?php
}

function save_meta_box( $post_id ) {
global $post, $meta_boxes, $key;

foreach( $meta_boxes as $meta_box ) {
$data[ $meta_box[ 'name' ] ] = $_POST[ $meta_box[ 'name' ] ];
}

if ( !wp_verify_nonce( $_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__) ) )
return $post_id;

if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;

update_post_meta( $post_id, $key, $data );
}

add_action( 'admin_menu', 'create_meta_box' );
add_action( 'save_post', 'save_meta_box' );


I'm sorry if I am not being too specific. Let me know if you want a higher prize amount or more explanation. My impression was that it is rather easy to do since it is the same code essentially, just an updated version where you need to plug in the old stuff in the right places.

BTW: this is for a gallery theme whereby the user adds this extra info when they are writing up or editing their post (it appears in a meta box).

Look at this http://jagst3r15.com/2008/06/post-format-test-image-attached/ and youll see the stuff appearing in the meta. This is more for backend stuff so nothing will be changes on the front end i think.


Jagst3r15 comments:

I don't think adding the functions.php worked...here it is, sorry it is so long:

<?php


// Install theme options needed for awesomeness
if( $_REQUEST['activated'] == true ):
update_option('thumbnail_size_w','150');
update_option('thumbnail_size_h','150');
update_option('medium_size_w','500');
update_option('medium_size_h','375');
update_option('posts_per_page','28');
endif;

// add support for post thumbnails
add_theme_support('post-thumbnails');
add_image_size('medium_gallery',500,375,1);

// register our secondary menu
function register_menus(){

if ( function_exists( 'register_nav_menu' ) ) {
register_nav_menu( 'secondary-menu', 'Secondary Menu' );
}

}

add_action('init','register_menus');

function gallery_category_menu(){ ?>
<div id="category-menu" class="menu">
<ul id="category-nav" class="sf-menu">
<li class="home"><a href="<?php bloginfo('url'); ?>"><?php _e('Home'); ?></a></li>
<?php wp_list_categories('title_li='); ?>
</ul>
</div>
<?php
}

// add gallery menus
function childtheme_override_access() {
?>
<div id="access">
<?php
if( function_exists('wp_nav_menu') ):

$secondary = array(
'theme_location' => 'secondary-menu',
'container_class' => 'menu',
'container_id' => 'page-menu',
'menu_class' => 'sf-menu',
'menu_id' => 'page-nav',
'fallback_cb' => 'gallery_page_menu'
);

wp_nav_menu( $secondary );

$primary = array(
'theme_location' => 'primary-menu',
'container_class' => 'menu',
'container_id' => 'category-menu',
'menu_class' => 'sf-menu',
'menu_id' => 'category-nav',
'fallback_cb' => 'gallery_category_menu'
);

wp_nav_menu( $primary );

else:

gallery_page_menu();
gallery_category_menu();

endif;
?>

</div><!-- #access -->

<?php
}

add_action('thematic_access','gallery_menus',1);

// add UpThemes logo to blog title area in header
function childtheme_override_blogtitle(){

global $sc_options;

echo '<div id="blog-title"><span><a href="' . get_bloginfo('url') . '/" title="' . get_bloginfo('name') . '" rel="home"><img src="' . $sc_options->logo . '" alt="" /></a></span></div>';

}

add_action('thematic_header','childtheme_override_blogtitle',3);

// remove blog description from header
function childtheme_override_blogdescription(){

}

// remove sidebar on gallery-style pages
function remove_sidebar() {
if(is_page()){
return TRUE;
} else {
return FALSE;
}
}

add_filter('thematic_sidebar', 'remove_sidebar');

// add post thumbnail to RSS feed
function rss_post_thumbnail($content) {

global $post;
if(has_post_thumbnail($post->ID)) {
$content = get_the_post_thumbnail($post->ID,'medium') . "<br/>" . $content;
}
return $content;

}

add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');

// remove navigation above & below
function remove_navigation() {

if( is_single() ):
remove_action('thematic_navigation_above', 'thematic_nav_above', 2);
remove_action('thematic_navigation_below', 'thematic_nav_below', 2);
endif;

}

add_action('wp_head', 'remove_navigation');

//
function sticky_footer(){
echo '<div class="push"></div></div><div class="footer">';
}

add_action('thematic_abovefooter','sticky_footer',100);

// add custom WP 3.0 menus
function gallery_navigation() {

global $post;

if ( !is_single() ) : ?>

<div id="nav-below" class="navigation">

<?php if(function_exists('wp_pagenavi')):
wp_pagenavi();
else : ?>

<div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">&laquo;</span> Older posts', 'thematic')) ?></div>
<div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">&raquo;</span>', 'thematic')) ?></div>

<?php endif; ?>
</div>

<?php else: ?>

<div id="nav-below" class="navigation">
<div class="nav-previous"><?php previous_post_link('%link', '<span class="meta-nav">&laquo;</span> %title') ?></div>
<div class="nav-next"><?php next_post_link('%link', '%title <span class="meta-nav">&raquo;</span>') ?></div>
</div>
<?php
endif;
}

add_action('gallery_navigation_below', 'gallery_navigation');

// create single post content
function remove_single_post() {

remove_action('thematic_singlepost', 'thematic_single_post');

}

add_action('init', 'remove_single_post');

// override the single post
function gallery_single_post() {

global $post;

if( function_exists('p75GetVideo') ):
if( p75GetVideo($post->ID) ):
$postclass = "video";
else:
$postclass = "gallery";
endif;
endif;
?>
<div id="post-<?php the_ID(); ?>" <?php post_class($postclass); ?>>

<div class="entry-content">

<?php if(function_exists('the_ratings')) { echo the_ratings(); } ?>

<h1><?php the_title(); ?></h1>

<?php the_content( __('Read More <span class="meta-nav">&raquo;</span>', 'thematic') ); ?>

<?php do_action('gallery_navigation_below'); ?>

</div><!-- /.entry-content -->

</div><!-- /.post -->

<div class="artwork-container">

<div class="entry-artwork">

<?php do_action('entry_artwork'); ?>

</div><!-- /.entry-artwork -->

</div><!-- /.artwork-container -->
<?php
}

add_action('thematic_singlepost', 'gallery_single_post');

// decide what to show in the single post image area
function what_to_show(){

global $post;

if( function_exists( 'p75GetVideo' ) ):

if( p75HasVideo( $post->ID ) ):

echo p75GetVideo($post->ID);

else:

show_medium_image();

endif;

else:

show_medium_image();

endif;

}

function show_medium_image(){

global $post;

$url = get_post_meta($post->ID, 'web-url', true);
$custom_image = get_post_meta($post->ID, 'full-image', true);


if( $url )
echo '<a href="' . get_post_meta($post->ID, 'web-url', true) . '">';

if( $custom_image ):
echo '<img src="' . $custom_image . '" alt="' . the_title_attribute('echo=0') . '"/>';
elseif( has_post_thumbnail() ):
the_post_thumbnail('medium_gallery');
else:
echo '<img src="' . get_bloginfo('stylesheet_directory') . '/images/full-image-default.jpg" alt="' . the_title_attribute('echo=0') . '"/>';
endif;

if( $url )
echo '</a>';

}

add_action('entry_artwork','what_to_show');

// decide what to add to single post
function setup_singlepost(){

if( is_single() )
add_filter('the_content','add_metadata_to_post',10);

}

add_action('wp_head','setup_singlepost');

// add meta data to the post
function add_metadata_to_post( $content ){

$content .= '<ul class="meta">';
$content .= add_twitter_to_meta();
$content .= add_designer_to_meta();
$content .= add_weburl_to_meta();
$content .= '</ul>';

return $content;

}

// add twitter to our meta data
function add_twitter_to_meta(){

global $sc_options,$post;

if( function_exists('file_get_contents') )
$shortenedurl = file_get_contents( 'http://tinyurl.com/api-create.php?url=' . urlencode( get_permalink() ) );
else
$shortenedurl = urlencode( get_permalink() );

if( $sc_options->twitter )
return '<li class="twitter"><a href="http://www.twitter.com/home?status=' . str_replace(' ', '+', the_title_attribute('echo=0')) . '+' . $shortenedurl . '+([email protected]'. $sc_options->twitter .')" title="Share ' . the_title_attribute('echo=0') . ' on Twitter">' . _('Tweet This') . '</a></li>';

}

// add the designer to the meta data
function add_designer_to_meta(){

global $post;

if( get_post_meta($post->ID, 'designed-by') )
return '<li class="designer">Designed by: ' . get_post_meta($post->ID, 'designed-by', $single = true) . '</li>';

}

// add the web URL to the meta data
function add_weburl_to_meta(){

global $post;

if( get_post_meta($post->ID, 'web-url') )
return '<li class="site-link"><a rel="source" href="' . get_post_meta($post->ID, 'web-url', $single = true) .'">' . get_post_meta($post->ID, 'web-url', $single = true) . '</a></li><li class="delicious"><a href="http://del.icio.us/post?url=' . get_post_meta($post->ID, 'web-url', $single = true) . '&amp;' . get_the_title() . '">Bookmark This (' . get_post_meta($post->ID, 'web-url', $single = true) . ')</a></li>';

}

// remove all loops and replace them with our thumbnail loop
function remove_loops() {

remove_action('thematic_categoryloop', 'thematic_category_loop');
remove_action('thematic_tagloop', 'thematic_tag_loop');
remove_action('thematic_archiveloop', 'thematic_archive_loop');
remove_action('thematic_indexloop', 'thematic_index_loop');

}

add_action('init', 'remove_loops');

// add our thumbnail loop instead
function thumbnail_loop() {

global $post;
get_template_part( 'loop', 'thumbnail' );

}

add_action('thematic_categoryloop', 'thumbnail_loop');
add_action('thematic_tagloop', 'thumbnail_loop');
add_action('thematic_archiveloop', 'thumbnail_loop');
add_action('thematic_indexloop', 'thumbnail_loop');

// filter the page title
function gallery_page_title ($content) {

if (is_category()) {
$content = '<h1 class="page-title"><span>';
$content .= single_cat_title("", false);
$content .= '</span></h1>';
if ( !(''== category_description()) ) {
$content .= '<div class="archive-meta">';
$content .= apply_filters('archive_meta', category_description());
$content .= '</div>';
}
} elseif (is_tag()) {
$content = '<h1 class="page-title"><span>';
$content = thematic_tag_query();
$content = '</span></h1>';
}
return $content;

}

add_filter('thematic_page_title', 'gallery_page_title');

// add slider and lazyload
function gallery_slider(){

wp_enqueue_script( 'lazyload', get_bloginfo('stylesheet_directory') . '/js/jquery.lazyload.pack.js', array('jquery') );
wp_enqueue_script( 'gallery', get_bloginfo('stylesheet_directory') . '/js/gallery.js', array('jquery','lazyload') );

}

add_action('init','gallery_slider');

// add custom post header
function childtheme_post_header(){

global $sc_options;

$gall_newlength = $sc_options->new_length;

if($gall_newlength){
$time = $gall_newlength;
} else {
$time = 3;
}

if ( (time()-get_the_time('U')) <= ($time*86400) ):
echo '<div class="new"></div>';
endif;
}

// add category nicenames in body and post class
function not_singular_body_class($classes) {

global $post,$wpdb,$wp_query;

if( is_home() || is_category() || is_archive() || is_tag() ):
$classes[] = 'not-singular';
endif;

return $classes;
}
add_filter('body_class', 'not_singular_body_class');

// change the default search box text
function childtheme_search_field_value() {
return "Search";
}
add_filter('search_field_value', 'childtheme_search_field_value');

/*
Plugin Name: Custom Write Panel
Plugin URI: http://wefunction.com/2008/10/tutorial-create-custom-write-panels-in-wordpress
Description: Allows custom fields to be added to the WordPress Post Page
Version: 1.0
Author: Spencer
Author URI: http://wefunction.com
/* ----------------------------------------------*/

$new_meta_boxes = array(
"full-image" => array(
"name" => "full-image",
"std" => "",
"title" => "Path to Full-Size Image (500x375)",
"description" => "Using the \"<em>Add an Image</em>\" button, upload a 500x375 image and paste the URL here."),

"thumbnail" => array(
"name" => "thumbnail",
"std" => "",
"title" => "Path to Thumbnail Image (125x125)",
"description" => "Using the \"<em>Add an Image</em>\" button, upload a 125x125 thumbnail image and paste the URL here."),

"designed-by" => array(
"name" => "designed-by",
"std" => "",
"title" => "Designed by",
"description" => "Enter the name of the designer (if known or applicable)."),

"web-url" => array(
"name" => "web-url",
"std" => "",
"title" => "Website URL",
"description" => "Enter the full website URL (if applicable).")
);

function new_meta_boxes() {
global $post, $new_meta_boxes;

foreach($new_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'], true);

if($meta_box_value == "")
$meta_box_value = $meta_box['std'];

echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';

echo'<label style="font-weight: bold; display: block; padding: 5px 0 2px 2px" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';

echo'<input type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';

echo'<p><label for="'.$meta_box['name'].'">'.$meta_box['description'].'</label></p>';
}
}

function create_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'Gallery Post Settings', 'new_meta_boxes', 'post', 'normal', 'high' );
}
}

function save_postdata( $post_id ) {
global $post, $new_meta_boxes;

foreach($new_meta_boxes as $meta_box) {
// Verify
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}

if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}

$data = $_POST[$meta_box['name']];

if(get_post_meta($post_id, $meta_box['name']) == "")
add_post_meta($post_id, $meta_box['name'], $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'], true))
update_post_meta($post_id, $meta_box['name'], $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
}
}

add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');

?>


Arnav Joy comments:

/* try using this

i have edited the code given by you it is not full functions.php just the code that produces meta boxex

*/

$key = "key";

$meta_boxes = array(

"image" => array(

"name" => "image",

"title" => "Image",

"description" => "Using the \"Add an Image\" button, upload an image and paste the URL here. Images will be resized. This is the Article's main image and will automatically be sized."),

"tinyurl" => array(

"name" => "tinyurl",

"title" => "Tiny URL",

"description" => "Add a small URL of this post that will be used to track tweets, and share the post.")

);



function create_meta_box() {

global $key;



if( function_exists( 'add_meta_box' ) ) {

add_meta_box( 'new-meta-boxes', ucfirst( $key ) . ' Custom Post Options', 'display_meta_box', 'post', 'normal', 'high' );

}

}



function display_meta_box() {

global $post, $meta_boxes, $key;

?>



<div class="form-wrap">



<?php

wp_nonce_field( plugin_basename( __FILE__ ), $key . '_wpnonce', false, true );



foreach($meta_boxes as $meta_box) {

$data = get_post_meta($post->ID, $meta_box[ 'name' ], true);


?>



<div class="form-field form-required">

<label for="<?php echo $meta_box[ 'name' ]; ?>"><?php echo $meta_box[ 'title' ]; ?></label>

<input type="text" name="<?php echo $meta_box[ 'name' ]; ?>" value="<?php echo htmlspecialchars( $data ); ?>" />

<p><?php echo $meta_box[ 'description' ]; ?></p>

</div>



<?php } ?>



</div>

<?php

}



function save_meta_box( $post_id ) {

global $post, $meta_boxes, $key;




foreach( $meta_boxes as $meta_box ) {

$data[ $meta_box[ 'name' ] ] = $_POST[ $meta_box[ 'name' ] ];

}


if ( !wp_verify_nonce( $_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__) ) )

return $post_id;



if ( !current_user_can( 'edit_post', $post_id ))

return $post_id;

foreach( $meta_boxes as $meta_box ) {

echo $meta_box[ 'name' ].'<br>';
echo $data[ $meta_box[ 'name' ]].'<br>';
update_post_meta( $post_id, $meta_box[ 'name' ], $data[ $meta_box[ 'name' ]] );
}


}



add_action( 'admin_menu', 'create_meta_box' );

add_action( 'save_post', 'save_meta_box' );


Arnav Joy comments:

/*use following code to fetch the values*/

<?php echo $image_meta = get_post_meta($post->ID,'image',true);?>
<?php echo $tinyurl_meta = get_post_meta($post->ID,'tinyurl',true);?>


Jagst3r15 comments:

where do i add

<?php echo $image_meta = get_post_meta($post->ID,'image',true);?>
<?php echo $tinyurl_meta = get_post_meta($post->ID,'tinyurl',true);?>


Arnav Joy comments:

at the place where you want to use the values of meta field may be at single.php or at any other file where you are showing these posts

inside the loop

if(have_posts())
while(have_posts()):the_post();
---
---
endwhile;
endif;


Jagst3r15 comments:

I am using a child theme though and thought I could just add to functions.php. The code you gave does not work in there. Sorry its my fault for not explaining


Jagst3r15 comments:

maybe if you see the site you can get a better idea? Want me to send wp user and pass?


Arnav Joy comments:

check single.php