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

Round 2: Custom Write Fields WordPress

  • SOLVED

Hi, so I am very sorry for before. I did not take the proper time to write up what I needed done and I think the prize was too low. Okay, so here is what I am trying to do. I am updating a <strong>child theme</strong> of Thematic I am modifying to use a newer version of these custom write fields I have, as per href="http://wefunction.com/2009/10/revisited-creating-custom-write-panels-in-wordpress/". I have attempted to redo the code myself, but do not think I did it correctly (you can see what is going on here href="http://jagst3r15.com/2008/06/post-format-test-image-attached/". The three fields I want are the path to the image, designed by, and website url. I tried pluging in the old values. The important parts of the code are at the end of the funtions.php:
/*
Plugin Name: Custom Write Panel
Plugin URI: http://wefunction.com/2009/10/revisited-creating-custom-write-panels-in-wordpress/
Description: Allows custom fields to be added to the WordPress Post Page
Version: 1.1
Author: Spencer
Author URI: http://wefunction.com
/* ----------------------------------------------*/

$key = "key";
$meta_boxes = array(
"full-image" => array(
"name" => "full-image",
"title" => "Path to the image",
"description" => "The path to the image."),
"designed-by" => array(
"name" => "designed-by",
"title" => "Designed by",
"description" => "Enter the name of the designer (if known or applicable)."),
"web-url" => array(
"name" => "web-url",
"title" => "Website URL",
"description" => "Enter the full website URL (if applicable).")
);

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' );


and in the middle (two parts):

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


and

// 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,'key', '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>';

}
.

Also the full functions.php for reference (it is 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 . '+(via+@'. $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,'key', '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/2009/10/revisited-creating-custom-write-panels-in-wordpress/
Description: Allows custom fields to be added to the WordPress Post Page
Version: 1.1
Author: Spencer
Author URI: http://wefunction.com
/* ----------------------------------------------*/

$key = "key";
$meta_boxes = array(
"full-image" => array(
"name" => "full-image",
"title" => "Path to the image",
"description" => "The path to the image."),
"designed-by" => array(
"name" => "designed-by",
"title" => "Designed by",
"description" => "Enter the name of the designer (if known or applicable)."),
"web-url" => array(
"name" => "web-url",
"title" => "Website URL",
"description" => "Enter the full website URL (if applicable).")
);

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 hope I have expalined this better. If you need more explanation or prize money let me know. I hope we can do this for the set amount though.

Answers (1)

2012-05-30

John Cotton answers:

Well the last bit of code looks wrong for a start....

Should be:


// add the designer to the meta data

function add_designer_to_meta() {
global $post;
if( $meta = get_post_meta($post->ID, 'designed-by', true) ) {
return '<li class="designer">Designed by: ' . $meta. '</li>';
}
}


// add the web URL to the meta data
function add_weburl_to_meta(){
global $post;

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


Try that and let me know what you get.


Jagst3r15 comments:

Hi John, thanks for the response. Unfortunately that did not work. Got unexpected T_VARIABLE on line 337, but I tried to just add the first part and that did not work either.


John Cotton comments:

This line was missing a comma.

return sprintf('<li class="site-link"><a rel="source" href="%1$s">%1$s</a></li><li class="delicious"><a href="http://del.icio.us/post?url=%1$s&amp;%2$s">Bookmark This (%1$s)</a></li>', $url, get_the_title() );


Jagst3r15 comments:

ahh, got this now - unexpected $end in /home/jagster/public_html/wp-content/themes/showcase/functions.php on line 515 . If you look at the tutorial http://wefunction.com/2009/10/revisited-creating-custom-write-panels-in-wordpress/ I think I messed up/ didnt do the implementation part - It think it needs to be implemented in these lines (?):

// add the designer to the meta data



function add_designer_to_meta() {

global $post;

if( $meta = get_post_meta($post->ID, 'designed-by', true) ) {

return '<li class="designer">Designed by: ' . $meta. '</li>';

}

}





// add the web URL to the meta data

function add_weburl_to_meta(){

global $post;



if( $url = get_post_meta($post->ID, 'web-url') ) {

return sprintf('<li class="site-link"><a rel="source" href="%1$s">%1$s</a></li><li class="delicious"><a href="http://del.icio.us/post?url=%1$s&amp;%2$s">Bookmark This (%1$s)</a></li>', $url, get_the_title() );


}


John Cotton comments:

You are missing a } on the end of the second function...


Jagst3r15 comments:

cool thanks John :). However, see here http://jagst3r15.com/2008/06/post-format-test-image-attached/ how it is only spitting out Array? any ideas?


John Cotton comments:

Change to

$url = get_post_meta($post->ID, 'web-url', true)

By default, get_post_meta returns an array of fields. If you know there is only one (as in this case) the "TRUE" forces a single value to be returned.


Jagst3r15 comments:

Don't I need to add the 'key' in there though? This does not work, but something like this? :

if( $url = get_post_meta($post->ID, 'key', 'web-url', true)) {


Sorry for being a pain, just trying to get it work right : )


Jagst3r15 comments:

Let me know if 1) you want the prize to be higher and 2) if you want access to my test site to make the changes yourself. No worries either way :)


