Add custom fields to Author Information in WordPress - php

I'm looking for a way to add custom fields and display them (without plugin).
I've found a great example on the web. The author adds a number of custom fields by adding the following function to the fuctions.php file:
function modify_contact_methods($profile_fields) {
// Add new fields
$profile_fields['linkedin'] = 'LinkedIn URL';
$profile_fields['telephone'] = 'Telephone';
return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');
I've been able to successfully add such fields to the Contact Information section of my user registration form. I've been trying to add custom fields to other sections, like the Author Information section (where the Bio is), but without success.
I think that I've to change the value user_contactmethods in the add_filter(...) function, but I have not been able to find anything.
I do not even know if this is the correct way to do this, but it worked so far.

As you are new to wordpress, You don't have knowledge about the filter and action. If you go through the filter list , You will find user_contactmethods here.
As you can see in Author and User Filters, there are only 4 filters for Author and User. And we can use none of them to achieve your desired output.
But somehow we can do it by adding another field under the About the User something like Author Information.
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("Author Information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="author"><?php _e("Author Information"); ?></label></th>
<td>
<textarea name="author" id="author" rows="5" cols="10" ><?php echo esc_attr( get_the_author_meta( 'author', $user->ID ) ); ?></textarea><br />
<span class="description"><?php _e("Please enter Author's Information."); ?></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, 'author', $_POST['author'] );
}
So in that way you can add as many field as you want.

Related

WordPress/WooCommerce - What is the hook for custom category bulk edit?

I have added bulk action in the WooCommerce product category edit. I just want to know what the hook is when I click on Apply to do some actions on selected categories. (Specifically, I want to update all selected categories by updating the wp_termmeta table and set the example value to 1 or 0) The only thing that I want is the name of the hook when clicking the Apply button in /wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product.
Just copy & paste into your functions.php. Enjoy
// Note: change all occurrences of "custom_field" with the key of your custom field add_action( 'woocommerce_product_bulk_edit_start', 'bbloomer_custom_field_bulk_edit_input' ); function bbloomer_custom_field_bulk_edit_input() { ?> <div class="inline-edit-group"> <label class="alignleft"> <span class="title"><?php _e( 'Custom Field', 'woocommerce' ); ?></span> <span class="input-text-wrap"> <input type="text" name="custom_field" class="text" value=""> </span> </label> </div> <?php } add_action( 'woocommerce_product_bulk_edit_save', 'bbloomer_custom_field_bulk_edit_save' ); function bbloomer_custom_field_bulk_edit_save( $product ) { $post_id = $product->get_id(); if ( isset( $_REQUEST['custom_field'] ) ) { $custom_field = $_REQUEST['custom_field']; update_post_meta( $post_id, 'custom_field', wc_clean( $custom_field ) ); } }

How to get all WooCommerce product attributes taxonomies slugs and names

In WooCommerce I am trying to add a custom field to all of product attributes as a plugin…
For the moment in the code below, I am able to add a custom field to one taxonomy:
function pippin_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'pippin' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','pippin' ); ?></p>
</div>
<?php
}
add_action( 'pa_flavor_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function pippin_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'pippin' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','pippin' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'pa_flavor_edit_form_fields', 'pippin_taxonomy_edit_meta_field', 10, 2 );
How to get all WooCommerce product attributes taxonomies slugs (and names)?
This way I could make those custom fields dynamically for all existing product attributes.
You can get product attribute taxonomy slugs (and taxonomy names) by using:
Before WooCommerce version 3.6 with wc_get_attribute_taxonomies() dedicated function:
// Get an array of product attribute taxonomies slugs
$attributes_tax_slugs = array_keys( wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ) );
// Get an array of product attribute taxonomies names (starting with "pa_")
$attributes_tax_names = array_filter( array_map( 'wc_attribute_taxonomy_name', $attribute_taxonomies ));
Since WooCommerce version 3.6+ with wc_get_attribute_taxonomy_labels() dedicated function:
// Get an array of product attribute taxonomies slugs
$attributes_tax_slugs = array_keys( wc_get_attribute_taxonomy_labels() );
// Get an array of product attribute taxonomies names (starting with "pa_")
$attributes_tax_names = array_filter( array_map( 'wc_attribute_taxonomy_name', $attribute_taxonomies ));
Official documentation:
All WooCommerce related product attribute functions: wc-attribute-functions.php core file
WordPress: wp_list_pluck() function
PHP functions: array_keys(), array_filter() and array_map()

