Datepicker and custom contact form (custom wp plugin) - wordpress

thank you for the help in advance. I have been trying to find tutorial about building my own contact form for wordpress (without plugin). I thought i found one (part of the question).
<?php
/*
Plugin Name: Self Catering Contact form plugin
Plugin URI: http://example.com
Description: Simple non-bloated WordPress Contact Form use [contact_form]shortcode
Version: 1.0
Author: Agbonghama Collins
Author URI: http://w3guy.com
*/
function html_form_code() {
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
echo '<p>';
echo 'Your Name (required) <br />';
echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p>';
echo 'Your Email (required) <br />';
echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p>';
echo 'Subject (required) <br />';
echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p>';
echo 'Your Message (required) <br />';
echo '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
echo '</p>';
echo 'Select date <br />';
echo '<p><input type="text" id="datepicker" name="datepicker" value="' . ( isset( $_POST["datepicker"] ) ? esc_attr( $_POST["datepicker"] ) : '' ) . '" size="40" />'.'</p>';
echo '<p><input type="submit" name="cf-submitted" value="Send"/></p>';
echo '</form>';
}
function deliver_mail() {
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
// sanitize form values
$name = sanitize_text_field( $_POST["cf-name"] );
$email = sanitize_email( $_POST["cf-email"] );
$subject = sanitize_text_field( $_POST["cf-subject"] );
$message = esc_textarea( $_POST["cf-message"] );
$datepicker = sanitize_text_field($_POST["datepicker"]);
// get the blog administrator's email address
$to = 'xx#xx.co.uk';
$headers = "From: example#yourdomainname.com" . "\r\n";
// If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $message, $headers ) ) {
echo '<div>';
echo '<p class="red">sent</p>';
echo '</div>';
} else {
echo 'An unexpected error occurred';
}
}
}
function cf_shortcode() {
ob_start();
deliver_mail();
html_form_code();
return ob_get_clean();
}
add_shortcode( 'contact_form', 'cf_shortcode' );
This is what my contact-form.php looks like. My first problem that it doesnt send the email - or it doesnt arrive even tho it says that the mail was sent. I have read a few things for example some hosting providers dont allow you to use different email address to your domain ones... Could somebody explain this?
If you still have enough patience left, could somebody guide me through how to implement datepicker (which i thought i have done), and how to handle or get the data from it to be included in the email?
Thank you very much for your time...

Related

Custom plugin with mailchimp API

