Wordpress Event posts - wordpress

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.

Related

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/

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

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

My custom add_meta_box is not showing up in wordpress admin panel

New to Wordpress and struggling with the meta boxes. Unable to get my piece of code to work/show, nor was I able find examples that work. Nothing just wants to show up in the admin panel and have no clue what I'm missing / doing wrong.
Until now it looks like it's not triggering the callback function or something.
What I want to do:
Show a checkbox to hide the page's title
What I see:
No checkbox is showing up on the page edit screen
My code in functions.php:
function twentyseventeenchild_hide_title() {
add_meta_box(
'no-title', // Unique id
__( 'Hide title' ), // Title
'twentyseventeenchild_hide_title_callback', // Callback
'post', // Screen (such as post type, link or comment)
'normal', // Context (normal, advanced, side)
'default' // Priority (default, core, high, low)
// Callback arguments
);
}
add_action( 'add_meta_boxes', 'twentyseventeenchild_hide_title' );
/**
* Meta box display callback.
*
* #param WP_Post $post Current post object.
*/
function twentyseventeenchild_hide_title_callback( $post ) {
$meta = get_post_meta($post->ID, '_title', true);
?>
<label><?php __('Hide title') ?></label>
<input id="no_title" type="checkbox" autocomplete="off" value="<?=esc_attr($meta)?>" name="page_title">
<?php
}
You're using checkbox incorrectly.
See input checkbox: https://www.w3schools.com/tags/att_input_type_checkbox.asp
Your method need to be like:
function twentyseventeenchild_hide_title_callback( $post ) {
$meta = get_post_meta($post->ID, '_title', true);
?>
<input id="no_title" type="checkbox" <?php ($meta === '1' ? 'selected' : ''); ?> name="page_title">
<label for="no_title"><?php _e('Hide title') ?></label>
<?php
}
_e() is for translate and echo, __() only return string and not print it.
($meta === '1' ? 'selected' : ''); is because when you save metabox value truewill be saved as string 1. With this ternary you can show as selected this metabox if value is euqals 1 (as string).

Allow one cart item before place order button click or proceed to checkout button click

I want to disallow place order if cart item is more than two on click of proceed to checkout or Place Order button click in woocommerce.
I do not want to check it on add to cart validation check, please any one can guide me about it?
You can use WooCommerce Min/Max Quantities extension to set a minimum and maximum quantity required to checkout.
or
You can set custom code to your functions.php theme file by using "woocommerce_after_checkout_validation" filter and can use $posted array to check value and set validation,
add_action('woocommerce_after_checkout_validation', 'rei_after_checkout_validation');
function rei_after_checkout_validation( $posted ) {
// do all your logics here...
}
function custom_checkout_button_action(){
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$total = 0;
foreach($items as $item => $values) {
$total = $values['quantity'];
}
if($total>=2){
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}
else{
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}
}
add_action('woocommerce_proceed_to_checkout', 'custom_checkout_button_action');
If you are using butto you can change your HTML accordingly to disable button instead of link in above code on check out page before place an order page.

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