default value not getting displayed in input field wordpress - php

I have written code for creating theme options
but default values not getting displayed in text field
Also settings saved message also not getting displayed
Thank you
<?php
// Default options values
$xyz_options = array(
'footer_copyright' => '© ' . date('Y') . ' ' . get_bloginfo('name'),
'facebook'=>'http://www.facebook.com/'
);
if ( is_admin() ) : // Load only if we are viewing an admin page
function xyz_register_settings() {
// Register settings and call xyz initiation functions
register_setting( 'xyz_theme_options', 'xyz_options', 'xyz_validate_options' );
}
add_action( 'admin_init', 'xyz_register_settings' );
function xyz_theme_options() {
// Add theme options page to the admin menu
add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'xyz_theme_options_page' );
}
add_action( 'admin_menu', 'xyz_theme_options' );
// Function to generate options page
function xyz_theme_options_page() {
global $xyz_options;
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false; // This checks whether the form has just been submitted. ?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Options' ) . "</h2>";
// This shows the page's name and an icon if one has been provided ?>
<?php if ( false !== $_REQUEST['updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
<?php endif; // If the form has just been submitted, this shows the notification ?>
<form method="post" action="options.php">
<?php $settings = get_option( 'xyz_options', $xyz_options ); ?>
<?php settings_fields( 'xyz_theme_options' );
/* This function outputs some hidden fields required by the form,
including a nonce, a unique number used to ensure the form has been submitted from the admin page
and not somewhere else, very important for security */ ?>
<table class="form-table">
<tr valign="top">
<th scope="row">
<label for="facebook">Facebook url</label>
</th>
<td>
<input id="facebook" name="xyz_options[facebook]" type="text" value="<?php esc_attr_e($settings['facebook']); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="footer_copyright">Footer Text 1</label>
</th>
<td>
<input id="footer_copyright" name="xyz_options[footer_copyright]" type="text" value="<?php esc_attr_e($settings['footer_copyright']); ?>" />
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="Save Options" />
</p>
</form>
</div>
<?php
}
function xyz_validate_options( $input ) {
global $xyz_options;
$settings = get_option( 'xyz_options', $xyz_options );
}
endif; // EndIf is_admin()
I have wriiten above code.
Please help
Thank you

I tested your code and the default values are being displayed correctly.
Doing a var_dump($_REQUEST); shows what's the problem with the update message, we have to check simpy for $_REQUEST['settings-updated'].
You can remove the is_admin() checking, as admin_init and admin_menu only run on admin.
The validation function should be something like this, validating each input field and returning a clean array with sanitized values:
function xyz_validate_options( $input ) {
$new_input = array();
if( isset( $input['facebook'] ) )
$new_input['facebook'] = esc_url( $input['facebook'] );
if( isset( $input['footer_copyright'] ) )
$new_input['footer_copyright'] = esc_attr( $input['footer_copyright'] );
return $new_input;
}

Related

Update post meta from frontend of different page

I'm trying to update the post content of a specified posttype. For some reason it's not working.
I have already successfully used the code in the backend in the mail page for editing. Now I want to use it also in the frontend for employees for quick administration.
$id is transferred correctly. I used this code
This is my code snippet:
<?php $id = $_GET['car']; ?>
<?php
if ( isset( $_POST['submit'] ) ){
$fields = [
'car-highlight-2',
];
foreach ( $fields as $field ) {
if ( array_key_exists( $field, $_POST ) ) {
update_post_meta( $id, $field, sanitize_text_field( $_POST[$field] ) );
}
}}
?>
<form method="post" action="">
<tr>
<td><input type="text" id="car-highlight-2" name="car-highlight-2" value="<?php echo get_post_meta($id, 'car-highlight-2', true); ?>"></td>
</tr>
<input type='submit' value='save' />
</form>
I forgot to add name="submit" to the input.

Kindly help to change input type text to radio from the PHP code

I'm trying to add and change the input type from text to radio button to use in my wordpress theme user profile settings like Gender: Male Female, but can't do the trick in the code. don't know how to change it.
tried with some code from w3schools given below, but that doesn't work too.
Tried radio type, but doesn't work
<label><?php _e( 'gender', 'themer' ); ?></label>
<input type="radio" name="user_new_field" <?php if (isset($new_field) && $new_field=="female") echo "checked";?> value="female">Female
<input type="radio" name="user_new_field" <?php if (isset($new_field) && $new_field=="male") echo "checked";?> value="male">Male
</div>
i have to change the code to radio type, gonna use them in wordpress functions.php
add_action( 'themer_after_user_profile_registration_fields_action', 'wpt_show_user_profile_custom_inputs' );
function wpt_show_user_profile_custom_inputs( $uid ) {
$user_new_field = get_user_meta( $uid, 'user_new_field', true );
$new_field = empty( $_POST['user_new_field'] ) ? $user_new_field : stripslashes( $_POST['user_new_field'] ); ?>
<div class="field">
<label><?php _e( 'gender', 'themer' ); ?></label>
<input type="text" value="<?php echo $new_field; ?>" name="user_new_field" size="40" placeholder="<?php echo _x( 'My New Field Placeholder', 'Placeholder for: New field', 'themer' ); ?>" />
</div>
<?php }
// Save the field value from user settings page
add_action( 'themer_user_profile_extra_fields_update', 'wpt_save_user_profile_custom_inputs' );
function wpt_save_user_profile_custom_inputs( $uid ) {
if ( isset( $_POST['save-info'] ) ) {
if ( isset( $_POST['user_new_field'] ) ) {
update_user_meta( $uid, 'user_new_field', $_POST['user_new_field'] );
}
}
}
You can do this by replacing your code with below code in functions.php
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra Field", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label ><?php _e("Gender"); ?></label></th>
<td>
<input type="radio" name="gender" id="gender" value="male" <?php if(esc_attr( get_the_author_meta( 'gender', $user->ID ) ) == 'male'){echo ' checked '; } ?>class="regular-text" />Male
<input type="radio" name="gender" id="gender" value="female" <?php if(esc_attr( get_the_author_meta( 'gender', $user->ID ) ) == 'female'){echo ' checked '; } ?> class="regular-text" />Female
<br />
<span class="description"><?php _e("Please enter your gender."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'gender', $_POST['gender'] );
}

wordpress edit wp_users table field from admin

I added a field "money" to wp_users table, whehe store a user money amount. Now i want to take ability for admin to change that input field value.
I can get the value with function added to functions.php
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>User money</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Amount</label></th>
<td>
<input type="text" name="money" id="money" value="<?php echo esc_attr( get_the_author_meta( 'money', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Enter user money</span>
</td>
</tr>
</table>
<?php }
but how can i update that field?
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
update_user_meta($user_id, 'money', $_POST['money']);
}
Would you please try above code?
finally I made in such a way
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
$money = $_POST['money'];
global $wpdb;
$wpdb->query("UPDATE wp_users SET money='$money' WHERE ID = '$user_id'");
}