Hi everyone I need help I am new to the API and also new when it comes to building a custom plugin in WordPress. I have a task that I want to achieve. I have a form and when the users submit it, it's added to the MailChimp for subscribers. It's all working but I don't want to add the api key and list id in the code. I want to add it on the admin side. It means I want it to be dynamic. Here's the code . The code is not mine. I just combined this code that I got on the internet, so credits to them.
/*
Plugin Name: API
Description: Simple form connecting to API
Version: 1.0
Author:
*/
// i already added a admin menu where the api key and list id should be added
add_action('admin_menu', 'test_plugin_setup_menu');
function test_plugin_setup_menu(){
add_menu_page( 'Test Plugin Page', 'API', 'manage_options', 'test-plugin', 'test_plugin' );
}
//this is the admin menu and also the form for api key and list id
function test_plugin() {
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
echo '<p>';
echo 'API key (required) <br/>';
echo '<input type="text" name="api_key" autocomplete="off"" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["api_key"] ) ? esc_attr( $_POST["api_key"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p><input type="submit" name="submit" value="add key"></p>';
echo '</form>';
}
`
//this is the form in the frontend where the user can submit and subscribe
function form_for_api() {
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
echo '<p>';
echo 'Name (required) <br/>';
echo '<input type="text" name="users_name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["users_name"] ) ? esc_attr( $_POST["users_name"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p>';
echo 'Email (required) <br/>';
echo '<input type="email" name="users_email" value="' . ( isset( $_POST["users_email"] ) ? esc_attr( $_POST["users_email"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p>';
echo 'Phone (required) <br/>';
echo '<input type="text" name="users_phone" ' . ( isset( $_POST["users_phone"] ) ? esc_attr( $_POST["users_phone"] ) : '' ) . '" size="40" />';
echo '</p>';
echo '<p><input type="submit" name="send-form" value="Send"></p>';
echo '</form>';
}
function send_mail() {
// if the submit button is clicked, add to subscriber
if ( isset( $_POST['send-form'] ) ) {
$apiKey = 'api key should not to be put here it should be in the admin side';
$users_name = $_POST['users_name']; // the user name we are going to subscribe
$users_email = $_POST['users_email']; // the user email we are going to subscribe
$users_phone = $_POST['users_phone']; // the user phone we are going to subscribe
$list_id = 'list id'; // List / Audience ID
$server = explode( '-', $apiKey );
$url = 'https://' . $server[1] . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/';
$response = wp_remote_post(
$url,
[
'method' => 'POST',
'data_format' => 'body',
'timeout' => 45,
'headers' => [
'Authorization' => 'apikey ' . $apiKey,
'Content-Type' => 'application/json; charset=utf-8',
],
'body' => json_encode(
[
'email_address' => $users_email,//email
'status' => 'subscribed',
'status_if_new' => 'subscribed',
'merge_fields' => [
'FNAME' => $users_name,// first name
'PHONE' => $users_phone//phone
]
]
)
]
);
}
}
function api_shortcode() {
ob_start();
send_mail();
form_for_api();
return ob_get_clean();
}
add_shortcode( 'form-with-api', 'api_shortcode' );
?>

Adding Custom Fields In wordpress add new post page

I am Developing a wordpress testimonials plugin.
I created a custom post type also display and working proper. But now I want to add more fields like "Company Name", "Websites". I know I can manage from this by adding custom fields, but I want to add a separate section for that.
Can anyone help me figure out how to do this?
You can also implement meta boxes which will be displayed on the right hand sidebar the same as you have categories displayed.
You can use "Advanced Custom Fields", which is a perfect solution.
// Save the Metabox Data
function wpt_save_events_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$events_meta['_address'] = $_POST['_address'];
$events_meta['_phone'] = $_POST['_phone'];
$events_meta['_type'] = $_POST['_type'];
$events_meta['_cap'] = $_POST['_cap'];
// Add values of $events_meta as custom fields
foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields
// The Event address Metabox
function wpt_events_address() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the address data if its already been entered
$address = get_post_meta($post->ID, '_address', true);
// Echo out the field
echo '<input type="text" name="_address" value="' . $address . '" class="widefat" />';
}
function wpt_events_phone() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the address data if its already been entered
$phone = get_post_meta($post->ID, '_phone', true);
// Echo out the field
echo '<input type="text" name="_phone" value="' . $phone . '" class="widefat" />';
}
function wpt_events_type() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the address data if its already been entered
$type = get_post_meta($post->ID, '_type', true);
// Echo out the field
echo '<input type="text" name="_type" value="' . $type . '" class="widefat" />';
}
function wpt_events_cap() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the address data if its already been entered
$cap = get_post_meta($post->ID, '_cap', true);
// Echo out the field
echo '<input type="text" name="_cap" value="' . $cap . '" class="widefat" />';
}
$events_meta['_address'] = $_POST['_address'];
$events_meta['_phone'] = $_POST['_phone'];
$events_meta['_type'] = $_POST['_type'];
$events_meta['_cap'] = $_POST['_cap'];
add_action( 'add_meta_boxes', 'add_events_metaboxes' );
// Add the Events Meta Boxes
function add_events_metaboxes() {
add_meta_box('wpt_events_address', 'Address', 'wpt_events_address', 'events', 'normal', 'high');
add_meta_box('wpt_events_phone', 'Phone', 'wpt_events_phone', 'events', 'normal', 'high');
add_meta_box('wpt_events_type', 'Email', 'wpt_events_Type', 'events', 'normal', 'high');
add_meta_box('wpt_events_cap', 'Capacity', 'wpt_events_cap', 'events', 'normal', 'high');
}

Wordpress Widget - Need to list Pages

I have a wordpress widget where im saving the options, Title + Copy + URL
The url is generated by listing the wordpress pages
The below code saves and works fine on the front end. But in the widget settings panel when you save it reverts back to select page, i need it to display the previously selected item that it saved.
// Sidebar CTA Widget
class SidebarCTAWidget extends WP_Widget {
function SidebarCTAWidget() {
$widget_ops = array('classname' => 'SidebarCTAWidget', 'description' => 'Editable Sidebar CTAs' );
$this->WP_Widget('SidebarCTAWidget', 'Sidebar Call to Action', $widget_ops);
}
function form($instance) {
$defaults = array( 'title' => '', 'copy' => '', 'url' => '');
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
$copy = $instance['copy'];
$url = $instance['url'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title:
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('copy'); ?>">Copy:
<textarea rows="5" class="widefat" name="<?php echo $this->get_field_name( 'copy' ); ?>" / ><?php echo esc_attr( $copy ); ?></textarea>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">URL:
<select class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" name="page-dropdown">
<option value=""> <?php echo esc_attr( __( 'Select Page' ) ); ?></option>
<?php
$pages = get_pages();
foreach ( $pages as $page ) {
$option = '<option value="' . get_page_link( $page->ID ) . '">';
$option .= $page->post_title;
$option .= '</option>';
echo $option;
}
?>
</select>
</label>
</p>
<?php }
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['copy'] = strip_tags( $new_instance['copy'] );
$instance['url'] = strip_tags( $new_instance['url'] );
return $instance;
}
function widget($args, $instance){
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
$copy = empty($instance['copy']) ? ' ' : apply_filters('widget_title', $instance['copy']);
$url = empty($instance['url']) ? ' ' : apply_filters('widget_title', $instance['url']);
if (!empty($title))
echo '<div class="clearfix sidebar-content"><h3>' . $title . '</h3>';
;
// This is the HTML
echo '<p>' . $copy . '</p></div>';
echo '<p class="sidebar-link">Find Out More</p>';
// END
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("SidebarCTAWidget");') );
Thanks to the help of #b__ i managed to work out how to compare the saved url in the database and mark it as selected
$option = '<option '. (get_page_link( $page->ID ) == attribute_escape($url) ? "selected='selected'":"").'value="' . get_page_link( $page->ID ) . '">';

WordPress - Display Current Date & Time Based on Website Timezone

I'm trying to display the current date and time on my WordPress website. I want the date and time to reflect the Timezone that has been set in the WordPress admin area.
I found this code in options-general.php:
<tr>
<th scope="row"><?php _e('Date Format') ?></th>
<td>
<fieldset><legend class="screen-reader-text"><span><?php _e('Date Format') ?></span></legend>
<?php
$date_formats = array_unique( apply_filters( 'date_formats', array(
__('F j, Y'),
'Y/m/d',
'm/d/Y',
'd/m/Y',
) ) );
$custom = true;
foreach ( $date_formats as $format ) {
echo "\t<label title='" . esc_attr($format) . "'><input type='radio' name='date_format' value='" . esc_attr($format) . "'";
if ( get_option('date_format') === $format ) { // checked() uses "==" rather than "==="
echo " checked='checked'";
$custom = false;
}
echo ' /> <span>' . date_i18n( $format ) . "</span></label><br />\n";
}
echo ' <label><input type="radio" name="date_format" id="date_format_custom_radio" value="\c\u\s\t\o\m"';
checked( $custom );
echo '/> ' . __('Custom:') . ' </label><input type="text" name="date_format_custom" value="' . esc_attr( get_option('date_format') ) . '" class="small-text" /> <span class="example"> ' . date_i18n( get_option('date_format') ) . "</span> <span class='spinner'></span>\n";
echo "\t<p>" . __('Documentation on date and time formatting.') . "</p>\n";
?>
</fieldset>
</td>
</tr>
I tried adding:
<?php m/d/Y ?>
To my website code to output the current date but it didn't work. What am I missing?
I think you must do
<?php echo date_i18n( 'm/d/Y' ) ?>
Try with:
$format = get_option('date_format') . ' ' . get_option('time_format');
print date_i18n($format, current_time('timestamp'));
WordPress has a current_time() function that returns the blog time, which might differ from the server time if you selected a different timezone in your blog settings...

wordpress nivo slider creating custom permalink option during post?

I am not using the nivo slider wordpress plugin, instead I am using regular nivo slider jquery and implemented it to work in wordpress, and currently my "read more" button is as follows:
<a href='".get_permalink()."'>Read More</a>
What I want to implement is something like a get_permalinkpage? So basically I want to be able to make the read more link to a wordpress page of my choosing instead of the post's permalink. But I do not know how to implement a custom option to the posts page that would allow the user to say "Choose from pages to link the nivo slider slide to: (then shows pages on website)" and then output that selection.
Any help? This is the last thing I need to implement for our website!
Right, I have your answer here as it's something I did myself recently. This is really a question about custom metaboxes. Here's some resources on it - I got sent a link on it from a mate who recommends this;
http://www.deluxeblogtips.com/meta-box/
And in the Bones theme the author recommends this one;
https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
I will post some code here if you want to quickly get going with it. I would place the following code in a file in it's own and include it from your functions.php, ie;
require_once('includes/metabox-post.php');
Create an includes directory in your theme directory and create a file containing this code;
<?php
/* CUSTOM METABOX -----------------------------------------------------*/
//We create an array called $meta_box and set the array key to the relevant post type
$meta_box_post['post'] = array(
//This is the id applied to the meta box
'id' => 'post-format-meta',
//This is the title that appears on the meta box container
'title' => 'My Custom Metabox',
//This defines the part of the page where the edit screen section should be shown
'context' => 'normal',
//This sets the priority within the context where the boxes should show
'priority' => 'high',
//Here we define all the fields we want in the meta box
'fields' => array(
array(
'name' => 'Home Slider Link',
'desc' => 'You can create a custom link for the home slider image (ie to link to the shop). If left blank, it will by default link through to this post.',
'id' => 'home-slide-link',
'type' => 'text',
'default' => ''
)
)
);
add_action('admin_menu', 'meta_add_box_post');
//Add meta boxes to post types
function meta_add_box_post() {
global $meta_box_post;
foreach($meta_box_post as $post_type => $value) {
add_meta_box($value['id'], $value['title'], 'meta_format_box_post', $post_type, $value['context'], $value['priority']);
}
}
//Format meta boxes
function meta_format_box_post() {
global $meta_box_post, $post;
// Use nonce for verification
echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box_post[$post->post_type]['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>'.
'<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
break;
case 'textarea':
echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
break;
case 'select':
echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
foreach ($field['options'] as $option) {
echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' /<br /> '. $field['desc'];
break;
}
echo '<td>'.'</tr>';
}
echo '</table>';
}
// Save data from meta box
function meta_save_data_post($post_id) {
global $meta_box_post, $post;
//Verify nonce
if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
//Check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
//Check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($meta_box_post[$post->post_type]['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
add_action('save_post', 'meta_save_data_post');
?>
The this will add a new custom metabox to your posts which you can type in an alternative url into. This custom option will have the id home-slide-link. To use that url you would include the following in your template loop whilst building up the list of Nivoslider images;
<?php
if ( get_post_meta($post->ID, 'home-slide-link', true) ) :
$slideLink = get_post_meta($post->ID, 'home-slide-link', true);
else :
$slideLink = get_permalink();
endif;
echo '<img src="image link in here" />';
?>
So if the post has a url set for the slider link then it uses that, if not it defaults to the permalink.
Hope this helps you a bit!
Here's my solution based on yours.
<div id="slider">
<?php
$captions = array();
$tmp = $wp_query;
$wp_query = new WP_Query('cat='.$category.'&posts_per_page=$n_slices' );
if($wp_query->have_posts()) :
while($wp_query->have_posts()) :
$wp_query->the_post();
$captions[] = '<p>'.get_the_title($post->ID).'</p><p>'.get_the_excerpt().'</p>';
$image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'nivothumb');
$mykey_values = get_post_custom_values('home-slide-link');
?>
<a href="<?php echo $mykey_values[0] ?>">
<img src="<?php echo $image[0]; ?>" class="attachment-nivothumb wp-post-image" title="#caption<?php echo count($captions)-1; ?>" alt="<?php the_title_attribute(); ?>" />
</a>
<?php
endwhile;
endif;
$wp_query = $tmp;
?>
</div><!-- close #slider -->
<?php
foreach($captions as $key => $caption) :
?>
<div id="caption<?php echo $key; ?>" class="nivo-html-caption">
<?php echo $caption;?>
</div>
<?php
endforeach;
?>

Resources