How can I display a desired date into a Wordpress page? - wordpress

I'm trying to find a way to choose a date and display it into a Wordpress page.
Simply put, that's the way this may operate:
One or several pages are input a shortcode or a PHP code via PHP insert
An operator/webmaster chooses a date from let's say a calendar or drop down
This date (formatted) shows into the pages that have the shortcode/PHP code
I do not want automatic or current date which I can do with this plugin.
Can anybody help achieve this with a plugin or a PHP snippet?

Display a custom date field in Settings->Reading:
function wpse_52414489_custom_date() {
// Add the section to reading settings so we can add our
// fields to it
add_settings_section(
'eg_custom_settings',
'My custom settings',
'',
'reading'
);
// Add the field with the names and function to use for our new
// settings, put it in our new section
add_settings_field(
'eg_custom_date',
'My custom date',
'eg_custom_date_callback',
'reading',
'eg_custom_settings'
);
// Register our setting so that $_POST handling is done for us and
// our callback function just has to echo the <input>
register_setting( 'reading', 'eg_custom_date' );
}
function eg_custom_date_callback() {
echo '<input name="eg_custom_date" id="eg_custom_date" type="date" value="' . get_option( 'eg_custom_date' ) . '" class="code" /> Explanation text';
}
add_action( 'admin_init', 'wpse_52414489_custom_date' );
Add your shortcode (EDIT: custom date format):
add_filter( 'init', function() {
add_shortcode( 'my-custom-date', function() {
return date( 'd/m/Y', strtotime( get_option( 'eg_custom_date' ) ) );
});
});
Usage:
[my-custom-date]
Output:
2018-09-18

Related

How to update Order meta data, from a WooCommerce Action

