Powered By Blogger
Showing posts with label custom fields. Show all posts
Showing posts with label custom fields. Show all posts

Friday, October 28, 2016

add meta key or meta value or custom field for post

// Post offer box
add_action( 'add_meta_boxes', 'post_offer' );
function post_offer() {
   add_meta_box(
       'post_offer',
       __( 'Price', 'agrg' ),
       'post_offer_content',
       'post',
       'side',
       'high'
   );
}

function post_offer_content( $post ) {
wp_nonce_field( 'myplugin_meta_boxeee', 'myplugin_meta_box_nonceeee' );
$post_offer = get_post_meta( $post->ID, 'post_offer', true );

echo '<label for="post_offer"></label>';
echo '<input type="text" id="post_offer" name="post_offer" placeholder="'._e('Enter price here','agrg').'" value="';
echo $post_offer;
echo '">';

}

add_action( 'save_post', 'post_offer_save' );
function post_offer_save( $post_id ) {

global $post_offer;

if ( ! isset( $_POST['myplugin_meta_box_nonceeee'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonceeee'], 'myplugin_meta_boxeee' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}

if(isset($_POST["post_offer"]))
$post_offer = $_POST['post_offer'];
update_post_meta( $post_id, 'post_offer', $post_offer );

}

Wednesday, October 19, 2016

add custom fields user registration wordpress

http://blog.ashfame.com/2010/11/add-custom-field-registration-wordpress/



<?php
/*
Plugin Name: Custom Profile field as a Registration Field
Plugin URI: http://blog.ashfame.com/2010/11/add-custom-field-registration-wordpress/
Description: Sample plugin for adding a custom profile field to WordPress and on the registration page
Author: Ashfame
Version: 0.1
Author URI: http://blog.ashfame.com/
*/

/**
 * Add additional custom field
 */

add_action ( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action ( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields ( $user )
{
?>
<h3>Store information</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Twitter</label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your Twitter username.</span>
</td>
</tr>
<tr>
<th><label for="sname">Store Name</label></th>
<td>
<input type="text" name="sname" id="sname" value="<?php echo esc_attr( get_the_author_meta( 'sname', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your Store name.</span>
</td>
</tr>
</table>
<?php
}

add_action ( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action ( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
update_usermeta( $user_id, 'sname', $_POST['sname'] );
}

/**
 * Add cutom field to registration form
 */

add_action('register_form','show_first_name_field');
add_action('register_post','check_fields',10,3);
add_action('user_register', 'register_extra_fields');

function show_first_name_field()
{
?>
<p>
<label>Twitter<br/>
<input id="twitter" type="text" tabindex="30" size="25" value="<?php echo $_POST['twitter']; ?>" name="twitter" />
</label>
</p>
<p>
<label>Store Name<br/>
<input id="sname" type="text" tabindex="30" size="25" value="<?php echo $_POST['sname']; ?>" name="sname" />
</label>
</p>
<?php
}

function check_fields ( $login, $email, $errors )
{
global $twitter;
if ( $_POST['twitter'] == '' )
{
$errors->add( 'empty_realname', "<strong>ERROR</strong>: Please Enter your twitter handle" );
}
else
{
$twitter = $_POST['twitter'];
}

global $sname;
if ( $_POST['sname'] == '' )
{
$errors->add( 'empty_realname', "<strong>ERROR</strong>: Please Enter your Store name" );
}
else
{
$sname = $_POST['sname'];
}
}

function register_extra_fields ( $user_id, $password = "", $meta = array() )
{
update_user_meta( $user_id, 'twitter', $_POST['twitter'] );
update_user_meta( $user_id, 'twitter', $_POST['sname'] );
}

?>