Only show metabox when date-value in other metabox is over?

probably a weird question.
I’m using the custom MetaBox and CustomFields Class by jaredatch on Github.

I have this “event-date” metabox:

$meta_boxes[] = array(
        'id'         => 'event_date',
        'title'      => 'Event Date',
        'pages'      => array( 'wr_event', ),
        'context'    => 'normal',
        'priority'   => 'high',
        'fields'     => array(
            array(
                'name' => 'Test Date Picker (UNIX timestamp)',
                'desc' => 'field description (optional)',
                'id'   => $prefix . 'event_date',
                'type' => 'text_date_timestamp',
            )
        ),
    );

I have a second metabox called “event-review”

$meta_boxes[] = array(
    'id'         => 'wr_event_review',
    'title'      => 'Event Review',
    'pages'      => array( 'wr_event', ), // Post type
    'context'    => 'normal',
    'priority'   => 'high',
    'show_names' => true, // Show field names on the left
    'fields'     => array(
        array(
            'name'    => 'Event Review',
            'id'      => $prefix . 'event_wysiwyg',
            'type'    => 'wysiwyg',
            'options' => array( 'textarea_rows' => 5, ),
        )
    ),
);

I wonder if it’s possible to show the event-review metabox only when the date is over?

Something like…

if ( date('U') > date('U', $_POST["_wr_event_date"] ) ) {
     $meta_boxes[] = array(
        'id'         => 'wr_event_review',
        'title'      => 'Event Review',

However I have no idea if this is even possible or even how I can get the current event_date that is in the input.

Any thoughts on this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Not sure about the linked class – it seems they collect metaboxes immediately and so there is no information regarding what post is being viewed.

In general though – yes it is possible. To add a metabox:

add_action( 'add_meta_boxes', 'myplugin_add_my_custom_box',10,2);

See the source code here. This add_meta_boxes hook passes two variables: the post type and the post object. You can use the post to get the post meta and then call add_meta_box where appropriate.

function myplugin_add_my_custom_box($post_type,$post){
     //Get event date as a timestamp
     $event_date = (int) get_post_meta($post->ID,'_wr_event_date',true);

     //Check date exists and is in the past. If not, return: don't add metabox.
     if( empty($event_date) || current_time('timestamp') < $event_date)
         return;

     add_meta_box( 
        'myplugin_sectionid',
        __( 'My Post Section Title', 'myplugin_textdomain' ),
        'myplugin_metabox_callback',
        'event' 
    );      
}

You’ll notice there is also a add_meta_boxes_{$post_type} hook – this is more efficient if you want to just add it to ‘event_cpt’ post types:

     add_action( 'add_meta_boxes_event_cpt', 'myplugin_add_my_custom_box',10,1);

In this case your callback only includes the $post as an argument!

Note: Avoid using php date/time functions: this will, by default, always have the timezone set to UTC. If you want the current date/time in the blogs timezone using something like current_time()

Method 2

I’d go for a JavaScript solution here. That way you have to possibility to instantly show or hide the event review metabox based on the date entered in the event date metabox.

I provided a solution for a very similar problem in which a metabox needed to be toggled based upon the chosen page template.

Please, see: Toggle admin metabox based upon chosen page template

Method 3

If you wanna show or hide entire meta box when event date is change, you can try to use Conditional Logic plugin, so you can rewrite your code like so:

    $meta_boxes[] = array(
    'id'         => 'wr_event_review',
    'title'      => 'Event Review',
    'pages'      => array( 'wr_event', ), // Post type
    'context'    => 'normal',
    'priority'   => 'high',
    'show_names' => true, // Show field names on the left
    'visible'    => array('event_date', '>', '2015-08-25'),
    'fields'     => array(
        array(
            'name'    => 'Event Review',
            'id'      => $prefix . 'event_wysiwyg',
            'type'    => 'wysiwyg',
            'options' => array( 'textarea_rows' => 5, ),
        )
    ),
);

You can change ‘2015-08-25’ to any value you want 😉


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x