Custom meta box on venue pages …Trying to get property of non-object

WordPress Event Management, Calendars & Registration Forums General Question Custom meta box on venue pages …Trying to get property of non-object

This topic contains 3 replies, has 2 voices, and was last updated by  Joy Katzen-Guthrie 7 years, 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #29358

    I am — I think — following the instructions on various pages in the forum for adding meta box and fields to the venue. They work in as far as they are there and when they are saved they are there. However, upon the creation of a new venue I get:

    Trying to get property of non-object … for every one of them.

    Please tell me what I’m doing wrong. I use ACF and CMB2 and never needed to do this. It’s super frustrating.

        <?php
    
    
    add_action( 'add_meta_boxes', 'venue_event_venue_metabox');
    function venue_event_venue_metabox(){
         add_meta_box( 'venue_contact_mb', 'Contact', 'venue_contact_callback', 'event_page_venues', 'normal', 'high');
    }
    
    
    function venue_contact_callback( $venue ){
    
    
        $website       = eo_get_venue_meta($venue->term_id, '_website', true);
    
        $url           = eo_get_venue_meta($venue->term_id, '_venueurl', true);
    
        $email         = eo_get_venue_meta($venue->term_id, '_email', true);
    
        $contact       = eo_get_venue_meta($venue->term_id, '_contact', true );
    
        $telephone     = eo_get_venue_meta($venue->term_id, '_telephone', true);
    
    
        $rabbi         = eo_get_venue_meta($venue->term_id, '_rabbi', true);
    
        $cantorial     = eo_get_venue_meta($venue->term_id, '_cantorial', true);
    
    
        $reverend     = eo_get_venue_meta($venue->term_id, '_reverend', true);
    
        $instructor   = eo_get_venue_meta($venue->term_id, '_instructor', true);
    
        $building     = eo_get_venue_meta($venue->term_id, '_building', true);
    
        //* Class Instructor/Concert Artist ====
    
        wp_nonce_field( 'venue_save_venue_contact_meta', 'venue_event_venue_nonce' );
    
        //begin form html
        ?>
    
         <!-- form html goes here -->
    
        <?php
    
    
    }
    
    
    
    // Update venue meta
    add_action ('eventorganiser_save_venue','venue_save_venue_contact_meta');
    function venue_save_venue_contact_meta( $venue_id ){
    
        //* If our nonce isn't present just silently abort.    
        if( ! isset( $_POST['venue_event_venue_nonce'] ) ) return;
    
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    
        //* Check permissions
        $tax = get_taxonomy( 'event-venue');
        if ( ! current_user_can( $tax->cap->edit_terms ) ) return;
    
        //* Check nonce
        check_admin_referer( 'venue_save_venue_contact_meta', 'venue_event_venue_nonce' );
    
        //* Retrieve meta value(s)
        $website     = $_POST['website'];
        $url         = $_POST['weburl'];
        $email       = $_POST['email'];
        $telephone   = $_POST['telephone'];
        $contact     = $_POST['contact'];
        $rabbi       = $_POST['rabbi'];
        $cantorial   = $_POST['cantorial'];
    
        $building   = $_POST['building'];
        $instructor = $_POST['instructor'];
    
        //* Update venue meta
        eo_update_venue_meta( $venue_id,'_website', $website );
        eo_update_venue_meta($venue_id,  '_venueurl',      $url );
        eo_update_venue_meta($venue_id,  '_email',         $email );
        eo_update_venue_meta($venue_id,  '_telephone',     $telephone );
        eo_update_venue_meta($venue_id,  '_contact',       $contact );
        eo_update_venue_meta($venue_id,  '_rabbi',       $rabbi );
        eo_update_venue_meta($venue_id,  '_cantorial',   $cantorial );
        eo_update_venue_meta($venue_id,  '_building',     $building );
        eo_update_venue_meta($venue_id,  '_instructor',   $instructor );
    
    
        return;
    }
    
    Joy Katzen-Guthrie
    #29391

    Hi Joy,

    My apologies, there is a slight bug in the tutorial that you were following. When editing an existing value your callback function venue_contact_callback() is passed the corresponding venue object, from which you get the term_id and use that to retrieve any stored data on the venue.

    But for a new venue, that value is false. Since the venue doesn’t exist yet, you can’t use $venue->term_id or eo_get_venue_meta(). The simple solution is to conditionally set your data variables ($website,$url, etc) based on whether $venue is an object or false:

    function venue_contact_callback( $venue ){
        if ( ! $venue ) {
            $website = $url = $email = $contact = $telephone = $rabbi = $cantorial = $reverend = $intstructor = $building = '';
        } else {
            $website      = eo_get_venue_meta($venue->term_id, '_website', true);
            $url          = eo_get_venue_meta($venue->term_id, '_venueurl', true);
            $email        = eo_get_venue_meta($venue->term_id, '_email', true);
            $contact      = eo_get_venue_meta($venue->term_id, '_contact', true );
            $telephone    = eo_get_venue_meta($venue->term_id, '_telephone', true);
            $rabbi        = eo_get_venue_meta($venue->term_id, '_rabbi', true);
            $cantorial    = eo_get_venue_meta($venue->term_id, '_cantorial', true);
            $reverend     = eo_get_venue_meta($venue->term_id, '_reverend', true);
            $instructor   = eo_get_venue_meta($venue->term_id, '_instructor', true);
            $building     = eo_get_venue_meta($venue->term_id, '_building', true);
        }
        // ... rest of function ...
    }
    

    In the above, if we are creating a new venue, I have set all the variables to any empty string, but you can specify more appropriate default values for each variable if you wish.

    Stephen Harris
    #29401

    Super! Will apply.

    Joy Katzen-Guthrie
    #29406

    Resolved.

    Joy Katzen-Guthrie
Viewing 4 posts - 1 through 4 (of 4 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.