How to get Text Area in WordPress posts section?

I need to change this code to implement "More info" field as a text field in my WordPress post section.
The "More info" field looks like this:
I use smartmetabox. It has 2 files:
textarea.php:
<textarea name="<?php echo $id?>" id="<?php echo $id?>" rows="5" cols="100" class="custom"><?php echo $value?></textarea>
text.php:
<input type="text" name="<?php echo $id?>" id="<?php echo $id?>" value="<?php echo $value?>" class="regular-text" />
And my_file.php code is:
<?php
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', __('Information', 'addict'), 'cd_meta_box_cb', 'post', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$creteria_1_text = isset( $values['creteria_1_text'] ) ? esc_attr( $values['creteria_1_text'][0] ) : '';
$check = isset( $values['my_meta_box_check'] ) ? esc_attr( $values['my_meta_box_check'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="creteria_1_text"><b><?php _e("More info, 'addict') ?></b></label>
<input style="width:85%" type="text" name="creteria_1_text" id="creteria_1_text" value="<?php echo $creteria_1_text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['creteria_1_text'] ) )
update_post_meta( $post_id, 'creteria_1_text', wp_kses( $_POST['creteria_1_text'], $allowed ) );
}
// function for show rating content
//$key_1_value = get_post_meta($post->ID, 'my_meta_box_text', true);
?>
After some digging i found that i should replace this code:
<input style="width:85%" type="text" name="creteria_1_text" id="creteria_1_text" value="<?php echo $creteria_1_text; ?>
To:
<textarea name="creteria_1_text" id="creteria_1_text" rows="5" cols="100" class="custom"><?php echo $creteria_1_text; ?></textarea>

Get the value from a custom field in category

I made a custom field in admin category interface named custom order. It is showing in the admin. But now I am having difficulties in getting the information from the field and echo it in index.php.
If I run
$thisCat = get_category( 29);
print_r($thisCat);
I don't get any information from the custom field.
echo get_post_custom_values('category_custom_order', 29); doesn't echo anything.
How should I get the value from category_custom_order?
Here is my code for custom field in functions.php:
<?php
/** Add Custom Field To Category Form */
add_action( 'category_add_form_fields', 'category_form_custom_field_add', 10 );
add_action( 'category_edit_form_fields', 'category_form_custom_field_edit', 10, 2 );
function category_form_custom_field_add( $taxonomy ) {
?>
<div class="form-field">
<label for="category_custom_order">Custom Order</label>
<input name="category_custom_order" id="category_custom_order" type="text" value="" size="40" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</div>
<?php
}
function category_form_custom_field_edit( $tag, $taxonomy ) {
$option_name = 'category_custom_order_' . $tag->term_id;
$category_custom_order = get_option( $option_name );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="category_custom_order">Custom Order</label></th>
<td>
<input type="text" name="category_custom_order" id="category_custom_order" value="<?php echo esc_attr( $category_custom_order ) ? esc_attr( $category_custom_order ) : ''; ?>" size="40" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</td>
</tr>
<?php
}
/** Save Custom Field Of Category Form */
add_action( 'created_category', 'category_form_custom_field_save', 10, 2 );
add_action( 'edited_category', 'category_form_custom_field_save', 10, 2 );
function category_form_custom_field_save( $term_id, $tt_id ) {
if ( isset( $_POST['category_custom_order'] ) ) {
$option_name = 'category_custom_order_' . $term_id;
update_option( $option_name, $_POST['category_custom_order'] );
}
}
You should be able to get it this way, and by the way it's not a custom field but an option.
$category_id = 29;
$category_custom_order = get_option( 'category_custom_order_' . $category_id );
echo $category_custom_order;

Categories