How to add custom field in edit user page

I would like to add new field in contact info section of edit user profile page.
I am using this code but it is adding new section after account management section but i want add it after email in contact info section.
<?php
add_action( 'show_user_profile', 'yoursite_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'yoursite_extra_user_profile_fields' );
function yoursite_extra_user_profile_fields( $user ) {
?>
<h3><?php _e("User profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="phone"><?php _e("Operational Manager Email"); ?></label></th>
<td>
<input type="text"
name="operational_email"
class="regular-text"
value="<?php echo esc_attr( get_the_author_meta( 'operational_email', $user->ID ) ); ?>"/>
<br />
<span class="description">
<?php _e("Please enter opertional manager email.">
</span>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'yoursite_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'yoursite_save_extra_user_profile_fields' );
function yoursite_save_extra_user_profile_fields( $user_id ) {
$saved = false;
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta(
$user_id,
'operational_email',
$_POST['operational_email']
);
$saved = true;
}
return true;
}
Well i had it done in my theme in recent project but i did it from the filter name 'user_contactmethods'
Read documentation : https://codex.wordpress.org/Plugin_API/Filter_Reference/contactmethods
add_filter( 'user_contactmethods', 'extra_contact_info' );
function extra_contact_info( $fields ) {
$fields['email'] = __( 'Operational Manager Email' );
return $fields;
}

Featured custom post type slider

I have a custom-post-type and I want to display a featured slider in my homepage with some of those custom posts.
I want to create a metabox to select if it's a featured post or not and only then display it.
This is the code that I have, which is not working as to add a metabox.
<?php function sm_custom_meta() {
add_meta_box( 'sm_meta', __( 'Featured Posts', 'sm-textdomain' ), 'sm_meta_callback', 'post' );
}
function sm_meta_callback( $post ) {
$featured = get_post_meta( $post->ID );
?>
<p>
<div class="sm-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $featured['meta-checkbox'] ) ) checked( $featured['meta-checkbox'][0], 'yes' ); ?> />
<?php _e( 'Featured this post', 'sm-textdomain' )?>
</label>
</div>
</p>
<?php
}
add_action( 'add_meta_boxes', 'sm_custom_meta' );
?>
I'm following the steps in this post and it's not working http://smallenvelop.com/how-to-create-featured-posts-in-wordpress/
I don't want to use any plugins.
I've changed the sm-textdomain to my theme name, and the 'post' to my slug-post-type.
Can you figure out what I'm doing wrong and help out?
Thanks.
check if you have other arrays or queries in the page, and since you mentioned it's a CPT, check if the query is looking in the post_type=>slug

Wordpress - Saving Custom User Profile Fields

I am currently trying to add some custom user profile fields for my Wordpress users.
I have added the following code into my functions.php but for some reason the data entered is not saving...
//** CUSTOM USER META **//
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>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="club">Club You Support</label></th>
<td>
<input type="text" name="club" id="club" value="<?php echo esc_attr( get_the_author_meta( 'club', $user->ID ) ); ?>" class="regular-text" /><br />
</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 ) {
update_usermeta( $user_id, 'club', sanitize_text_field( $_POST['club']) );
}
Any ideas as to why this data isn't sticking ?
There is an easier and proper way to create new profile fields in Wordpress. Based on your code above, try dropping the code below on your functions.php file on your theme:
function my_show_extra_profile_fields {
$user_contact_method['club'] = 'Club You Support';
return $user_contact_method;
}
add_filter( 'user_contactmethods', 'my_show_extra_profile_fields' );
This will automatically create the new fields on your profile page and accordingly save them to the data base as custom fields (meta) for user.
You can display this info on your theme using the_author_meta('club');

Categories