Adding Custom Fields In wordpress add new post page - wordpress

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');
}

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' );
?>

Custom Option Cart Woocommerce

to everyone !
Someone can help me with my proyect, Is very simple but I have one thing that makes me crazy.
I have this:
every checkbox have a value (additional value price)
I need change the price wen I checked in one, two or all checkbox and save this options to send it to the checkout and email....And show this options to order information in the back-end.
add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
function custom_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
$item_name .= $value3.'<div class="lineList">';
$value2 = $cart_item['data']->get_meta('_preciosCata');
if( $value1 = $cart_item['data']->get_meta('_varArrayTitle') ) {
for ($i=0;$i<count($value1); $i++) {
//$item_name .= '<div class="lineOne lorem"><input type="checkbox" name="cart[%s][lorem]" id="addPrice" value="' . $value2[$i] . '"> <span class="custom-field"> ' . $value1[$i] . ' </span></div>';
$item_name .= sprintf( '<div class="lineOne lorem"><input type="checkbox" id="_checkbox' . $i . '" name="_checkbox' . $i . '" value="' . $value2[$i] . '" class="checkedBox url text" /> <span class="custom-field">' . $value3.$value1[$i] . ' </span></div>', $cart_item_key, esc_attr( $values['url'] ) );
}
}
$item_name .= '</div>';
return $item_name;
}
I update the car here but dont save the value:
add_action( 'wp_footer', 'bbloomer_cart_refresh_update_qty' );
function bbloomer_cart_refresh_update_qty() {
if (is_cart()) {
?>
<script>
jQuery('div.woocommerce').on('change', '.checkedBox', function(){
jQuery("[name='update_cart']").prop("disabled", false);
jQuery("[name='update_cart']").trigger("click");
//alert($(this).val());
});
</script>
<?php
}
}
I need help please

Woocommerce custom add to cart loop

I am using Woocommerce Product Add-on plugin to allow visitor to select multiple options on simple product. As the product is simple product Woocommerce display an Add to cart button on products page view instead of Choose option button linking on Product detail page where visitor can choose options.
I am looking to create a function that will display Choose option button on product by ID.
Here is my starting code.
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button' );
function change_product_button() {
global $product;
$id = $product->id;
if ($id == 91){
echo '' . __('View Product', 'woocommerce') . '';
} else {
// display usual add to cart button
}
}
Or maybe we can force product by id as non purchasable to simulate a variable product.
This is a working solution
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button', 10, 2 );
function change_product_button($html, $product) {
$values = array(190, 91);
$id = $product->id;
if(in_array($id, $values)){
$html= '' . __('Choose an option', 'woocommerce') . '';
} else {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}

Datepicker and custom contact form (custom wp plugin)

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...

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