John Cotton comments:

if( $url = get_post_meta($post->ID, 'web-url', true)) {

is the correct syntax


John Cotton comments:

<blockquote>Don't I need to add the 'key' in there though? </blockquote>

'web-url' is the key....


Jagst3r15 comments:

Maybe something is wrong with that guys tutorial because I have this:

// add the web URL to the meta data

function add_weburl_to_meta(){

global $post;

if( $url = get_post_meta($post->ID, 'web-url', true)) {

return sprintf('<li class="site-link"><a rel="source" href="%1$s">%1$s</a></li><li class="delicious"><a href="http://del.icio.us/post?url=%1$s&amp;%2$s">Bookmark This (%1$s)</a></li>', $url, get_the_title() );


}

}

but it's not working :x


John Cotton comments:

Firstly, have you looked in the database and checked that the values are being stored properly?

Given their names you should also be able to see them on the edit page for the particular post.

It might be worth putting this code on the page somewhere and seeing what comes out:

<?php
global $post;
print_r(get_post_custom( $post->ID ));
?>


If you can put that in the template that outputs the page you linked to, it might shed some light.


Jagst3r15 comments:

bah, nothing. I don't think it is worth it now. Nothing is working - not that it is your fault - I still don't think I explained what I am trying to do very well....

I will just give you the money for your time...how do i pick you?


Jagst3r15 comments:

Unless you want access to my site to look at it first. If you do that I will increase the prize amount. Thanks a bunch.


John Cotton comments:

:) It seems a shame to give up...you've got some output. If you're getting nothing from that last print_r that suggest you've not got it in the right place....there's nearly always from meta data for each post, even if it's just what WP put in.

Let's have one more go...

1/ Can you see the field values when you edit the post in the dashboard?

2/ Are you certain you're putting the print_r(get_post_custom...) on the correct template file for the post you're viewing (you should probably have edited single.php, but it could be another file if you're dealing with a custom post type)

3/ To be sure, let's mod one of the functions:


function add_weburl_to_meta(){
global $post;
print_r(get_post_custom( $post->ID ));
if( $url = get_post_meta($post->ID, 'web-url', true) ) {
return sprintf('<li class="site-link"><a rel="source" href="%1$s">%1$s</a></li><li class="delicious"><a href="http://del.icio.us/post?url=%1$s&amp;%2$s">Bookmark This (%1$s)</a></li>', $url, get_the_title() );
}


Put that code in, answer the questions above and post me a link to the page output.


John Cotton comments:

<blockquote>Unless you want access to my site to look at it first. If you do that I will increase the prize amount. Thanks a bunch.</blockquote>

PM me the details and I'll take a quick look


John Cotton comments:

I've just edited your code and things are working (I think?) on this page:

[[LINK href="http://jagst3r15.com/2008/06/post-format-test-image-attached/"]]http://jagst3r15.com/2008/06/post-format-test-image-attached/[[/LINK]]

The problem was that the tutorial code you are using stores everything is a "key" array (I think you were trying to make that point to me - sorry). I have left the print_r code at the top of the page to prove that point (you can find it in the single.php file in the Thematic theme and delete when you're ready).


Jagst3r15 comments:

Awesome, looks great and I think the custom_fields meta that appeared is cool, but how would I remove that if I needed too?

Also, how exactly do I give you the prize? (I added $5 - sorry my paypal is low on cash).


John Cotton comments:

<blockquote>I think the custom_fields meta that appeared is cool, but how would I remove that if I needed too? </blockquote>
See my PM to you.

<blockquote>how exactly do I give you the prize</blockquote>
Mark the question as answered and vote the prize to me :)


Jagst3r15 comments:

I'm so sorry my brain is not working today...I forgot to mark as answered and now it's open to voting or something even though i voted for you?

And how do i see my pm's?

Thanks for your patience


Jagst3r15 comments:

Nevermind found everything.