Support Forum
I created a simple shortcode to display custom fields of a custom post but it does not work.
I created the custom post "events".
I created a template with elementor and on this template I want to diplay the shortcode
[datesofevent] to get "from 12/03/2021 to 13/02/2021".
But it does not work and I don't know why. In fact I don't get any error message, it's just the shortcode which is not seen as a shortcode.
add_shortcode( 'datesofevent', function() {
$post_id = get_the_ID();
// Get the dates
$startdate = get_post_field( 'ci_event_start_date', $post_id );
$enddate = get_post_field( 'ci_event_end_date', $post_id );
// return
return "from $startdate to $enddate" ;
}
);
Could you help me please?
Hi,
Where do you add the code? In the file functions.php in the theme folder?
If you use the Oxygen Builder, it prevents the theme's file loaded. You can use the plugin Code Snippets to run the custom functions.
https://wordpress.org/plugins/code-snippets/
Thank you.
I was using "script organizer" and it didn't work. With code snippet, it works.
I have another issue with the dates format.
I want to change the format and I failed.
I added a line to check the date: echo date('l jS \of F Y',$startdate)
So now, it's like that:
add_shortcode( 'datesofevent', function() {
$post_id = get_the_ID();
// Get the dates
$startdate = get_post_field( 'ci_event_start_date', $post_id );
$enddate = get_post_field( 'ci_event_end_date', $post_id );
echo date('l jS \of F Y',$startdate);
// return
return "from $startdate to $enddate";
}
);
But the result is the following:
Thursday 1st of January 1970
from 2021-05-24 to 2021-05-18
It's like $startdate was empty but it's not as it's well displayed in the following line.
My metabox is like that:
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Fields for Events', 'your-text-domain' ),
'id' => 'fields_for_events',
'post_types' => ['event'],
'fields' => [
[
'name' => __( 'Organizer', 'your-text-domain' ),
'id' => $prefix . 'ci_event_organizer',
'type' => 'text',
],
[
'name' => __( 'Start date', 'your-text-domain' ),
'id' => $prefix . 'ci_event_start_date',
'type' => 'date',
'timestamp' => true,
],
[
'name' => __( 'End date', 'your-text-domain' ),
'id' => $prefix . 'ci_event_end_date',
'type' => 'date',
'timestamp' => true,
],
[
'name' => __( 'Start Time', 'your-text-domain' ),
'id' => $prefix . 'ci_event_start_time',
'type' => 'time',
],
[
'name' => __( 'End time', 'your-text-domain' ),
'id' => $prefix . 'ci_event_end_time',
'type' => 'time',
],
[
'name' => __( 'Location name', 'your-text-domain' ),
'id' => $prefix . 'ci_event_location_name',
'type' => 'text',
],
[
'name' => __( 'Location website', 'your-text-domain' ),
'id' => $prefix . 'ci_event_location_website',
'type' => 'url',
],
[
'name' => __( 'Location address', 'your-text-domain' ),
'id' => $prefix . 'ci_event_location_address',
'type' => 'text',
],
[
'name' => __( 'Location map', 'your-text-domain' ),
'id' => $prefix . 'ci_event_location_map_osm',
'type' => 'map',
'api_key' => 'AIzaSyB10BYJnnZJ1WuLERm9IsGVD2xb0mPiFsk',
'address_field' => 'ci_event_location_address',
],
[
'name' => __( 'Website to buy ticket', 'your-text-domain' ),
'id' => $prefix . 'ci_event_website_to_buy_ticket',
'type' => 'url',
],
[
'name' => __( 'Performer name', 'your-text-domain' ),
'id' => $prefix . 'ci_event_performer_name',
'type' => 'text',
],
[
'name' => __( 'Event status', 'your-text-domain' ),
'id' => $prefix . 'ci_event_status',
'type' => 'checkbox_list',
'options' => [
'EventCancelled' => __( 'EventCancelled', 'your-text-domain' ),
'EventMovedOnline' => __( 'EventMovedOnline', 'your-text-domain' ),
'EventPostponed' => __( 'EventPostponed', 'your-text-domain' ),
'EventRescheduled' => __( 'EventRescheduled', 'your-text-domain' ),
'EventScheduled' => __( 'EventScheduled', 'your-text-domain' ),
],
'inline' => true,
],
[
'name' => __( 'Event attendance mode', 'your-text-domain' ),
'id' => $prefix . 'ci_event_attendance_mode',
'type' => 'checkbox_list',
'options' => [
'OfflineEventAttendanceMode' => __( 'OfflineEventAttendanceMode', 'your-text-domain' ),
'OnlineAttendanceMode' => __( 'OnlineAttendanceMode', 'your-text-domain' ),
'MixedEventAttendanceMode' => __( 'MixedEventAttendanceMode', 'your-text-domain' ),
],
'inline' => true,
],
[
'name' => __( 'Gallery', 'your-text-domain' ),
'id' => $prefix . 'ci_event_gallery',
'type' => 'image',
'label_description' => __( 'Upload up to 5 images', 'your-text-domain' ),
'max_file_uploads' => 5,
'force_delete' => true,
'upload_dir' => '/home/creativinn/public_html/\\events',
],
],
];
return $meta_boxes;
}
Hi,
The function get_post_field() get the post fields such as post_type
, post_status
, post_content
. You can see on the documentation https://developer.wordpress.org/reference/functions/get_post_field/
To get the post meta (custom field), you can use the function get_post_meta() or helper function rwmb_meta()
Hi,
It does not work either.
Here is my new code:
<?php
add_shortcode( 'datesofevent', function() {
$post_id = get_the_ID();
// Get the dates
$startdate = get_post_field( 'ci_event_start_date', $post_id );
$enddate = get_post_field( 'ci_event_end_date', $post_id );
$startdate1 = rwmb_meta( 'ci_event_start_date', $post_id ) ;
echo date('l jS \of F Y ', $startdate1);
// return
return "startdate = $startdate or startdate1 = $startdate1" ;
}
);
?>
And the result:
Thursday 1st of January 1970
startdate = 2021-05-24 or startdate1 = 2021-05-24
So when I want to format the date, the value is not right.
Hi,
There are two points in your code:
$startdate1 = rwmb_meta( 'ci_event_start_date', '', $post_id ) ;
$startdate1
in the timestamp format. Please double-check this point.Thank you very much.
Now it works like that:
<?php
add_shortcode( 'datesofevent', function() {
$post_id = get_the_ID();
// Get the dates
$startdate = get_post_field( 'ci_event_start_date', $post_id );
$enddate = get_post_field( 'ci_event_end_date', $post_id );
echo "from ", date('l jS \of F Y ', $startdate), " to ", date('l jS \of F Y ', $enddate);
// return
return ;
}
);
?>
I wanted to write :
return "from ". date('l jS \of F Y ', $startdate). " to ". date('l jS \of F Y ', $enddate);
but it didn't work. Si I let return alone and included a "echo xxxxx"
Sorry but I have a new question related to the shortcodes. Do I need to sanitize the data in the shortcode. I thought they were already sanitized in the metaboxes.
If yes, I understand that I have to do it for every shortcode or function I have created? Thank you for your clarification.
Hi again
I eventually made this code:
<?php
add_shortcode( 'datesofevent', function() {
$post_id = get_the_ID();
// Get the dates
$startdate = get_post_field( 'ci_event_start_date', $post_id );
$enddate = get_post_field( 'ci_event_end_date', $post_id );
$datesofevent = "from ". date('l jS \of F Y ', $startdate) . " to ". date('l jS \of F Y ', $enddate);
sanitize_text_field( $datesofevent ) ;
// return
return $datesofevent ;
}
);
?>
Please, could you tell me if it is well sanitized?
Hi,
Custom fields created by Meta Box have the default sanitize callbacks, you can read more here https://docs.metabox.io/sanitization/.
And sanitization use for input data, for output data, you should use the escaping output. Follow on WordPress documentation https://developer.wordpress.org/themes/theme-security/data-sanitization-escaping/
Hi,
Thank you for your answer.
It's what I tried to use with sanitize_text_field( $datesofevent ) in my code. Is it correct?
Do I have to replace my return $datesofevent by return esc_textarea( $datesofevent ) ?
Thank you very much.