I'm trying to add some custom meta_data to a WooCommerce Order, by running a Order action.
Here is my code:
function custom_add_order_actions( $actions ){
global $theorder;
$actions['my_custom_action'] = 'My custom action';
return $actions;
}
add_action( 'woocommerce_order_actions', 'custom_add_order_actions' );
function custom_add_single_action( $order ){
// Non of these change anything on the order
$order->set_billing_first_name( 'A new test name' );
$order->update_post_meta( 'a_test_field', 'Test field value' );
update_post_meta( $order->get_id(), 'a_test_field', 'Some other value' );
// $order->save(); // I even tried adding this as well, but it doesn't change anything.
}
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
How do I change the order (or specifically, post_meta fields for an order) from inside an action?
A example
Imagine that I add a post_meta field, with the field name (key): a_test_field.
It's currently an ACF-field, but it's the same for regular WordPress custom fields.
If I change the value of the field and press 'Update', then the value changes:
So far so good. Now the value of the field is 'Foobar'.
What's wierd is that even if I do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
update_post_meta( $order->get_id(), 'a_test_field', 'A new value' );
die(); // This die is vital, to make the change in the database.
}
Then I can see the value change in the database to 'A new value'.
But if I just do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
$order->update_post_meta( 'a_test_field', 'A new value' );
// No die(); here...
}
Then the value remains 'Foobar' in the database.
Sorry but the following lightly revisited code works (Selecting the action and click on the button arrow):
add_action( 'woocommerce_order_actions', 'add_custom_order_action' );
function add_custom_order_action( $actions ){
$actions['my_custom_action'] = __('My custom action', 'WooCommerce');
return $actions;
}
add_action( 'woocommerce_order_action_my_custom_action', 'triggered_custom_order_action' );
function triggered_custom_order_action( $order ){
$order->update_meta_data( '_test_1_custom_field', 'AAFFBB9977' );
$order->save();
update_post_meta( $order->get_id(), '_test_2_custom_field', 'Some other value' );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Note: Order actions are mostly used for some other things than what you are trying to do.
Now when using a meta box with an input field (as you are showing), on submit, you should save that field value using the action hook save_post_shop_order like in those related threads:
Metabox with multi checkbox in WooCommerce admin single orders
Dynamic custom order numbers based on payment method
Metabox with multiple custom fields for WooCommerce admin order pages

How create new custom fields for my Theme in Wordpress

I have created a super simple theme. What i want to have is the ability to define an array of 3 fields for each post and page.
For example having the fields: Name, Link and a Dropdown for Type
Additionally i want to add multiple items of these "field sets" per post/page. I couldn't find any documentation on this but always results in Google which led me to WP Plugins. In general this is ood, cause in this case this seems to be possible programmatically, but bad cause i couldn't find any kind of information on this.
Hopefully someone can help me.
You are looking for custom meta boxes, with the add_meta_box() function:
Adds a meta box to one or more screens.
Source # https://developer.wordpress.org/reference/functions/add_meta_box/
And the add_meta_boxes action hook.
Fires after all built-in meta boxes have been added:
Source # https://developer.wordpress.org/reference/hooks/add_meta_boxes/
A simple example would be if we wanted to add a "Listening to..." custom meta box, to share our mood while writing a post.
<?php
add_action( 'add_meta_boxes', 'add_meta_listening_to' );
function add_meta_listening_to() {
add_meta_box(
'meta_listening_to', //id
'Listening to ...', //title
'listeningto', //callback
'post', //screen &/or post type
'normal', //context
'high', //priority
null //callback_args
);
};
function listeningto( $post ) { //function handle same as callback
$ListeningToInput = get_post_meta( $post->ID, 'ListeningToInput', true );
echo '<input name="listening_to_input" type="text" placeholder="Listening to ..." value="'. $ListeningToInput .'" style="width:100%;">';
};
add_action( 'save_post', 'save_meta_listening_to' );
function save_meta_listening_to( $post_ID ) {
if ( isset( $_POST[ 'listening_to_input' ] ) ) {
update_post_meta( $post_ID, 'ListeningToInput', esc_html( $_POST[ 'listening_to_input' ] ) );
};
}; ?>
Then to display on the front end, we would use the following:
<?php echo get_post_meta( $post->ID, 'ListeningToInput', true ); ?>
Learn more
Learn more about the Custom Meta Boxes # https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/

Wordpress Event posts

I Need to make a event list in wordpress, show up by: "today" - "week" - "month" and all this show just my post. Witch is the best way to do that?
The author must just add the date and time of event and its appear in the current category us well.
If you just want to add the possibility for the author to add the date of an event, use the add_meta_boxes_{$post_slug} action.
This action allows to configure and print a new metabox in any post (choosen) post screen.
add_action( 'add_meta_boxes_post', 'adding_custom_meta_boxes');
add_action('save_post', 'save_sticked_metabox');
function adding_custom_meta_boxes(){
add_meta_box(
'sticked-list',
__( 'Featured list', $this->plugin_text_domain ),
'sticked_list_Render',
'post',
'side',
'default'
);
}
function sticked_list_Render(){
global $post;
$sticked = get_post_meta($post->ID, 'sticked', true);
$checked = '';
if($sticked != '' && isset($sticked)){
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="sticked" value="true" <?php echo $checked;?> > <?php _e('Check to set as pre-defined list', $this->plugin_text_domain);?>
<?
}
function save_sticked_metabox($post_id, $post){
if(isset($_POST['sticked'])){
update_post_meta($post_id, 'sticked', 'true');
}
else{
delete_post_meta($post_id, 'sticked');
}
}
This is just an example to show how to print a new metabox in the edit-post admin screen. You'll need to change the "sticked_list_render" to print a date & time picker.

How to populate wp-admin/post.php file during edit?

In post.php page of wp-admin I like to populate a post meta while the page is loading. For that I tried the following
add_action('load-post.php', 'show_buy_ticket_date');
function show_buy_ticket_date() {
$screen = get_current_screen();
if ($screen->post_type === 'ai1ec_event') {
$post_id = filter_input(INPUT_GET, 'post');
$buy_ticket_date_unix_timestamp = get_post_meta($post_id,
'buy_ticket_date', TRUE);
}
}
Here I fetched the post's meta value. But How can I populate it in one of the field in the form once edit page is loaded / before loading.
For e.g. the field looks like this
<input class="ai1ec-date-input ai1ec-form-control datepicker hasDatepicker" name="ai1ec_admin_buy_ticket-date-input" id="ai1ec_admin_buy_ticket-date-input" type="date">
You need to convert the timestamp to a date value suitable for input taking into account that, provided that the timestamp is in UTC time, you probably want to align it with your current timezone.
Finally, you pass the date value to input with the value attribute:
date_default_timezone_set( 'Europe/Helsinki' );
$buy_ticket_date = date( 'Y-n-j', intval( $buy_ticket_date_unix_timestamp ));
<input
class="ai1ec-date-input ai1ec-form-control datepicker hasDatepicker"
name="ai1ec_admin_buy_ticket-date-input"
id="ai1ec_admin_buy_ticket-date-input"
type="date" ,
value="<?php echo $buy_ticket_date; ?>
>
UPDATE:
Given the limitation that input is in some third-party code, I would update the field via JavaScript:
Pass $buy_ticket_date with localized JavaScript, PHP:
add_action( 'wp_enqueue_scripts', 'my_scripts' ) );
function my_scripts() {
// ... get $buy_ticket_date_unix_timestamp based on post ID
date_default_timezone_set( 'Europe/Helsinki' );
$buy_ticket_date = date( 'Y-n-j', intval( $buy_ticket_date_unix_timestamp ));
wp_register_script( 'my_script', MY_PLUGIN_URL . 'js/script.js', array( 'jquery' ) );
wp_localize_script( 'my_script', 'buy_ticket_date', $buy_ticket_date );
wp_enqueue_script( 'my_script' );
}
and JavaScript (script.js):
/*global document, jQuery */
(function ($) {
'use strict';
$(document).ready(function () {
$('#ai1ec_admin_buy_ticket-date-input').val(buy_ticket_date);
});
}(jQuery));

create custom field and upload image in wordpress

i'm trying to learn the way for uploading image file through custom field but cant get the easiest code to do it. i just done a bit here:
add_action('admin_init', 'create_image_box');
function create_image_box() {
add_meta_box( 'meta-box-id', 'Image Field', 'display_image_box', 'post', 'normal', 'high' );
}
//Display the image_box
function display_image_box() {
global $post;
$image_id = get_post_meta($post->ID,'xxxx_image', true);
echo 'Upload an image: <input type="file" name="xxxx_image" id="xxxx_image" />';
// Upload done: show it now...(as thmbnail or 60 x 50)
anybody please take me to next step and show the way to display the image in blog page too.
Lets go Stepwise here:
Create custom field Meta Box for inserting Image Url in post type => post.
Update/Save the custom field value in back end.
Display the custom field value in front end.
Seeing your code it seems that you are missing #2. Try the code below to save custom field:
function save_joe_details($post_id){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
update_post_meta($post->ID, "custom_field_image", $_POST["custom_field_image"] );
}
add_action('save_post', 'save_joe_details');
Code for #3 that displaying the custom field will be:
<?php global $post;
$custom_image = get_post_custom($post->ID); ?>
<img src="<?php echo $custom_image["custom_field_image"][0] ?>" />
you could try wrap it in wp_get_attachment_url like this:-
wp_get_attachment_url( $custom_image["custom_field_image"][0] );

Resources