Wrox Programmer Forums
|
BOOK: Professional WordPress
This is the forum to discuss the Wrox book Professional WordPress by Hal Stern, David Damstra, Brad Williams; ISBN: 9780470560549
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional WordPress section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2010, 05:20 PM
Registered User
 
Join Date: Jul 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Post Products plugin generates error...

I downloaded the sample code for the Post Products plugin, posted it to my WordPress test server, activated it, and when I try to preview or view the page, I get the following warnings/errors:

Code:
Warning: Missing argument 2 for pp_save_meta_box() in /public_html/wordpress/test/wp-content/plugins/post-products/post-products.php  on line 136

Notice: Undefined variable: post in /public_html/wordpress/test/wp-content/plugins/post-products/post-products.php on line 138

Notice: Trying to get property of non-object in /public_html/wordpress/test/wp-content/plugins/post-products/post-products.php on line 138
Line 136 is:


Code:
function pp_save_meta_box($post_id,$post) {
Line 138 is:


Code:
	if($post->post_type == 'revision') { return; }
I assume it is having trouble finding the $post variable/value but other than a global $post being declared at the top of the file, I can't see where this is being set... it should be passed in via the function call and since the plugin doens't call the function, I'm not sure where the parameters are being set.

What am I missing?

Last edited by mb2i; July 22nd, 2010 at 05:22 PM.. Reason: added code tags
 
Old July 23rd, 2010, 10:55 AM
Wrox Author
 
Join Date: Mar 2010
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey mb2i, nice catch! The issue stems when creating a new post. Since it is a new post the global $post variable is empty, thus the errors. You can fix this issue by updating the pp_save_meta_box function to the below code:

PHP Code:
function pp_save_meta_box($post_id) {
    global 
$post;
    
    
// if post is a revision skip saving our meta box data
    
if( isset( $post) && $post->post_type == 'revision') { return; }
    
    
// process form data if $_POST is set
    
if(isset($_POST['pp_sku']) && $_POST['pp_sku'] != '') {

        
// save the meta box data as post meta using the post ID as a unique prefix
        
update_post_meta($post_id,'pp_sku'esc_attr($_POST['pp_sku']));
        
update_post_meta($post_id,'pp_price'esc_attr($_POST['pp_price']));
        
update_post_meta($post_id,'pp_weight'esc_attr($_POST['pp_weight']));
        
update_post_meta($post_id,'pp_color'esc_attr($_POST['pp_color']));
        
update_post_meta($post_id,'pp_inventory',esc_attr($_POST['pp_inventory']));

    }

Notice I removed $post as a function parameter and instead bring in the global $post value. I also check if the $post variable is set before checking its value. I'll update the downloadable code as well.

Thanks again!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Try catch on sql database error(re-post) Andy Woodward BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 March 17th, 2009 02:22 PM
xslt_create occasionally generates Fatal Error csbdeady XSLT 1 March 16th, 2006 01:35 AM
Post Method Error spitz Classic ASP Databases 2 June 10th, 2005 04:31 AM
Catch POST error ? Mantis HTML Code Clinic 1 December 18th, 2004 06:37 PM
DLL generates Database read-only error Howard Classic ASP Databases 0 July 19th, 2003 08:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.